vb.net radiobutton progromatically设置'Checked'而不是'Click'事件

时间:2017-04-15 10:44:04

标签: vb.net visual-studio-2015

我正在使用来自注册表

的存储设置中的值加载表单

通常:             如果sFormat = TG_ReportFormatDft那么                 RadioButton1.Checked = True                 ........             其他                 RadioButton2.Checked = True                 ..........             结束如果

TG_ReportFormatDft是一个字符串常量,没有任何意义。单选按钮分组正确,手动单击行为正确。

在程序的后面,我检查是否有手动更改:

'Follow what the user is doing
Private Sub RadioButton1_Click(sender As Object, e As EventArgs) Handles RadioButton1.Click
    msReports_Format2 = TG_ReportFormatDft
    DoButtons(True)
End Sub

Private Sub RadioButton2_Click(sender As Object, e As EventArgs) Handles RadioButton2.Click
    msReports_Format2 = TG_ReportFormatAlt
    DoButtons(True)
End Sub

想象一下,在没有鼠标点击的情况下触发这些下游子程序时我会感到惊讶。它看起来像是:        RadioButton1.Checked = True ---触发鼠标点击事件。

我可以理解“On Change”事件但是没有发生鼠标点击。

如何防止此“点击”事件传播?

1 个答案:

答案 0 :(得分:0)

声明一个类级别的布尔变量" ClickedFromCode"。现在,当您从代码中设置值时,将布尔值设置为true。见下面的示例代码。

Private ClickedFromCode As Boolean

Private Sub Intialize

    ClickedFromCode = True
    If sFormat = TG_ReportFormatDft Then 
        RadioButton1.Checked = True 
    Else 
        RadioButton2.Checked = True
    End If
    ClickedFromCode = False

End Sub

Private Sub RadioButton1_Click(sender As Object, e As EventArgs) Handles RadioButton1.Click
    If ClickedFromCode Then 
        Return
    End If
    msReports_Format2 = TG_ReportFormatDft
    DoButtons(True)
End Sub

Private Sub RadioButton2_Click(sender As Object, e As EventArgs) Handles RadioButton2.Click
    If ClickedFromCode Then 
        Return
    End If
    msReports_Format2 = TG_ReportFormatAlt
    DoButtons(True)
End Sub