On BNET: 4 tips for a killer PowerPoint

Create a 'sticky' button that toggles in VB6

Tags: Mice, Microsoft Visual Basic 6.0, Peter Aitken, Downstate, Command Button, Visual Basic Tips Newsletter

  • Save
  • Print
  • Recommend
  • 3

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!

  • Save
  • Print
  • Recommend
  • 3

Print/View all Posts Comments on this article

You can accomplish the same thing with a checkbox, style 'graphical' mscir@... | 10/17/05
Good Job. christog@... | 11/18/05
Reinventing the wheel Blaine Moore | 11/18/05
Why not use a toggle button? - job done! john.foggitt@... | 11/18/05
MS Forms 2.0 cVanDuin@... | 11/18/05
Does not stay olsonmh@... | 11/18/05
Please Help, it doesn't stay! Bithiah | 01/24/06
Keep the button down dennismartin@... | 03/31/06
As Others Already Pointed Out smichels@... | 04/02/06

What do you think?

White Papers, Webcasts, and Downloads

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

Smartphones

advertisement
Click Here