从另一种形式调用形式1函数

本文关键字:函数 调用 另一种 | 更新日期: 2023-09-27 18:13:03

public partial class UserLoginForm : Form
{
   private void LoginForm_Load(object sender, EventArgs e)
   {
     Common.UserLoginFormObject = this;  //Store UserLoginForm Object in Static class Common.
   }
   private void DoSomething()
   { 
     //some code
   }
}
public partial class MainForm : Form
{
        private void cmdLogOut_Click(object sender, EventArgs e)
        {
           Common.UserLoginFormObject.DoSomething();//Now here i have to call Dosomething function.
        }
}

如何从另一个表单调用表单1函数

从另一种形式调用形式1函数

Make DoSomething function Public

public void DoSomething()
{ 
  //some code
}

要调用DoSomething方法,应该有一个对象Common.UserLoginFormObject,确保您创建new UserLoginForm(),您将对象分配给Common.UserLoginFormObject。你还需要把DoSomething方法设为public。

正如Henk Holterman在评论中提到的,你可以使DoSomething方法静态,然后你不需要有对象来调用该方法。