继我的问题:使用组合框调用私有函数

时间:2010-11-11 07:51:20

标签: vb.net

以下是我想要运行的代码。我想调用用户从组合框中选择的相同功能。请告知如何管理它。

Public Class Form1
Private Sub One()
    MsgBox("One is called")
End Sub
Private Sub Two()
    MsgBox("Two is called")
End Sub
Private Sub Three()
    MsgBox("Three is called")
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim vrTextNow As String = ComboBox1.Text
    Call vrTextNow
End Sub

结束班

1 个答案:

答案 0 :(得分:1)

您需要使用反射来实现此目的。 Reflection is the process by which a computer program can observe and modify its own structure and behavior at runtime

在类定义之前添加Imports System.Reflection并在您的ComboBox1_SelectedIndexChanged方法中使用此代码

Dim vrTextNow As String = ComboBox1.Text
        Dim method As MethodInfo
        method = Me.GetType().GetMethod(vrTextNow, BindingFlags.NonPublic Or BindingFlags.Instance)
        method.Invoke(Me, Nothing)