|
Making a form appear as if it is disabled
|
Total Hit (2192) |
By changing the WS_CHILD style bit of a window, you can make it appear as if it is disabled, even if the form is fully active and functional. These are the statements you need:
|
Rating
|
|
|
Move focus with Up and Down keys
|
Total Hit (1829) |
In all standard controls, Up and Down arrow keys move the focus on the previous or next control in the TabIndex order, respectively. This contrasts with text boxes, where these keys move the caret on the adjacent character, which you can achieve in a more natural way using the Left and Right cursor
....Read More |
Rating
|
|
|
Mutually exclusive list boxes
|
Total Hit (2085) |
Many Windows programs use two adjacent list box controls to let the user select a number of items from a list of available values; such list boxes are mutually exclusive, in the sense that a given item always appear in the list box on the left (available items) or in the list box on the right (items
....Read More |
Rating
|
|
|
Open the list portion of a ComboBox control
|
Total Hit (3029) |
To programmatically open and close the list portion of a ComboBox control, all you need is sending the CB_SHOWDROPDOWN message to the control. Here is a routine that encapsulate the SendMessage API function:
«Code LangId=1»
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
....Read More |
Rating
|
|
|
Optimized Paint procedures with subclassing
|
Total Hit (2582) |
The Paint event doesn't provide with information about which region of the form must be actually repainted, and therefore forces the programmer to repaint the entire client area, which in some cases can be a time-consuming operation.
You can determine the smallest rectangle that needs to be updat
....Read More |
Rating
|
|
|
Overwrite mode for textbox controls
|
Total Hit (2392) |
By default, textbox controls work in insert mode, where each new character never overwrites existing ones. If you wish to implement overwrite mode you can take advantage of the fact that characters pressed by the user replace the currently selected text, if any.
You need to declare a form-level
....Read More |
Rating
|
|
|
Prevent an iconized window from being restored
|
Total Hit (1766) |
When the user clicks on an iconized (minimized) form in the taskbar, Windows sends that form a WM_QUERYOPEN message, and inspects the value returned to the operating system. If this value is zero, the operation is canceled.
Using subclassing you can therefore determine when a form is about to be
....Read More |
Rating
|
|
|
Provide a short description of the menu item being highlighted
|
Total Hit (2389) |
Visual Basic lacks of the capability to display a short description of the menu command being highlighted with the mouse or the keyboard, a feature that all professional applications should have. To add menu descriptions to your program, you must subclass the form and trap the WM_MENUSELECT message.
....Read More |
Rating
|
|
|
|
Quickly build a simple About form
|
Total Hit (2299) |
If you need a quick-and-dirty About dialog box for your application, that maintains a consistent look with other Windows applications, look no further than the ShellAbout routine in SHELL32.DLL. Using this function you have little control on what the dialog displays, and you can only customize the P
....Read More |
Rating
|
|
|
Quickly find which OptionButton is selected
|
Total Hit (1686) |
Often OptionButton controls are arranged in control arrays. To quickly find the index of the only selected OptionButton control you can use the following code:
«Code LangId=1»
' assumes that the control array contains three OptionButton controls
intSelected = Option(0).Value * 0 - Option(1).Va
....Read More |
Rating
|
|
|
Read and modify a TextBox control's formatting rectangle
|
Total Hit (3460) |
By sending appropriate messages to a multi-line TextBox control you can read and modify its formatting rectangle - that is, the inner portion of the control where the user can type. Modifying the size and position of this area can be useful, for example, to leave a left margin where you can add line
....Read More |
Rating
|
|
|
References to form and controls prevent complete form unloading
|
Total Hit (1796) |
When you assign a reference to a form (or one of its controls) to an object variable which is stored outside the form module, you must set the variable to Nothing or the form won't be completely unloaded. In other words, it will become invisible but will continue to take memory.
Note that this i
....Read More |
Rating
|
|
|
Remove the Close command from the system menu
|
Total Hit (3233) |
If you want to prevent the user from closing a form by using Alt+F4 or by clicking on the "X" button in the upper-right corner you just need to write some code in the form's QueryUnload event procedure:
«Code LangId=1»
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
....Read More |
Rating
|
|
|
|
Set tab stop positions for a multiline TextBox control
|
Total Hit (2510) |
By default, multiline TextBox controls insert a tab stop position every 8 characters; in other words, if you press the Ctrl+Tab key while the focus is inside a multiline TextBox control, the caret advances to position 8,16,24, etc. (If the TextBox is the only control on the form that can receive the
....Read More |
Rating
|
|
|
Show a custom caret
|
Total Hit (2636) |
VB gives you no control on the size of the text caret. At times it can be necessary to change its size, however, for example when you want to signal that the TextBox is in overwrite mode. Here's a routine that does the trick:
«Code LangId=1»
Private Declare Function GetFocus Lib "user32" () As
....Read More |
Rating
|
|
|
Show a custom popup menu for a TextBox without subclassing
|
Total Hit (2072) |
Elsewhere in the TipBank we show how you can display a custom popup menu on a TextBox control by subclassing the WM_CONTEXTMENU message that Windows sends the control when the user right-clicks on it. If you don't like to resort to subclassing for such an easy job, you can use the following tip, tak
....Read More |
Rating
|
|
|
Smart Tab key processing in multiline TextBox controls
|
Total Hit (2468) |
The only way for a Tab key to insert a tab character in a multiline text box is that the text box is the only control on the form, or at least the only control whose TabStop property is set to True. Otherwise, pressing the Tab key you simply move the focus to another control on the form.
If there
....Read More |
Rating
|
|
|
|
Ensure that a TextBox caret is visible
|
Total Hit (3145) |
Setting a multiline TextBox's SelStart property doesn't ensure that the insertion point (the caret) is visible. You can achieve this by sending the control an appropriate message:
|
Rating
|
|
|
Extended user interface for combo boxes
|
Total Hit (2950) |
Apart from the different kind of combo boxes you can obtain by manipulating the Style property, another feature is made available by Windows when dealing with combo box controls, the so-called extended user interface. With standard combo box controls, the list portion stays invisible until the user
....Read More |
Rating
|
|
|
Get a reference to a form given its name
|
Total Hit (1964) |
In some cases you might want to activate a form given its name, but VB doesn't let you do it directly. However, it is easy to create a function that does it by iterating over all the forms in the current project:
|
Rating
|
|
|
Get the handle of the edit portion of a ComboBox
|
Total Hit (3004) |
The ComboBox control contains an invisible Edit window, which is used for the edit area. In most cases you don't need to access this inner control directly, but occasionally a direct control of that window can be useful. The first thing to do is get the handle of such inner Edit control, which you d
....Read More |
Rating
|
|
|
Hide and Show a control's scrollbars
|
Total Hit (3302) |
Most VB controls don't let you determine whether they should display a scrollbar or not. For example, a VB ListBox control displays a vertical scrollbar only when the number of its items is larger than the number of visible items. If you want to directly control individual scrollbars, you can use th
....Read More |
Rating
|
|
|
Highlight current word and line in a TextBox control
|
Total Hit (3043) |
Here's a simple way to highlight the current word in a TextBox control (i.e. the word where the caret is):
«Code LangId=1»
Text1.SetFocus
SendKeys "^{LEFT}+^{RIGHT}"
«/Code»
Similarly, you can highlight the current line in a multiline TextBox control as follows: «Code LangId=1»
Text1.SetFo
....Read More |
Rating
|
|
|
Highlight the contents of a control on entry
|
Total Hit (1849) |
Here's a simple way to highlight the current word in a TextBox control (i.e. the word where the caret is):
«Code LangId=1»
Text1.SetFocus
SendKeys "^{LEFT}+^{RIGHT}"
«/Code»
Similarly, you can highlight the current line in a multiline TextBox control as follows: «Code LangId=1»
Text1.SetFo
....Read More |
Rating
|
|
|
Implement a MaxLength property for the ComboBox control
|
Total Hit (2190) |
Unlike the TextBox control, the ComboBox control doesn't expose any MaxLength property, so you have no means of limiting the numbers of characters typed by the end user in the edit area. However you can set this value by sending a CB_LIMITTEXT message to the control, passing the maximum number of ch
....Read More |
Rating
|
|
|
Incremental searches within list boxes
|
Total Hit (2595) |
The DBList and DBCombo controls expose a MatchEntry property, that - when set to True - permits to perform quick incremental searches by simply typing the searched sequence of characters. If MatchEntry is set to False, any time the user presses a key, the next item that begins with that character be
....Read More |
Rating
|
|
|
Make a Checkbox control read-only
|
Total Hit (3003) |
By default, VB's CheckBox controls automatically toggle their Value property when the user clicks on them. This is usually the desired behavior, but at times you may want to be able to change their value only programmatically. Unfortunately, you can't achieve this result by simply setting the CheckB
....Read More |
Rating
|
|