|
Add an horizontal scrollbar to a ListBox control
|
Total Hit (4722) |
By default, VB ListBox controls don't display an horizontal scrollbar, so if the items you add to the controls are wider than the control's Width, the end user isn't able to read them in their entirety.
Adding an horizontal scrollbar to a ListBox control is easy, however. You only have to increas
....Read More |
Rating
|
|
|
Add size grips to a form
|
Total Hit (2771) |
There are two simple ways to add the so-called size-grips to the bottom-right corner of a form. The first method is to add a StatusBar control, which includes a size-grip of its own. The second method is to add a multiline TextBox control, set its ScrollBars property to 3-Both and add some resizing
....Read More |
Rating
|
|
|
Append a string to a textbox quickly and without flickering
|
Total Hit (2859) |
The usual way to append text to a TextBox or a RichTextBox control is to concatenate the current contents with the new string:
«Code LangId=1»
Text1.Text = Text1.Text & newString
«/Code»
Here is a different approach, which is slightly faster and causes less flickering: «Code LangId=1»
Text1
....Read More |
Rating
|
|
|
Avoid beeps on forms without a default button
|
Total Hit (3924) |
If a form contains one CommandButton control whose Default property is set to True, the Enter key activates the CommandButton. If the form doesn't contain any default CommandButton control, however, pressing the Enter key when the focus is inside a TextBox control (and a few other controls as well)
....Read More |
Rating
|
|
|
Bind a group of OptionButtons to a Data control
|
Total Hit (2805) |
You can't directly bind a group of OptionButton controls to a Data control, RDO Data control or ADO Data control. However, you can work around this limitation by adding an hidden TextBox control and add a few lines of code:
«Code LangId=1»
Private Sub optRadioButton_Click(Index As Integer)
....Read More |
Rating
|
|
|
Catch user's attention with a flashing caption
|
Total Hit (3257) |
If you want to draw user's attention but you don't want to force the form to move to the foreground, you can simply flash its caption (and its icon in the taskbar), using the FlashWindow API function. To do so, just add a Timer with a suitable Interval property (for example, 1000 milliseconds) and t
....Read More |
Rating
|
|
|
Caution when moving or resizing forms
|
Total Hit (2714) |
Any action that affects a form's size or position should be inhibited when the form is minimized or maximized, because this causes a runtime error 384. Always double check your code to ensure that when a form is moved or resized – using a Move method or acting on a form's Left, Top, Width or Height
....Read More |
Rating
|
|
|
Change a CheckBox or OptionButton style at runtime
|
Total Hit (4203) |
Visual Basic doesn't let you change the Style property of a CheckBox or an OptionButton control at runtime. However, you can easily do it by manipulating the control's style bit, with the SetWindowLong API function. Here's a routine that does the trick:
Using the routine is straightforward. For
....Read More |
Rating
|
|
|
Change caret size and blink rate
|
Total Hit (3932) |
You can alter the size of the caret (this is the name of the cursor within text boxes, not to be confused with the mouse cursor), for instance to ease visibility on notebook computers, or to signal that the text box is in overwrite mode. All you need is a couple of API functions:
«Code LangId=1»
....Read More |
Rating
|
|
|
Change the DataSource at runtime
|
Total Hit (3714) |
Visual Basic 6 is the first VB version that lets you programmatically change the DataSource property at runtime, for example to point to another ADO Data control or another ADO Recordset.
However, when you assign the new DataSource property VB immediately checks that the DataField and DataMember
....Read More |
Rating
|
|
|
Change the ShowInTaskbar property at runtime
|
Total Hit (4336) |
The ShowInTaskbar property lets you decide whether a form is visible in Windows taskbar or not. However, this property is read-only at runtime, so it seems that you can't change this setting while the program is running. Luckly, you just need to change the window's style, using a pair of API functio
....Read More |
Rating
|
|
|
Change the width of the dropdown portion of a ComboBox
|
Total Hit (3258) |
The ComboBox control doesn't expose any property that lets you to control the width of its list area, but this can be easily accomplished by sending it the CB_SETDROPPEDWIDTH message, and passing the new length in pixel in wParam:
«Code LangId=1»
Private Declare Function SendMessage Lib "user32"
....Read More |
Rating
|
|
|
Check whether a form is loaded
|
Total Hit (2745) |
You can load several instances of the same form, but VB doesn't let you determine how many forms of a given class are currently loaded. You can work around this flaw by iterating over the Forms collection:
«Code LangId=1»
' Return the number of instances of a form
' that are currently loaded
....Read More |
Rating
|
|
|
Clone a Font object
|
Total Hit (2788) |
When you want to assign a control's Font to another control, the first obvious way is to assign the Font property directly, as in:
«Code LangId=1»
Set Text2.Font = Text1.Font
«/Code»
but in most cases this approach doesn't really work, because it assigns a reference to the same font to both c
....Read More |
Rating
|
|
|
Delete a folder and all its subfolders
|
Total Hit (3332) |
The RmDir command can delete a directory only if it doesn't contain files or sub-directories. If the directory you want to delete does contain other files or, worse, subdirectories it seems that you are forced to use a recursive routine that does the job.
A simpler solution is offered by the Dele
....Read More |
Rating
|
|
|
Get the canonical name of a file
|
Total Hit (3352) |
In many cases you may need the canonical (or absolute) name of a file, for example when you need to compare two relative file names (relative to the current directory or drive, that is) and decide whether they point to the same or different files.
You can obtain the canonical path of a file usin
....Read More |
Rating
|
|
|
|
Load a text file in one operation
|
Total Hit (2821) |
The fastest way to read a text file is using the Input$ function, as shown in this reusable procedure:
«Code LangId=1»
Function FileText (filename$) As String
Dim handle As Integer
handle = FreeFile
Open filename$ For Input As #handle
FileText = Input$(LOF(handle), handle)
....Read More |
Rating
|
|
|
Retrieve information on all available drives
|
Total Hit (3025) |
You can retrieve information about all the available drives using calls to Windows API, if you like the hard way of doing things. A much simpler solution is offered by the Microsoft Scripting Runtime library, that exposes a Drive object that lets you get all those info by querying a property:
....Read More |
Rating
|
|
|
Search a file in a directory tree using the Imagehlp DLL
|
Total Hit (3659) |
You can search a file in all the subdirectories of a given drive in VB using a recursive routine based on the Dir$ function, the FileSystemObject component, or the FindFirstFile/FindNextFile API functions. There is a fourth way you might want to try out, based on the SearchTreeForFile function embed
....Read More |
Rating
|
|
|
Using the CreateDirectory and CreateDirectoryEx API functions
|
Total Hit (6741) |
The VB's MkDir function creates a directory in a specified path. If the directory already exists, MkDir raises error 75 (Path/file access error); yet, it raises the same error code if you attempt to create the directory on a read-only drive. Even worse, in Windows NT/2K/XP workstations, if you try t
....Read More |
Rating
|
|
|
Add comments to End If and Loop statements
|
Total Hit (2370) |
Here's a little programming tip that will save you hours of headaches later.
Most of us already indent our Ifs, Selects, Do...Loops, etc., and that is good. But suppose you have some fairly complex code with several levels of indentation. Example:
«Code LangId=1»
If A= 0 and B=1 then
'
....Read More |
Rating
|
|
|
Convert a VB6 project back to VB5
|
Total Hit (2330) |
If you load a VB6 project file in the VB5 environment you get a warning, even though the project is correctly loaded. The cause of the warning is a VB6 attribute "Retained" (that is, the "Retained in memory" option in the Project Properties dialog box) that isn't recognized by VB5. If you want to ma
....Read More |
Rating
|
|
|
Copying menu controls from one form to another
|
Total Hit (2652) |
If you're building a project with multiple similar forms, it's nice to be able to cut-and-paste controls from one form to another. However, in VB6, you can't do this for menu controls - unless you get tricky. The trick is to make an end-run around the VB IDE. Warning: don't try this unless you have
....Read More |
Rating
|
|
|
Fast Copy of Properties between Controls
|
Total Hit (2341) |
You probably already know that if you select multiple controls on a form you can use the Property Window to change one of their common property in one single operation. However, here is a trick that is likely to be ignored by most programmer (included me, until recently).
If you want to copy a n
....Read More |
Rating
|
|
|
Logging under a different name makes your add-ins disappear
|
Total Hit (2148) |
After you install Visual Basic and log on to the machine as a different user (with or without administrator privileges), you cannot see any Add-Ins in the Add-In dialog in Visual Basic.
I ran across this problem when I logged in and installed VB6 on NT Workstation under my User, for a developer
....Read More |
Rating
|
|
|
Multiple compilation constants
|
Total Hit (2217) |
Visual Basic's documentation does not explain how to specify more than just one conditional compilation constant in the Advanced tab of the Options dialog. The correct way is using a colon as a delimiter, as in:
demo = -1: version = 100
Note that you can only assign integer numeric values, an
....Read More |
Rating
|
|
|
Quick Property Edits
|
Total Hit (2148) |
When you are placing controls on a form at design time and wish to edit one of its properties, the quickest way to switch to the Property window is pressing the Ctrl+Shift+key combination, where key is the first letter in the property name. For instance, to quickly modify the Caption property you si
....Read More |
Rating
|
|
|
Quickly copy field attributes when creating tables
|
Total Hit (2949) |
Under VB6 you can create new SQL Server and Oracle tables - but not MDB tables - without leaving the environment. You only have open the DataView window, right-click on the Tables folder of a database, and select the New Table mennu command.
When working with similar tables, that is tables with m
....Read More |
Rating
|
|
|
Start the IDE with maximized code and form windows
|
Total Hit (2196) |
The VB IDE remembers most of the configuration settings that were active when you closed the previous session. However, the maximized status of child MDI windows (the code editor and the designer window) isn't remembered, and the IDE always starts with non-maximized windows. To have VB always start
....Read More |
Rating
|
|