Results 1 to 11 of 11

Thread: [RESOLVED] How to have a third state with 2 radio buttons or check boxes.

  1. #1

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,114

    Resolved [RESOLVED] How to have a third state with 2 radio buttons or check boxes.

    I have a form with two text boxes. Basically to contain text to be found or not found as a condition. To the left of each I installed radio buttons. Once clicked this grants two states. I'd like a third and that's to have neither selected. EG I click on the button that's already checked and now neither are checked. But it doesn't work that way. Checking one that is checked results in checked. Is there a parameter to change this behavior? Or perhaps a logic trick? I've tried making checkboxes work like that also, but I can't figure out a way as changing one triggers the event handler.
    It's not important, I can always add another control for the third state. I just thought it a fun challenge and opportunity to learn.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,656

    Re: How to have a third state with 2 radio buttons or check boxes.

    Try this...

    Code:
    Private isSelected As RadioButton
    
    Private Sub RadioButtons_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RadioButton2.MouseUp, RadioButton1.MouseUp
        Dim rb As RadioButton = DirectCast(sender, RadioButton)
        If isSelected Is rb Then rb.Checked = False
    End Sub
    
    Private Sub RadioButtons_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RadioButton2.MouseDown, RadioButton1.MouseDown
        Dim rb As RadioButton = DirectCast(sender, RadioButton)
        If rb.Checked Then isSelected = rb Else isSelected = Nothing
    End Sub

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,656

    Re: How to have a third state with 2 radio buttons or check boxes.

    To achieve the same functionality with two checkboxes...

    Code:
    Private Sub CheckBoxes_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles CheckBox2.MouseDown, CheckBox1.MouseDown
        Dim cb As CheckBox = DirectCast(sender, CheckBox)
        Dim group() As CheckBox = {CheckBox1, CheckBox2}
        Dim other As CheckBox = group.First(Function(chkbox) Not chkbox Is cb)
        other.Checked = False
    End Sub
    The radiobuttons are slightly more complicated, because of the default behaviour, but it's just a case of knowing the order of events, so as to change things.
    The checkboxes are easier to manipulate...
    Last edited by .paul.; Jul 27th, 2024 at 06:53 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,663

    Re: How to have a third state with 2 radio buttons or check boxes.

    Quote Originally Posted by .paul. View Post
    To achieve the same functionality with two checkboxes...

    Code:
    Private Sub CheckBoxes_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles CheckBox2.MouseDown, CheckBox1.MouseDown
        Dim cb As CheckBox = DirectCast(sender, CheckBox)
        Dim group() As CheckBox = {CheckBox1, CheckBox2}
        Dim other As CheckBox = group.First(Function(chkbox) Not chkbox Is cb)
        other.Checked = False
    End Sub
    The radiobuttons are slightly more complicated, because of the default behaviour, but it's just a case of knowing the order of events, so as to change things.
    The checkboxes are easier to manipulate...
    I think I'd tend to handle the CheckedChanged event and modify the code slightly for CheckBoxes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,114

    Re: How to have a third state with 2 radio buttons or check boxes.

    Thank you much. My problem was I was barking up the wrong tree on which event to use. Makes total sense now.
    I like the checkbox one better. I did have to change CheckBox to Windows.Forms.CheckBox in all places because it complained "BC30311: Value of type ‘CheckBox’ cannot be converted to ‘VisualStyleElement.Button.CheckBox’.". I don't understand, but it's not important. It works.

    John that's a good idea too. I played with that as well.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,656

    Re: How to have a third state with 2 radio buttons or check boxes.

    Is this a WPF App? You’re asking in the winforms forum…

  7. #7

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,114

    Re: How to have a third state with 2 radio buttons or check boxes.

    I don't know how to do WPF. This is a Windows Forms Application. .NET Framework 4.5.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,656

    Re: How to have a third state with 2 radio buttons or check boxes.

    Quote Originally Posted by cory_jackson View Post
    I don't know how to do WPF. This is a Windows Forms Application. .NET Framework 4.5.
    Ok. I was just wondering. Usually, System.Windows.Forms is imported by default

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,663

    Re: How to have a third state with 2 radio buttons or check boxes.

    It sounds like you have imported the System.Windows.Forms.VisualStyles namespace or a member type from it somewhere. VisualStyleElement.Button.CheckBox is a class in that namespace. System.Windows.Forms would be imported at the project level but if you have an import at the file level that conflicts with that, the one with the more narrow scope will win. Check the top of your code file for such an import. If it exists, it presumably exists for a reason, so you're going to have to alias and/or qualify something, most likely.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,114

    Re: How to have a third state with 2 radio buttons or check boxes.

    Imports System.Windows.Forms.VisualStyles.VisualStyleElement.Button
    This was my imports section and not System.Windows.Forms. I have no idea why, but I changed it.
    I remember being tricked once when starting a new project and selecting the wrong type. Maybe I changed it after the form was created? Dunno.
    Good catch both of you. Thanks!

    Say... If you could find a forum member named Jones to work with, you could be John Paul Jones. That would 'rock'. :-)

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,656

    Re: How to have a third state with 2 radio buttons or check boxes.

    Quote Originally Posted by cory_jackson View Post
    Imports System.Windows.Forms.VisualStyles.VisualStyleElement.Button
    This was my imports section and not System.Windows.Forms. I have no idea why, but I changed it.
    I remember being tricked once when starting a new project and selecting the wrong type. Maybe I changed it after the form was created? Dunno.
    Good catch both of you. Thanks!

    Say... If you could find a forum member named Jones to work with, you could be John Paul Jones. That would 'rock'. :-)
    Indeed

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