进一步回答我的问题:使用组合框调用私有函数 [英] Further to my question: Calling private functions using combobox

查看:22
本文介绍了进一步回答我的问题:使用组合框调用私有函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Below is my code that I want to run. I want to call the same function that user choses from the combo box. Please advise how it can be managed.

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

结束课程

推荐答案

您将需要使用反射来实现这一点.反射是计算机程序在运行时观察和修改自身结构和行为的过程

You will need to use reflection to achieve this. 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 方法中使用此代码

Add Imports System.Reflection before your class definition and use this code in you ComboBox1_SelectedIndexChanged method

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

这篇关于进一步回答我的问题:使用组合框调用私有函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