|
Correctly set scrollbars' width and height
|
Total Hit (3297) |
You should always modify a vertical scrollbar's width and a horizontal scrollbar's height to conform to the display resolution. You can learn the suggested size (in pixels) using the GetSystemMetrics API function, then convert it to twips, and rezise all the scrollbars on your forms accordingly. The
....Read More |
Rating
|
|
|
Create a owner form with API functions
|
Total Hit (3066) |
Starting with VB5, the Show method supports a second optional argument, that lets you specify which form owns the form that is about to be displayed:
«Code LangId=1»
Form2.Show , Me
«/Code»
A owned form is always displayed in front of its owner, and when the owner is unloaded, VB automatical
....Read More |
Rating
|
|
|
Create arrowed buttons without icons
|
Total Hit (4023) |
You don't need to use icons or bitmap to create buttons with arrows on them. In fact, you just have to select the "Windings" font, set a suitable font size (e.g. 12 or 14 points), and then enter one of these characters:
«Code LangId=1»
Line arrows
Chr$(223) Left Arrow
Chr$(224) Right Arrow
Chr
....Read More |
Rating
|
|
|
Create colorful Command Buttons
|
Total Hit (4273) |
The VB CommandButton control supports neither the ForeColor nor the BackColor properties. If you want to create colorful buttons without resorting to 3rd party controls, you can use an OptionButton control with the Style property set to 1-Graphical. Such OptionButton controls appear to be very simil
....Read More |
Rating
|
|
|
Create ListBox controls with companion priority buttons
|
Total Hit (2862) |
You can easily create a ListBox control similar to the one found in the VB's Project References dialog box, that lets you select multiple items and move them up and down using two companion buttons.
First of all, create a ListBox control (say, List1) and two CommandButton controls to its right, n
....Read More |
Rating
|
|
|
Create TextBox with dithered background
|
Total Hit (2144) |
If your video display has 256 colors or less and you assign a dithered color to the BackColor property of a TextBox control, you'll find that the background color under the text inside the TextBox is displayed in a different (solid) color.
To work around this issue, you must trap the WM_CTLCOLOR
....Read More |
Rating
|
|
|
Cut, copy, and paste using API functions
|
Total Hit (4771) |
Copying and pasting text and images programmatically isn't difficult at all, using the methods of the Clipboard object. However, implementing a generic Edit menu that copies and pastes the highlighted contents of whatever control is the selected control is often cumberson. For example, depending on
....Read More |
Rating
|
|
|
Detect whether a field on a form has been edited
|
Total Hit (2018) |
Many tip & trick collections report that you can learn if the contents of a text box has been modified by sending a EM_GETMODIFY message to the control. Actually, getting this information is much more simple. Just test the DataChange property: if non zero the field has been modified since the form w
....Read More |
Rating
|
|
|
Determine how a control got the focus
|
Total Hit (2388) |
At times it might be convenient to know how a given control got the focus, namely by means of the Tab key, an associated hotkey or a click of the mouse. You can learn this information through the GetKeyState API function, that returns the current state of a key. Here is a simple function that is mea
....Read More |
Rating
|
|
|
Determine maximum size and the position of a maximized form
|
Total Hit (2433) |
When the user resizes a form with the mouse or the keyboard, or maximizes it, Windows sends the form a WM_GETMINMAXINFO message, with lParam pointing to a MINMAXINFO structure that the form must fill with information about the minimum and maximum size that it is willing to be resized. Using a subcla
....Read More |
Rating
|
|
|
|
Determine when a ComboBox's area is being closed
|
Total Hit (2206) |
The ComboBox control exposes the DropDown event, that lets you determine when its list area is being opened. However, VB doesn't offer any simple way to understand when the list area is being closed. You can achieve this by intercepting the WM_COMMAND message that the ComboBox control sends to its c
....Read More |
Rating
|
|
|
Determine when a TextBox control is scrolled
|
Total Hit (1740) |
When the user scrolls the contents of a TextBox control - either by clicking on the companion scrollbars or by typing new characters in the edit area - the TextBox control sends its parent form a WM_COMMAND message, and passes its own hWnd property in lParam and the value EN_HSCROLL or EN_VSCROLL in
....Read More |
Rating
|
|
|
Determine whether a control has a scrollbar
|
Total Hit (2846) |
There is no built-in VB function that lets you know whether a control - such as a ListBox - is currently displaying a vertical scrollbar. Here's a pair of functions that check the control's style bits and return a Boolean that tells if a vertical or horizontal scrollbar is visible:
«Code LangId=1
....Read More |
Rating
|
|
|
Don't hard-code font names and size
|
Total Hit (1761) |
Unless you have good reason to do otherwise, you should always use standard fonts in your programs, because this ensures that your application will work on every Windows system.
If you want to use non-standard fonts, you should at least adopt the following guidelines:
«Code LangId=1»
Assign
....Read More |
Rating
|
|
|
Don't stop timers when a MsgBox is active
|
Total Hit (2143) |
Not all developers know that Timer controls in compiled VB5 and VB6 applications aren't stopped by MsgBox statement, so the Timer's Timer event procedure is regularly executed even when the message box is being displayed on the screen.
However, inside interpreted applications under all VB version
....Read More |
Rating
|
|
|
Don't use SetFocus on invisible controls
|
Total Hit (2086) |
In the Show event of a form you might be tempted to use SetFocus to select which field the end user should begin working with. However, this method raises an error 5 when applied on a control that is currently invisible.
If the logic of your application should give the focus always to the same c
....Read More |
Rating
|
|
|
|
Enable and Disable all or part of a scrollbar
|
Total Hit (3544) |
The new FlatScrollbar controls expose the ability to selectively disable their arrows. This is useful, for example, when the thumb indicator is at its minimum or maximum:
«Code LangId=1»
Private Sub FlatScrollBar1_Change()
If FlatScrollBar1.Value = FlatScrollBar1.Min Then
FlatScrol
....Read More |
Rating
|
|
|
Ensure that a form's TextBox and ComboBox controls have same height
|
Total Hit (3261) |
In general you don't have any control on a ComboBox's Height property, because it is determined automatically by VB depending on the font used for the ComboBox control. If you have a form that contains both TextBox and ComboBox controls, you should ensure that all your single-line TextBox controls a
....Read More |
Rating
|
|
|
Bind a group of OptionButtons to a Data control
|
Total Hit (2887) |
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 (3340) |
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 (2802) |
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 (4302) |
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 (4043) |
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 (3799) |
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 (4437) |
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 (3348) |
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 (2805) |
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 (2896) |
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
|
|