|
|
|
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: |
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 Command1.Move .Left, .Top, .Width, .Height .Move .Left + ScaleX(1, vbPixels, ScaleMode), .Top + ScaleY(1, vbPixels, _ ScaleMode), .Width - 2 * ScaleX(1, vbPixels, ScaleMode), _ .Height - 2 * ScaleY(1, vbPixels, ScaleMode) .ZOrder .TabStop = False End With End Sub
Private Sub Option1_Click() If GetAsyncKeyState(vbKeyLButton) = 0 And Option1.Value Then Option1.Value = False 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) Option1_Click End Sub
Private Sub Command1_Click() 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 ) |
|
|