我有一个MDI Parentform,它有多个表单,声明为:
Partial Public Class Giriş
Private clients As thirdperson
Private suppliers As thirdperson
Private activecontrol As thirdperson
Private proposals As transactions
Private addproposal As addtransaction
private sales as transactions
private addsales as addtransation
Private products As products
...
...
我还有一个dataworks子作为一个类,但我在表单和每个表单中编码,如下所示: '例如,按下添加新按钮
If ActiveControl Is **clients** Or ActiveControl Is **suppliers** Then
activeform.dataworks (counter, "add new")
ElseIf ActiveControl Is products Then
products.dataworks (counter, "add new")
ElseIf ActiveControl Is addproposal Then
addproposal.dataworks (counter, "add new")
End If
我需要一种方法来弄清楚如何使用vairble名称作为实例名称。 (我不想每次都想定义每个实例名称,我只想知道是否有使用方法如下所示: '例如 dim formvariable as form
formvariable.dataworks(counter,"add new") --> just want to use this and assign the value for products, proposals vs to this variable.
这可能吗?
提前感谢您的帮助。 Sertac。
答案 0 :(得分:1)
正确的方法是开发一个Interface,以便您的所有子表单都能实现。该接口将具有dataworks()方法。然后,您可以将当前mdichild强制转换为接口类型并运行该方法。这将导致强类型编码,这是有道理的。
如果您只是想破解它,那么尝试使用反射从当前mdichild获取dataworks()子,并执行如下所示:
Dim frm As Form = Me.ActiveMdiChild
If Not IsNothing(frm) Then
Dim MI As Reflection.MethodInfo = frm.GetType.GetMethod("dataworks", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.IgnoreCase)
If Not IsNothing(MI) Then
MI.Invoke(frm, New Object() {counter, "add new"})
End If
End If
这是接口方法的一个简单示例:
Public Interface Data
Sub DataWorks(ByVal counter As Integer, ByVal msg As String)
End Interface
这是第三方实现界面的原因:
Public Class thirdperson
Implements Data
Public Sub DataWorks(counter As Integer, msg As String) Implements Data.DataWorks
Debug.Print(counter & ", " & msg)
End Sub
End Class
您的所有mdichild表单都必须以类似的方式进行修改。
然后MdiParent中的代码将更改为:
Dim frm As Form = Me.ActiveMdiChild
If TypeOf frm Is Data Then
Dim D As Data = DirectCast(frm, Data)
D.DataWorks(counter, "add new")
End If