我有一个名为“ Cheat_Enabled ”的变量:
Dim Cheat_Enabled As Boolean
还有一个名为“ CheckBox_Cheat ”的复选框,带有“ Cheat ”标签
现在我想做一个动态方法,通过拆分(或其他)控件的名称来更改 var 的值。
例如,像这样的东西(代码显然不起作用):
Private Sub CheckBoxes_CheckedChanged(sender As Object, e As EventArgs) Handles _
CheckBox_Cheat.CheckedChanged
Dim SenderVarEquivalent As Object = _
Me.variables.Find(sender.Tag & "_Enabled")(0)
If sender.Checked Then SenderVarEquivalent = True _
Else SenderVarEquivalent = False
End Sub
' Info:
' Sender.Tag = "Cheat"
' Sender.Name = "CheckBox_Cheat"
' VariableName = "Cheat_Enabled"
我认为这可以通过一个 CType 或 DirectCast 或 GetObject 来完成,但我不知道该怎么做。
更新:
这是一个类似的简单代码,但不是将 ControlName 转换为 VariableName,而是将控制标记转换为 Checkbox 的 ControlName 对象:
Public Class Form1
Dim Cheat_Enabled As Boolean = True
Private Sub CheckBox_Cheat_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox_Cheat.CheckedChanged
CType(Controls("CheckBox" & "_" & sender.tag), CheckBox).Checked = Cheat_Enabled
End Sub
End Class
我希望如果我可以对 CType 控件名称执行相同的操作以捕获变量名称并使用它,例如像这样:
Dim Cheat_Enabled As Boolean
Private Sub CheckBox_Cheat_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox_Cheat.CheckedChanged
' Sender.name is : "CheckBox_Cheat"
' Sender.tag is : "Cheat"
' Variable name is: "Cheat_Enabled"
If sender.checked Then
CType(Variable(sender.tag & "_Enabled"), Boolean) = True
Else
CType(Variable(sender.tag & "_Enabled"), Boolean) = False
End If
End Sub