Results 1 to 12 of 12

Thread: [RESOLVED] how to make two button toggle - one in other out

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Resolved [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.

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    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.

  4. #4
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: how to make two button toggle - one in other out

    Quote Originally Posted by jmsrickland View Post
    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.

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    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

  6. #6
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: how to make two button toggle - one in other out

    Quote Originally Posted by MarkT View Post
    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)

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  8. #8
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: how to make two button toggle - one in other out

    I'm not sure what you are saying. Can you post an example?

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: how to make two button toggle - one in other out

    Example of what?


    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.

  10. #10
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    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)

  11. #11
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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.

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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
  •  



Click Here to Expand Forum to Full Width