none
Detect radio button change ONLY on click? RRS feed

  • Question

  • I've encountered a minor annoyance. I'm trying to detect when a radio button state is changed BY THE USER.

    Problem is, it detects a Click event even when the state is already True. And it detects a "ChangedState" event when my program loads (even though it is Disabled by default.)

    Is there a simple method to detect when a radio button's state is changed, and ONLY by the user?

    TIA

    Thursday, September 21, 2017 11:34 AM

Answers

  • I've encountered a minor annoyance. I'm trying to detect when a radio button state is changed BY THE USER.

    Problem is, it detects a Click event even when the state is already True. And it detects a "ChangedState" event when my program loads (even though it is Disabled by default.)

    Is there a simple method to detect when a radio button's state is changed, and ONLY by the user?

    TIA

    Hi

    I have had that type of annoyance before, for all sorts of controls.

    The only way I have found to combat it is to use a flag to indicate if the event handlers need to be skipped or not.

    Here is an example to show it -the variable 'skip' is used.

    ' Form1 with GrouoBox containing
    ' RadioButton1, RadioButton2 and
    ' RadioButton3
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim skip As Boolean = True
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            RadioButton1.Text = "False"
            RadioButton2.Text = "False"
            RadioButton3.Text = "False"
        End Sub
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
            skip = False
        End Sub
        Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
            If skip Then Exit Sub
            Dim rb As RadioButton = DirectCast(sender, RadioButton)
            RB_Click(rb.Name)
        End Sub
        Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
            If skip Then Exit Sub
            Dim rb As RadioButton = DirectCast(sender, RadioButton)
            RB_Click(rb.Name)
        End Sub
        Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
            If skip Then Exit Sub
            Dim rb As RadioButton = DirectCast(sender, RadioButton)
            RB_Click(rb.Name)
        End Sub
        Sub RB_Click(s As String)
            RadioButton1.Text = (s = "RadioButton1").ToString
            RadioButton2.Text = (s = "RadioButton2").ToString
            RadioButton3.Text = (s = "RadioButton3").ToString
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, September 21, 2017 12:12 PM
    • Marked as answer by Mugsy_in_Houston Thursday, September 21, 2017 12:58 PM
    Thursday, September 21, 2017 12:11 PM

