|
-
May 16th, 2013, 10:43 AM
#1
[RESOLVED] how to make two button toggle - one in other out
I need two commands buttons that act like a one or the other (kind of like two radio buttons where only one is ticked at a time). If one button is pressed it stays pressed and the other becomes unpressed. The button that is pressed in stays pressed until the other button is pressed then that button stays pressed and the first button becomes unpressed.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
May 16th, 2013, 10:48 AM
#2
Re: how to make two button toggle - one in other out
OK, I see if I use two radio buttons and set their style to graphical it gives me what I need. Is there any way to show the pressed in button without the inner rectangle?
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
May 16th, 2013, 12:59 PM
#3
Re: how to make two button toggle - one in other out
If you want to use command buttons, you can simply change the 'downed' button image on it's click event, and then when you click the OTHER button, set a 'regular' image to the first button.
Here's and example:
Code:
Option Explicit
Dim gCmd1Clicked As Boolean
Dim gCmd2Clicked As Boolean
Private Sub Command1_Click()
If gCmd1Clicked = False Then
gCmd1Clicked = True
gCmd2Clicked = False
Command1.Picture = LoadPicture(App.Path & "\downbtn.jpg")
Command2.Picture = LoadPicture(App.Path & "\upbtn.jpg")
Else
gCmd1Clicked = False
End If
End Sub
Private Sub Command2_Click()
If gCmd2Clicked = False Then
gCmd2Clicked = True
gCmd1Clicked = False
Command2.Picture = LoadPicture(App.Path & "\downbtn.jpg")
Command1.Picture = LoadPicture(App.Path & "\upbtn.jpg")
Else
gCmd2Clicked = False
End If
End Sub
EDIT: You can also set the enabled property to false and/or true on the click events so it cannot even be clicked --- my code allows you to CLICK it, but nother happens--but you do see the btn 'flicker'. Enabled property use can prevent that as well.
Last edited by SamOscarBrown; May 16th, 2013 at 01:04 PM.
-
May 16th, 2013, 01:16 PM
#4
Re: how to make two button toggle - one in other out
 Originally Posted by jmsrickland
OK, I see if I use two radio buttons and set their style to graphical it gives me what I need. Is there any way to show the pressed in button without the inner rectangle?
The easiest way is to add a small borderless picturebox to the form and then in the click event of the option button set focus on the picturebox. You can also use APIs to remove the focus rect from the control but that is a bit more involved.
-
May 16th, 2013, 01:25 PM
#5
Re: how to make two button toggle - one in other out
Or, mod my slightly and get rid of graphical property.....
Code:
Option Explicit
Dim gCmd1Clicked As Boolean
Dim gCmd2Clicked As Boolean
Private Sub Command1_Click()
If gCmd1Clicked = False Then
gCmd1Clicked = True
gCmd2Clicked = False
Command1.Enabled = False
Command2.Enabled = True
' Command1.Picture = LoadPicture(App.Path & "\downbtn.jpg")
' Command2.Picture = LoadPicture(App.Path & "\upbtn.jpg")
Else
gCmd1Clicked = False
' Command1.Enabled = True
End If
End Sub
Private Sub Command2_Click()
If gCmd2Clicked = False Then
gCmd2Clicked = True
gCmd1Clicked = False
Command2.Enabled = False
Command1.Enabled = True
' Command2.Picture = LoadPicture(App.Path & "\downbtn.jpg")
' Command1.Picture = LoadPicture(App.Path & "\upbtn.jpg")
Else
gCmd2Clicked = False
' Command2.Enabled = True
End If
End Sub
-
May 17th, 2013, 02:36 AM
#6
Re: how to make two button toggle - one in other out
 Originally Posted by MarkT
You can also use APIs to remove the focus rect from the control but that is a bit more involved.
Just a few code is required. 
Code:
Private Const WM_KILLFOCUS As Long = &H8
Private Declare Function SendMessageW Lib "user32.dll" (ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Sub Option1_Click()
SendMessageW Option1.hWnd, WM_KILLFOCUS, 0&, 0&
End Sub
Private Sub Option2_Click()
SendMessageW Option2.hWnd, WM_KILLFOCUS, 0&, 0&
End Sub
@ jmsrickland
If you want your graphical buttons to be themed, see this thread.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
May 17th, 2013, 11:21 AM
#7
Re: how to make two button toggle - one in other out
The methods used, whether it's using the API or putting focus on a picturebox, needs to be placed in the MouseDown events rather than the Click events. If in the Click events it works for that time only. If button is down and user clicks on it the focus returns to the button and shows the rectangle again. Putting it in the MouseDown event avoids this.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
May 17th, 2013, 11:30 AM
#8
Re: how to make two button toggle - one in other out
I'm not sure what you are saying. Can you post an example?
-
May 17th, 2013, 11:53 AM
#9
Re: how to make two button toggle - one in other out
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
May 17th, 2013, 12:11 PM
#10
Re: how to make two button toggle - one in other out
The GotFocus event is another suitable event.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
May 17th, 2013, 12:18 PM
#11
Re: how to make two button toggle - one in other out
I was just having trouble grasping what you were saying. After eating lunch and looking what you wrote again, I understand now. Like Bonnie mentioned, GotFocus would be a good place to put the code. This would also prevent the focus rect if you tab into the control.
-
May 17th, 2013, 01:07 PM
#12
Re: how to make two button toggle - one in other out
Yes, the GotFocus is the best way to go.
Thanks, Bonnie. I'm using your API suggestion and the GotFocus event
Thanks to everyone else; your ideas were also very good
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|