0

我有一个名为“ 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
4

1 回答 1

2

MSDN中,您可以找到一个 VB.Net 示例如何枚举给定类的所有成员:

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Class MyFindMembersClass

    Public Shared Sub Main()
        Dim objTest As New Object()
        Dim objType As Type = objTest.GetType()
        Dim arrayMemberInfo() As MemberInfo
        Try
            'Find all static or public methods in the Object 
            'class that match the specified name.
            arrayMemberInfo = objType.FindMembers(MemberTypes.Method, _
                              BindingFlags.Public Or BindingFlags.Static _
                              Or BindingFlags.Instance, _
                              New MemberFilter(AddressOf DelegateToSearchCriteria), _
                              "ReferenceEquals")

            Dim index As Integer
            For index = 0 To arrayMemberInfo.Length - 1
                Console.WriteLine("Result of FindMembers -" + ControlChars.Tab + _
                               arrayMemberInfo(index).ToString() + ControlChars.Cr)
            Next index
        Catch e As Exception
            Console.WriteLine("Exception : " + e.ToString())
        End Try
    End Sub 'Main

    Public Shared Function DelegateToSearchCriteria _
                            (ByVal objMemberInfo As MemberInfo, _
                             ByVal objSearch As Object) As Boolean
        ' Compare the name of the member function with the filter criteria.
        If objMemberInfo.Name.ToString() = objSearch.ToString() Then
            Return True
        Else
            Return False
        End If
    End Function 'DelegateToSearchCriteria 
End Class 'MyFindMembersClass

另一种选择是将所有布尔变量放入一个Dictionary对象中。这将允许您“按名称”访问布尔值而不使用Reflection.

举例说明:

Imports System.Collections.Generic

Module vbModule

Class Example

    Private _dictionary

    Public Sub New()
        ' Allocate and populate the field Dictionary.
        Me._dictionary = New Dictionary(Of String, Boolean)
        Me._dictionary.Add("v1", False)
        Me._dictionary.Add("v2", True)
    End Sub

    Public Function GetValue(name as String) As Boolean
        ' Return value from private Dictionary.
        Return Me._dictionary.Item(name)
    End Function

    Public Sub SetValue(name as String, val as Boolean)
        Me._dictionary.Item(name) = val
    End Sub

End Class

Sub Main()
    ' Allocate an instance of the class.
    Dim example As New Example

    ' Write a value from the class.
    Console.WriteLine(example.GetValue("v1"))
    Console.WriteLine(example.GetValue("v2"))
    example.SetValue("v1", true)
    Console.WriteLine(example.GetValue("v1"))
End Sub
End Module
于 2013-04-12T10:24:46.927 回答