|
Overwrite mode for textbox controls
|
Total Hit (2328) |
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 (1718) |
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 (2336) |
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 (2245) |
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 (1634) |
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 (3393) |
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 (1745) |
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 (3159) |
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 (2443) |
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 (2571) |
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 (2013) |
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 (2418) |
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
|
|
|
|
Suppress Max and Min buttons in MDI forms
|
Total Hit (2107) |
Unlike regular forms, forms don't expose the MinButton and MaxButton properties and these buttons are always displayed. You can suppress these buttons using the following code:
|
Rating
|
|
|
The behavior of the LostFocus event depends on VB version
|
Total Hit (1707) |
The behavior of the LostFocus event has changed in the transition from VB4 to VB5. Under VB4, if a control on a form has the input focus and the user clicks on a toolbar button, the control fires a LostFocus event. Under VB5 and VB6, the same action doesn't fire any LostFocus event, so you must awar
....Read More |
Rating
|
|
|
The fastest way to append contents to a TextBox control
|
Total Hit (1916) |
The most intuitive way to append a string to the current contents of a TextBox is through the & string operator, as in:
«Code LangId=1»
Text1.Text = Text1.Text & "more text"
«/Code»
However, you can reach the same result about 4 times faster with the following method: «Code LangId=1»
Tex
....Read More |
Rating
|
|
|
The first visible line in a multiline TextBox control
|
Total Hit (3951) |
To determine the index of the first visible line in a multiline TextBox control you only need to send it the EM_GETFIRSTVISIBLELINE message:
«Code LangId=1»
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _
hwnd As Long, ByVal wMsg As Long, ByVal wParam As Lon
....Read More |
Rating
|
|
|
The number of lines in a multiline TextBox control
|
Total Hit (2770) |
You can quickly determine how many lines of text are contained in a multiline TextBox control whose Scrollbars property is set to 3-Both - that is, when the TextBox behaves like a programmer's editor - by counting the number of CR-LF characters in the Text property. You can do that in just one line
....Read More |
Rating
|
|
|
Topmost windows
|
Total Hit (2021) |
This is an evergreen, but it's still popular. It is very easy to create a window that always stays on top of the others, thanks to the SetWindowPos API function. Here is a general procedure that can be called to make a form the topmost window and to revert to the normal status. This capability is ex
....Read More |
Rating
|
|
|
Undo changes in a TextBox control
|
Total Hit (3812) |
The TextBox control supports the capability to undo changes, but there is no property or method that exposes this feature. You can achieve the same goal with a a set of messages.
«Code LangId=1»
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _
hwnd As Long,
....Read More |
Rating
|
|
|
Unload a form with shrinking effect
|
Total Hit (1583) |
A simple way to add some pizazz to your forms is by minimizing them and then closing them, instead of making them disapper istantaneously. Just try this code:
«Code LangId=1»
Private Sub Form_Unload(Cancel As Integer)
Me.WindowState = vbMinimized
End Sub
«/Code»
....Read More |
Rating
|
|
|
Use a ListBox as a poor man's grid
|
Total Hit (2172) |
You can easily create columns of data in a ListBox control by setting its tab stop at appropriate positions. This way, you can use a ListBox control as a sort of grid control with (very) limited functionality, but without using third-party controls.
You can set ListBox's tab stop by sending the
....Read More |
Rating
|
|
|
Use DataChanged with unbound controls
|
Total Hit (1935) |
When you need to learn if a field has been changed by the user, you have at least three options:
you can use a global boolean variable and set it in the control's Change event, or
you can save the original value in global variable to compare it against the current value
you can use the EM_GE
....Read More |
Rating
|
|
|
Use Refresh, not DoEvents
|
Total Hit (1904) |
The DoEvent statement should be used only to give other portions of your program to be reactive to the end user's actions. Instead, many VB developers use it to force a repaint of a form or some of its controls. This means adding a lot of overhead to your code, which you can avoid by using the Form'
....Read More |
Rating
|
|
|
UseMnemonics property for bound Label controls
|
Total Hit (2189) |
If your forms contain Label controls used to display data from a database, set their UseMnemonic property to False. In fact, if you leave this property to its default value (True), ampersand "&" characters won't be showed correctly, and the Label will mistakenly react to hotkeys.
....Read More |
Rating
|
|
|
Using Enter to move to next field
|
Total Hit (1604) |
Most MsDos programs used the Enter key to move from one field to the next one. You can easily simulate this behavior by setting the form's KeyPreview property to True and writing this code in the Form_KeyPress event:
«Code LangId=1»
Sub Form_KeyPress (KeyAscii As Integer)
If KeyAscii = 13 Th
....Read More |
Rating
|
|
|
AlwaysOnTheTop - Move a form on top of all other windows
|
Total Hit (2181) |
«Code LangId=1»
Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, _
ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, _
ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_SHOWW
....Read More |
Rating
|
|
|