All replies

  • I've encountered a minor annoyance. I'm trying to detect when a radio button state is changed BY THE USER.

    Problem is, it detects a Click event even when the state is already True. And it detects a "ChangedState" event when my program loads (even though it is Disabled by default.)

    Is there a simple method to detect when a radio button's state is changed, and ONLY by the user?

    TIA

    Hi

    I have had that type of annoyance before, for all sorts of controls.

    The only way I have found to combat it is to use a flag to indicate if the event handlers need to be skipped or not.

    Here is an example to show it -the variable 'skip' is used.

    ' Form1 with GrouoBox containing
    ' RadioButton1, RadioButton2 and
    ' RadioButton3
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim skip As Boolean = True
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            RadioButton1.Text = "False"
            RadioButton2.Text = "False"
            RadioButton3.Text = "False"
        End Sub
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
            skip = False
        End Sub
        Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
            If skip Then Exit Sub
            Dim rb As RadioButton = DirectCast(sender, RadioButton)
            RB_Click(rb.Name)
        End Sub
        Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
            If skip Then Exit Sub
            Dim rb As RadioButton = DirectCast(sender, RadioButton)
            RB_Click(rb.Name)
        End Sub
        Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
            If skip Then Exit Sub
            Dim rb As RadioButton = DirectCast(sender, RadioButton)
            RB_Click(rb.Name)
        End Sub
        Sub RB_Click(s As String)
            RadioButton1.Text = (s = "RadioButton1").ToString
            RadioButton2.Text = (s = "RadioButton2").ToString
            RadioButton3.Text = (s = "RadioButton3").ToString
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, September 21, 2017 12:12 PM
    • Marked as answer by Mugsy_in_Houston Thursday, September 21, 2017 12:58 PM
    Thursday, September 21, 2017 12:11 PM
  • I've encountered a minor annoyance. I'm trying to detect when a radio button state is changed BY THE USER.

    Problem is, it detects a Click event even when the state is already True. And it detects a "ChangedState" event when my program loads (even though it is Disabled by default.)

    Is there a simple method to detect when a radio button's state is changed, and ONLY by the user?

    TIA

    Here's one way:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Enum UserSelection
            NotSet
            Blue
            Green
            Red
        End Enum
    
        Private _userSelection As UserSelection
        Private _isStarting As Boolean = True
    
        Private Sub _
            Form1_Activated(sender As Object, _
                            e As System.EventArgs) _
                            Handles Me.Activated
    
            _isStarting = False
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                               Handles MyBase.Load
    
            ' Initialization here (if applicable)...
    
        End Sub
    
        Private Sub _
            Form1_Shown(sender As Object, _
                        e As System.EventArgs) _
                        Handles Me.Shown
    
            rb_Blue.Checked = False
            rb_Green.Checked = False
            rb_Red.Checked = False
    
        End Sub
    
        Private Sub _
            RadioButtonClick(sender As Object, _
                             e As System.EventArgs) _
                             Handles rb_Blue.Click, _
                                     rb_Green.Click, _
                                     rb_Red.Click
    
            If Not _isStarting Then
                Dim rb As RadioButton = DirectCast(sender, RadioButton)
    
                Select Case rb.Name
                    Case "rb_Blue"
                        If Not _userSelection = UserSelection.Blue Then
                            _userSelection = UserSelection.Blue
                            MessageBox.Show("The state was changed.")
                        End If
    
                    Case "rb_Green"
                        If Not _userSelection = UserSelection.Green Then
                            _userSelection = UserSelection.Green
                            MessageBox.Show("The state was changed.")
                        End If
    
                    Case "rb_Red"
                        If Not _userSelection = UserSelection.Red Then
                            _userSelection = UserSelection.Red
                            MessageBox.Show("The state was changed.")
                        End If
                End Select
            End If
    
        End Sub
    
    End Class



    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Thursday, September 21, 2017 12:24 PM ...modified the code: The names were confusing.
    Thursday, September 21, 2017 12:16 PM
  • I like to use the tag property to cancel the event like this:

    Public Class Form3 Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    'set tag true to cancel the event RadioButton1.Tag = True

    'now set the control RadioButton1.Checked = True RadioButton1.Tag = False End Sub Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles _ RadioButton1.CheckedChanged, RadioButton2.CheckedChanged If Not CBool(RadioButton1.Tag) Then MsgBox("Here I am") End Sub End Class


    Thursday, September 21, 2017 12:38 PM
  • Here is an example to show it -the variable 'skip' is used.
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
            skip = False
        End Sub
        Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
            If skip Then Exit Sub
    

    This appears to work (I'll follow-up if the problem returns.)

    I hadn't used the "Form.Shown" property before, so I'm not sure when it is triggered, but so far, so good. I was looking for a "simple" solution and this seems to do it.

    Thx.

    Thursday, September 21, 2017 1:01 PM
  • Option Strict On
    Option Explicit On Option Infer Off Public Class Form1 Private Enum UserSelection NotSet Blue Green Red End Enum Private _userSelection As UserSelection Private _isStarting As Boolean = True

    Thanks for the reply, though "leeshay"s solution seems to work with less code.

    Does your solution handle situations the other method does not that I haven't encountered yet?

    Thx.


    Thursday, September 21, 2017 1:06 PM
  • I like to use the tag property to cancel the event like this:

    Thx. This looks like a simple solution too. I'll refer back to it if the earlier solution provided doesn't do what I need consistently.
    Thursday, September 21, 2017 1:09 PM

  • Thanks for the reply, though "leeshay"s solution seems to work with less code.

    Does your solution handle situations the other method does not that I haven't encountered yet?

    Thx.


    You never have to refer back to the radio buttons again. A single class-scoped variable (an integer) knows the status by virtue of the enumeration.

    But less code is always better ...

    ;-)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, September 21, 2017 1:22 PM