Create a 'sticky' button that toggles in VB6
Takeaway: If you want to use a VB6 Command Button that you can toggle, then learn how to create "sticky" buttons, which are useful for selecting On/Off options as an alternative to a checkbox.
VB6's Command Buttons are widely used for registering user input such as mouse clicks. The normal behavior of these buttons is to appear "raised" normally, then to appear "down" during the click, and return to the "raised" appearance when the mouse button is released.
For most users, this is what they want. However, you may prefer a Command Button that you can toggle. You could click it once and it would stay in the "down" state; click it again and it would return to the "raised" appearance. These types of "sticky" buttons are useful for selecting On/Off options as an alternative to a checkbox.
To create a "sticky" button, you need to send a message to the Command Button using the SendMessageBynum Windows API function. You must put the function declaration in your program, as well as a constant definition:
Public Const BM_SETSTATE = &HF3
Declare Function SendMessageBynum Lib "user32" Alias
"SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long,
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Then it's a simple matter of toggling the button in its Click event procedure. The following code changes the button's caption as well:
Private Sub Command1_Click()
Static Downstate As Long
Downstate = Not Downstate
If Downstate Then
Command1.Caption = "Option On"
Else
Command1.Caption = "Option Off"
End If
Call SendMessageBynum(Command1.hwnd, BM_SETSTATE, Downstate,
0)
End Sub
If you make the Downstate variable global rather than local, code in other parts of the program will be able to check the button's state as needed.
Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!
Print/View all Posts Comments on this article
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
White Papers, Webcasts, and Downloads
- Leveraging SMB ERP for an Economic Recovery ZDNet Times are tough but better days are sure to follow. In the wake of an ... Download Now
- The Impact of Virtualization Software on Operating Environments VMware Today's use of virtualization technology allows IT professionals to ... Download Now
- Five Steps to Determine When to Virtualize YourServers VMware Thinking of virtualizing the servers at your company? Use this step-by-step guide to determine when's the best time to make your big move. Download Now
- Building the Virtualized Enterprise with VMware Iinfrastructure VMware VMware virtualization software has been adopted by over 120,000 enterprise ... Download Now
- Building the Virtualized Enterprise with VMware Infrastructure VMware This paper explains how adopting a virtual infrastructure -- comprised of server, storage, and networking virtualization technologies -- can help your organization build a sustainable competitive ... Download Now
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET

