我不确定我是否正确理解了这个问题,但这里有一些提示。
- 表单就是对象
您正在做的是使用 myForm 的默认实例,应该避免这种情况。相反,最好更新表单,如下所示:
dim frm as myForm ' Assuming myForm is the name of the form in the name field of the properties.
set frm = new myForm ' Now you have an instance of myForm, which you can pass along to other subs.
frm.label1 = "Hello"
frm.show
othersub frm
end Sub
public sub otherSub(byref frm as myForm)
frm.label1 = "byebye"
frm.show
end sub
- 如果您有 myForm1 和 myForm2,即具有不同类型的两个不同表单,并且您希望能够将一个或另一个传递给 OtherSub 并更改标签,该怎么办。
在这种情况下,我会做的是使用changeLabel(byval lbl as string) 方法创建一个接口。
为此,您需要创建一个名为 ILabelModifiable 的类模块(例如),您将在其中声明:
public sub changelabel(byval lbl as string)
public sub show() ' You need this method to be able to show the form.
然后,在 myForm1 和 myForm2 中,您将在代码隐藏的顶部添加:
Implements ILabelModifiable
并在表单的主体中,创建以下子
private sub ILabelModifiable_changelabel(byval lbl as string)
me.label1 = lbl
end
private sub ILabelModifiable_show()
me.show
end
完成此操作后,主要代码变为:
dim frm as iLabelModifiable
set frm = new myForm1
frm.changelabel "Hello"
frm.show
othersub frm
end Sub
public sub otherSub(byref frm as ILabelModifiable)
frm.changelabel "byebye"
frm.show
end sub
此方法的一个额外好处是您的代码不需要知道实际调用的标签是什么。可以在Myform1中调用label1,在myForm2中调用label2,没关系,代码只调用changeLabel。
- 杂项
在您的示例中,您无法控制用户的操作,例如您不会检查用户是否单击了确定,或者是否通过取消按钮或单击窗口右上角的“X”取消。我认为这是因为您希望尽可能地保持示例的重点,但以防万一,不要忘记这样做。请注意,如果表单在 firstSub 中被销毁(例如,因为您将其设置为空,而不是在用户单击取消按钮或“X”时将其隐藏),则 otherSub 将出错。