从另一个表单 vb.net 运行一个函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36822782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:50:27  来源:igfitidea点击:

Run a function from another form vb.net

vb.netforms

提问by Randomizers

I've got Module Main() with a function called Main() in Form1

我在 Form1 中有一个名为 Main() 的函数的 Module Main()

Public Module Main 
 Public Sub Main()

 End Sub
End Module

And I want to run this from Form2 doing Form1.Main() won't work because that will look for a class in form2 named form1.

我想从 Form2 运行它,做 Form1.Main() 将不起作用,因为它将在 form2 中查找名为 form1 的类。

So how can I do this?

那么我该怎么做呢?

采纳答案by Visual Vincent

I believe I've found your problem. Mainseems to be a reserved keyword (or it at least serves some other purpose to Visual Studio), so you cannot use it as a class or module name.

我相信我已经找到了你的问题。Main似乎是一个保留关键字(或者它至少对 Visual Studio 有其他用途),因此您不能将其用作类或模块名称。

If you rename the module to for example MainModule, you are then able to call:

如果将模块重命名为 example MainModule,则可以调用:

MainModule.Main()

回答by Mattheu Norwood

you can also use the structure of the Form1 and Form2 class to target your routines that you wish to run.

您还可以使用 Form1 和 Form2 类的结构来定位您希望运行的例程。

public class form1 
public shared sub testsub()
msgbox("hello world")
end sub
end class

public class form2 
public shared sub testsub2()
' this will allow you to call a sub from form1 this also works with variables, functions, ect
form1.testsub()
end sub
end class