Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Create colorful Command Buttons

Total Hit ( 4274)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


The VB CommandButton control supports neither the ForeColor nor the BackColor properties. If you want to create colorful buttons without resorting to 3rd party controls, you can use an OptionButton control with the Style property set to 1-Graphical. Such OptionButton controls appear to be very similar to regular CommandButton controls, but they let you modify their foreground and background colors. To have them behave as CommandButton controls you only have to add the following line of code in their Click event procedure:

Click here to copy the following block
Private Sub Option1_Click()
  ' reset the value to False, so that the
  ' control doesn't appear to be depressed
  Option1.Value = False
  ' here you insert the code that you want
  ' to execute when the button is clicked
  ' ....
End Sub

One problem with this simple approach is that the border of this "fake" CommandButton control doesn't change its appearance when the control has the focus. Another problem is that you can't easily make this button the default button on the form, unless you trap the Enter key with a form-level keyboard handler (that is, you set KeyPreview = True).
You can solve both problems using a real CommandButton control and place it "behind" the colorful OptionButton control. This will provide the desidered border (without having to draw it yourself) and if the Default property of such a button is set to Default, it will work as expected.

The following code excerpt shows how you can do the trick. It assumes that Option1 is the colorful OptionButton control, and Command1 is the hidden CommandButton control:

Click here to copy the following block
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As _
  Integer

Private Sub Form_Load()
  With Option1
    ' move the CommandButton control under the OptionButton control,
    Command1.Move .Left, .Top, .Width, .Height
    ' but shrink the latter control, so that the border is visible
    .Move .Left + ScaleX(1, vbPixels, ScaleMode), .Top + ScaleY(1, vbPixels, _
      ScaleMode), .Width - 2 * ScaleX(1, vbPixels, ScaleMode), _
      .Height - 2 * ScaleY(1, vbPixels, ScaleMode)
    ' ensure that the OptionButton is on top
    .ZOrder
    ' the user can't tab to the OptionButton
    .TabStop = False
  End With
End Sub

Private Sub Option1_Click()
  If GetAsyncKeyState(vbKeyLButton) = 0 And Option1.Value Then
    ' if the optionbutton is activated using a hotkey
    ' or the user clicked on it and then released the mouse
    ' we must reset its appearance
    Option1.Value = False
    ' and then fire the Click event of the real button
    Command1.SetFocus
    Command1.Value = True
  End If
End Sub

Private Sub Option1_MouseUp(Button As Integer, Shift As Integer, X As Single, _
  Y As Single)
  ' fire the Click event again when the mouse button is released
  Option1_Click
End Sub

Private Sub Command1_Click()
  ' place here the code that you want to execute
  ' when the button is clicked
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.