Get the most out of Visual Basic with these tips and tricks!


Controls

This article shows you how to implement the standard editing commands (including Undo) using Windows messages and the SendMessage API function.

The SelectText procedure highlights the text within a text box. This article also shows you how to make text boxes behave properly.

Forms

Give your document window the 3D look by adding a client edge (a sunken border). This article uses the Windows API to change window styles.

The CenterForm procedure moves a form to the center of the desktop, taking into account the area covered by the taskbar.

A topmost window is visible even when another window has the focus. This article shows you how to use the SetWindowPos API function to create a form that is "Always on Top."

More tips

Graphics

If you need to determine the amount of red, green, or blue in a color value, use the GetRed, GetGreen, and GetBlue functions.

Math

Split words and double words and put them back together with the LoByte, LoWord, HiByte, HiWord, MakeWord, and MakeDWord functions.

The LongToUInt and UIntToLong functions let you manage integers between 32768 and 65535.

Keyboard and Mouse

Don't let the Shortcut list provided by the Menu Editor constrain your choice of shortcut keys.

This article presents two versions of the handy Hourglass procedure, which shows and hides the hourglass mouse pointer.

If you have a caption-less window, let the user move it by dragging its client area instead. This article demonstrates how to use two API functions to accomplish that.

Shell Services

The standard Browse for Folder dialog box lets the user select a folder from an Explorer-like hierarchical listing of folders. This article demonstrates how to use the Shell API to display the dialog box.

Strings

The ZTrim procedure, useful when working with strings returned from API calls, truncates a string at its first null (ASCII 0) character.

Windows

Would you like to extend the event-handling capabilities of Visual Basic? If so, start with this overview of Windows messages. Learn how programs receive user input and how they communicate with the operating system.




Recent Questions

Q: How do I send Alt+D to another application to open that menu?

First, activate the other application. If you know the title of the application window, call AppActivate. If you have a handle to the application window, call the SetForegroundWindow API function:

Declare Function SetForegroundWindow Lib "user32" ( _
    ByVal hWnd As Long) As Long

Then call SendKeys:

SetForegroundWindow hAppWnd  ' or AppActivate "AppTitle"
SendKeys "%D"  ' Send Alt+D

Q: How do I get the label and serial number of a hard disk or floppy disk?

A: Call the GetVolumeInformation API function:

Declare Function GetVolumeInformation _
    Lib "kernel32" Alias "GetVolumeInformationA" ( _
        ByVal RootPathName As String, _
        ByVal VolumeNameBuffer As String, _
        ByVal VolumeNameSize As Long, _
        ByRef VolumeSerialNumber As Long, _
        ByRef MaximumComponentLength As Long, _
        ByRef FileSystemFlags As Long, _
        ByVal FileSystemNameBuffer As String, _
        ByVal FileSystemNameSize As Long _
    ) As Long

The following code excerpt outputs the volume label and serial number of drive C: to the Immediate window:

Dim VolumeName As String, SerialNumber As Long, Unused As Long
VolumeName = String$(64, 0)

If GetVolumeInformation("C:\", VolumeName, Len(VolumeName), _
        SerialNumber, Unused, Unused, vbNullString, 0) <> 0 Then
    Debug.Print "Volume label:  "; ZTrim(VolumeName)
    Debug.Print "Serial number: "; Hex$(SerialNumber)
Else
    Debug.Print "Error: " & Err.LastDllError
End If

See Trimming Null Characters From Strings for the definition of the ZTrim function.

Q: How do I add an F9 shortcut for a command button?

A: Set the form's KeyPreview property to True, and add the following code to the Form_KeyDown event:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyF9 Then
        Command1.Value = True
    End If
End Sub
1