Almost everything in Windows is a window and you can call the Windows API (the Application Program Interface) to manipulate forms and controls beyond the scope of VB itself.
The key to many of these tricks is the object's handle. This is a long integer held in the hWnd property of the form or control. You won't see this property in the Property Window at design time. It�s a read-only value that's assigned by Windows at runtime. It's also a transient property that can change as Windows juggles resources so you should never store the value for later use.
Once you know the handle of the object that you want to manipulate, you use the SendMessage function to send a message to that object telling it what to do. As an example, you can drop or hide the list of a combo box, scroll a list box, or undo the changes in a text box.
SendMessage is a function of the Windows API and you have to declare it before you can use it. Put the following code into the General Declarations of a code module:
Declare Function SendMessage _
Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Long) _
As Long
If you don't have a code module in your project, just add one to hold this declaration. It does not have to be set as the project's StartUp object, it just has to exist as part of the project. This single declaration will let you use the SendMessage function anywhere in the project.
The user can already undo changes by pressing CTRL-Z but that only works until the control loses focus. This API call is more selective, you can use it undo changes in a particular textbox and retain more recent changes to another text box. The call is simple:
Const EM_UNDO = &HC7
Call SendMessage(Text1.hwnd, _
EM_UNDO, 0, 0)
You can save the user a mouse-click by dropping the list of a combo box as soon as it receives focus. Put the following code in the GotFocus event of the combo box:
Const CB_SHOWDROPDOWN = &H14F
Call SendMessage(Combo1.hwnd, CB_SHOWDROPDOWN, 1, ByVal 0)
Many API calls are simple once you know their details, the problem is finding the details.
Start with MSDN and look in Platform SDK, User Interface Services, Windows User Interface, Controls. This lists the generic types of controls so you look under Edit Controls for details of Text Boxes and under Buttons for Command Buttons, Option Buttons, and Check Boxes:
The Messages category for each control lists all the messages in alphabetical order.
Each type of control has many, many messages. All you can do is browse through them until you see something useful.
There are two types of message. One type is sent by the control, for example the combo box sends a CBN_DBLCLK to the form when the box receives a double-click. We are looking for the other type, messages that are sent to the control to modify its behavior or appearance. You have to read the Help to find out which is which. Look for messages that use SendMessage.
Help for each message will be in C++ syntax. That's not too difficult to translate but the constants quoted are all C++ constants and aren't mentioned in VB at all. Use the API Text Viewer to find them in Win32API.txt. This should be in ...\Visual Studio\Common\Tools\WinAPI\. The file is a long list of declarations such as:
Public Const EM_UNDO = &HC7
You can search the file for the constant required by the API call then cut and paste its definition into your program.
The Viewer is part of Visual Studio and may not be installed with all VB packages. Use NotePad if you don't have the Viewer.
One final warning. The API is outside the protective environment of Visual Basic. It gives you many new features but does not protect you from your mistakes.