-
Jul 26th, 2024, 07:15 PM
#1
Thread Starter
Frenzied Member
[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.
-
Jul 26th, 2024, 08:53 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 27th, 2024, 06:50 PM
#3
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 29th, 2024, 01:15 AM
#4
Re: How to have a third state with 2 radio buttons or check boxes.
Originally Posted by .paul.
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.
-
Jul 29th, 2024, 05:50 PM
#5
Thread Starter
Frenzied Member
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.
-
Jul 29th, 2024, 06:08 PM
#6
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…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 29th, 2024, 06:47 PM
#7
Thread Starter
Frenzied Member
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.
-
Jul 29th, 2024, 07:04 PM
#8
Re: How to have a third state with 2 radio buttons or check boxes.
Originally Posted by cory_jackson
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 29th, 2024, 10:55 PM
#9
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.
-
Jul 30th, 2024, 10:28 AM
#10
Thread Starter
Frenzied Member
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'. :-)
-
Jul 30th, 2024, 11:00 AM
#11
Re: How to have a third state with 2 radio buttons or check boxes.
Originally Posted by cory_jackson
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
|