在事件中更改 RadioButton 检查状态
Change RadioButton checkstate in event
我正在将一些遗留代码从 VB6 迁移到 VB.NET,我 运行 遇到了一个恼人的问题。当前正在发生的是向用户提供 RadionButton
控件来表示层次结构。当它们 select 一个值时,代码会验证它是否有效(C 不能是 A 的子代,必须是 B 的子代),如果不是 returns 则 RadioButton
到原来的设置。
问题是当执行此操作的函数 returns 到事件处理程序时 returns RadioButton
状态变为单击时的状态(用户单击 C,代码 returns 它到 B,当函数退出时它 returns 到 C 触发事件,这将再次把它变成 B,等等,等等,导致无限循环)。有没有办法更改 RadioButton is selected inside a function called by
CheckedChanged` 事件并让它保持不变?
我知道更好的设计是提前禁用无效的 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
保留其 "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 value As Boolean
.
启用 Option Strict 也会突出显示 sender.Checked
作为错误:
Option Strict On disallows late binding
修复方法是将 sender
转换为 RadioButton
,以便您可以直接访问其属性; DirectCast(sender, RadioButton).Checked
.
您的代码将类似于:
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
我正在将一些遗留代码从 VB6 迁移到 VB.NET,我 运行 遇到了一个恼人的问题。当前正在发生的是向用户提供 RadionButton
控件来表示层次结构。当它们 select 一个值时,代码会验证它是否有效(C 不能是 A 的子代,必须是 B 的子代),如果不是 returns 则 RadioButton
到原来的设置。
问题是当执行此操作的函数 returns 到事件处理程序时 returns RadioButton
状态变为单击时的状态(用户单击 C,代码 returns 它到 B,当函数退出时它 returns 到 C 触发事件,这将再次把它变成 B,等等,等等,导致无限循环)。有没有办法更改 RadioButton is selected inside a function called by
CheckedChanged` 事件并让它保持不变?
我知道更好的设计是提前禁用无效的 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
保留其 "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 value As Boolean
.
启用 Option Strict 也会突出显示 sender.Checked
作为错误:
Option Strict On disallows late binding
修复方法是将 sender
转换为 RadioButton
,以便您可以直接访问其属性; DirectCast(sender, RadioButton).Checked
.
您的代码将类似于:
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