RadioButton上的Click事件从一个移动到另一个时阻止

时间:2016-11-29 14:39:26

标签: vb.net radio-button

我有一个Form with Panel contains 5 RadioButton

我只处理了所有RadioButtons的Click事件。

当我使用箭头键从RadioButton移动到另一个时,每次移动都会引发Click事件。

是否可以仅选择RadioButton并使用SpaceBar单击它?

2 个答案:

答案 0 :(得分:1)

我会使用父表单的Form.KeyDown事件并执行以下操作:

Private Sub Form1_KeyPress(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    REM Check if space is the button pressed
    If e.KeyCode = Keys.Space Then
        REM See if a Radio Button has focus to know which was selected
        If RadioButton1.ContainsFocus Then
            RunMyCode(RadioButton1)
        ElseIf RadioButton2.ContainsFocus Then
            RunMyCode(RadioButton2)
        End If
    End If
End Sub

''' <summary>
''' Method which executes the code you want run when a radio button is selected
''' </summary>
''' <param name="rdButtona"></param>
Private Sub RunMyCode(ByRef rdButtona As RadioButton)
    REM Check which radio button was selected and execute the apprpriate code
    If rdButtona.Equals(RadioButton1) Then
        MsgBox("Radio Button 1")
    ElseIf rdButtona.Equals(RadioButton2) Then
        MsgBox("Radio Button 2")
    End If
End Sub

要阻止检查RadioButton,请处理RadioButton.Click事件,然后使用RadioButton取消选中radioButton1.checked = false。如果您处理RadioButton.CheckChanged事件,radioButton1.checked = false将引发另一个我们不需要的CheckChanged事件。我也会在那里做些什么来显示选择了哪个单选按钮。在我的示例中,我更改了文本颜色,但您可以执行任何操作。

   Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
    RadioButton2.Checked = False
    ResetColors()
    RadioButton2.ForeColor = Color.DarkRed
End Sub

Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
    RadioButton3.Checked = False
    ResetColors()
    RadioButton3.ForeColor = Color.DarkRed
End Sub

Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
    RadioButton1.Checked = False
    ResetColors()
    RadioButton1.ForeColor = Color.DarkRed
End Sub

Private Sub ResetColors()
    RadioButton2.ForeColor = Color.Black
    RadioButton1.ForeColor = Color.Black
    RadioButton3.ForeColor = Color.Black
End Sub

当然代码可以简化,但这是一般的想法。

答案 1 :(得分:0)

以下代码运行RadioButtons仅在鼠标单击时(或在空格键上按下)单击事件,而不是按箭头键。 它还关注使用箭头键选择的RadioButton

Dim HandleRBtnClick As Boolean = True

Private Sub RadioButtons_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles RadioButton1.Click, RadioButton2.Click, RadioButton3.Click, RadioButton4.Click
    If Not HandleRBtnClick Then
        With CType(sender, RadioButton)
            .Checked = False
            .Focus()
        End With
        Exit Sub
    End If
    'Code for RadioButton Checked (on mouse click or spacebar pressed)
    Me.TextBox1.Text = CType(sender, RadioButton).Name
End Sub

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
    If keyData = Keys.Left OrElse
        keyData = Keys.Right OrElse
        keyData = Keys.Up OrElse
        keyData = Keys.Down Then

        HandleRBtnClick = False
    Else
        HandleRBtnClick = True
    End If
    Return MyBase.ProcessCmdKey(msg, keyData)
End Function