vb.net - 更改事件中的单选按钮检查状态

标签 vb.net events radio-button vb6-migration

我正在将一些遗留代码从 VB6 迁移到 VB.NET,但遇到了一个恼人的问题。当前发生的情况是向用户提供RadionButton 控件来表示层次结构。当他们选择一个值时,代码会验证它是否有效(C 不能是 A 的子级,必须是 B 的子级),如果不是,则将 RadioButton 返回到原始设置.

问题是,当执行此操作的函数返回到事件处理程序时,它会将 RadioButton 状态返回到单击时的状态(用户单击 C,代码将其返回到 B,当函数退出它返回到 C 触发事件,该事件将再次将其转到 B 等,导致无限循环)。有没有办法更改在由 CheckedChanged` 事件调用的函数中选择的 RadioButton 并使其保持不变?

我知道更好的设计是提前禁用无效的 RadioButton 控件,但这就是它的设计方式,我的工作是让它在有限的时间范围内工作,所以我现在坚持糟糕的设计,而不是以后好的设计。

代码:

Private Sub optIndent_2_ClickEvent(sender As Object, e As EventArgs) Handles optIndent_2.CheckedChanged
    optIndent_Click(2, sender.Checked)
End Sub

Private Sub optIndent_3_ClickEvent(sender As Object, e As EventArgs) Handles optIndent_3.CheckedChanged
    optIndent_Click(3, sender.Checked)
    Dim i As Integer = 1
End Sub

Private Sub optIndent_Click(ByRef Index As Short, ByRef value As Short)
    If value = False Then
        Exit Sub
    End If

    If Index = 2 Then
        Exit Sub
    End If
    If Index = 3 Then
        optIndent_2.Checked = True
        Exit Sub
    End If
End Sub

您将看到,当代码退出时,optIndent_Click (3, sender.checked) 值将从 false 转换为 true,该过程将永远重复。

最佳答案

问题在于 ByRef 的使用:

Specifies that an argument is passed in such a way that the called procedure can change the value of a variable underlying the argument in the calling code.

您应该使用 ByVal :

Specifies that an argument is passed in such a way that the called procedure or property cannot change the value of a variable underlying the argument in the calling code.

将参数 value 更改为 ByVal 参数将解决该问题。这将阻止 value 保留其“值”。

我很感激您坚持糟糕的设计,因此这可能不适合该项目范围,但在某些时候您应该考虑转向 Option Strict On :

Restricts implicit data type conversions to only widening conversions, disallows late binding, and disallows implicit typing that results in an Object type.

这会突出显示 ByVal value As Short 是一个问题:

Option Strict On disallows implicit conversions from 'Boolean' to 'Short'.

解决办法是: ByVal 值作为 bool 值

设置 Option Strict On 也会将 sender.Checked 突出显示为错误:

Option Strict On disallows late binding

修复方法是将 sender 转换为 RadioButton,以便您可以直接访问其属性; DirectCast(发送者,RadioButton)。已选中

你的代码看起来像这样:

Private Sub optIndent_2_ClickEvent(sender As Object, e As EventArgs) Handles optIndent_2.CheckedChanged
    optIndent_Click(2, DirectCast(sender, RadioButton).Checked)
End Sub

Private Sub optIndent_3_ClickEvent(sender As Object, e As EventArgs) Handles optIndent_3.CheckedChanged
    optIndent_Click(3, DirectCast(sender, RadioButton).Checked)
End Sub

Private Sub optIndent_Click(ByVal Index As Short, ByVal value As Boolean)
    If value = False Then
        Exit Sub
    End If

    If Index = 2 Then
        Exit Sub
    End If

    If Index = 3 Then
        optIndent_2.Checked = True
        Exit Sub
    End If
End Sub

关于vb.net - 更改事件中的单选按钮检查状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43849332/

相关文章:

c# - 在另一个窗体中显示 Winform 'Form',就好像它是用户控件一样

c# - 如何作为委托(delegate)传递属性?

c# - 数据集中特定列的所有行

java - 将 Drawable 放置在按钮的右侧

python - 不同对象中的复选框和单选按钮同时在所有对象中使用react(不希望的)

c# - 这是 Me.Top 的 .NET Framework 错误吗?

jquery - 单击事件未在 IE 中注册,div 上的 jQuery (jsfiddle)

javascript - 如何检测 Javascript 中的侧鼠标点击?

java - 如何使用JAVA跨多个窗口(不同的jframe)处理事件?

android - 如何在android中使用带有recyclerview的单选按钮?