Code Snippets

  

Visual Basic Source Code


Welcome to Dream.In.Code
Become a VB Expert!

Join 137,192 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,329 people online right now. Registration is fast and FREE... Join Now!





VB6 sticky button, VB6 toggle button

Make a sticky button, i.e. a button that toggles.

Submitted By: jens
Actions:
Rating:
Views: 477

Language: Visual Basic

Last Modified: October 9, 2008
Instructions: Instructions in the code.

Snippet


  1. Form1 code
  2. [code]
  3. '===================================================
  4. ' Sticky button (toggle) example.
  5. ' You need one form with a button named "btnSticky".
  6. ' You need one module "Module1" (code as second part)
  7. ' This is the code for "Form1"
  8. '---------------------------------------------------
  9.  
  10. Option Explicit
  11.  
  12. Private Sub btnSticky_Click()
  13. ' If you ONLY need the button to keep state and the state to be available
  14. ' in this form you can declare it here. Otherwise declare it in a global
  15. ' part of your program (as done in this example).
  16. 'Static Downstate As Long
  17.  
  18.    'Whatever state the button has, it will now become the opposite.
  19.    Downstate = Not Downstate
  20.    'I change what the button reads and the color depending on state.
  21.    If Downstate Then
  22.       btnSticky.Caption = "btnSticky On"
  23.       btnSticky.BackColor = &HFF&
  24.    Else
  25.       btnSticky.Caption = "btnSticky Off"
  26.       btnSticky.BackColor = &H8000000F
  27.    End If
  28.    Call SendMessageBynum(btnSticky.hwnd, BM_SETSTATE, Downstate, 0)
  29. End Sub
  30. [/code]
  31.  
  32. Module1 code
  33. [code]
  34. '===================================================
  35. ' Sticky button (toggle) example.
  36. ' This is the code for "Module1"
  37. '---------------------------------------------------
  38.  
  39. Option Explicit
  40.  
  41. ' To create a "sticky" button, you need to send a message to the Command Button
  42. ' using the SendMessageBynum Windows API function.
  43. ' Without the declaration and the public const the button won't work.
  44.  
  45. ' This must be in a global part of your program
  46. Declare Function SendMessageBynum Lib "user32" Alias "SendMessageA" _
  47.     (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
  48.     ByVal lParam As Long) As Long
  49.  
  50. ' This must be in a global part of your program
  51. Public Const BM_SETSTATE = &HF3
  52.  
  53. ' If this isn't in a global part of your program your button will lose
  54. ' state as soon as you leave the form where it resides.
  55. ' This way you can test the buttons state everywhere in your program too.
  56. Public Downstate As Long
  57. [/code]

Copy & Paste


Comments


jens 2008-10-09 08:41:32

Sorry, I got the [code]-stuff messed up. /Jens


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month