如何从Form2调用Form1的方法 [英] How do I call method of Form1 from Form2

查看:148
本文介绍了如何从Form2调用Form1的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个名为Form1(主要名称)的表单和另一个名为Form2的表单。在Form1中,我有一个与Form1中的textBox控件交互的方法。我想在Form2中调用此方法。







--------- -------------------------------------------------- ----------------------------------------------

 命名空间 WindowsFormsApplication 
{
public partial class Form1:Form
{
internal static Form2 form2 = new Form2();

public Form1()
{
InitializeComponent();
}

private void buttonShowForm2_Click( object sender,EventArgs e)
{
form2.Show();
}

public void ShowText() // 我要调用的方法
{
textBoxInForm1.Text = 从另一个表单调用的方法;
}
}
}





------------ -------------------------------------------------- -------------------------------------------



 命名空间 WindowsFormsApplication 
{
public partial class Form2:表格
{
< span class =code-keyword> public Form2()
{
InitializeComponent();
}

private void buttonCallMethod_Click( object <> span sender,EventArgs e)
{
Form1 form1 = new Form1(); // 这会创建新的实例,这是错误的
form1.ShowText(); // Form1中定义的方法
。关闭();
}
}
}



-------------------- -------------------------------------------------- -----------------------------------

解决方案

< blockquote>你可能会做类似

  public   partial   class  Form2:表格
{
Form1 form1;
public Form2(Form1 form1)
{
this .form1 = form1;
// ...
}
private void buttonCallMethod_Click( object sender,EventArgs e)
{
form1.ShowText(); // Form1中定义的方法
// ...
}
// ...


I必须使用当前表单在Form1类中定义一个:



-------------------- -------------------------------------------------- -----------------------------------

 命名空间 WindowsFormsApplication 
{
public partia l class Form1:表格
{
内部 静态 Form2 form2;
内部 静态 Form1 form1;

public Form1()
{
InitializeComponent();
form1 = ; // 参考当前Form1
}

私有 void buttonShowForm2_Click( object sender,EventArgs e)
{
form2 = new Form2( );
form2.Show();
}

public void ShowText() // 我要调用的方法
{
textBoxInForm1.Text = 从另一个表单调用的方法;
}
}
}





------------ -------------------------------------------------- -------------------------------------------

 命名空间 WindowsFormsApplication 
{
public < span class =code-keyword> partial class Form2:表单
{
public Form2()
{
InitializeComponent();
}

private void buttonCallMethod_Click( object sender,EventArgs e)
{
Form1.form1.ShowText(); // Form1中定义的方法
this .Close();
}
}
}





------------ -------------------------------------------------- -------------------------------------------


我强烈建议阅读谢尔盖的过去的答案 [ ^ ]和他的优秀提示:一次回答的许多问题 - Windows窗体或WPF Windows之间的协作 [ ^ ]


So I have one form, named Form1 (main one) and another one, named Form2. In Form1 I have a method which interact with a textBox control in Form1. I want to call this method in Form2.



---------------------------------------------------------------------------------------------------------

namespace WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        internal static Form2 form2 = new Form2();

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonShowForm2_Click(object sender, EventArgs e)
        {
            form2.Show();
        }

        public void ShowText()//the method which I want to call
        {
            textBoxInForm1.Text = "Method Called From Another Form";
        }
    }
}



---------------------------------------------------------------------------------------------------------

namespace WindowsFormsApplication
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void buttonCallMethod_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();//this creates new instance, which is wrong
            form1.ShowText();//the method defined in Form1
            this.Close();
        }
    }
}


---------------------------------------------------------------------------------------------------------

解决方案

You might do something like

public partial class Form2 : Form
{
  Form1 form1;
  public Form2(Form1 form1)
  {
      this.form1 = form1;
      //...
  }
  private void buttonCallMethod_Click(object sender, EventArgs e)
  {
      form1.ShowText();//the method defined in Form1
      //...
  }
  //...


I had to use the current form with defining one in the Form1 class:

---------------------------------------------------------------------------------------------------------

namespace WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        internal static Form2 form2;
        internal static Form1 form1;

        public Form1()
        {
            InitializeComponent();
            form1 = this;//refering to the current Form1
        }

        private void buttonShowForm2_Click(object sender, EventArgs e)
        {
            form2 = new Form2();
            form2.Show();
        }

        public void ShowText()//the method which I want to call
        {
            textBoxInForm1.Text = "Method Called From Another Form";
        }
    }
}



---------------------------------------------------------------------------------------------------------

namespace WindowsFormsApplication
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void buttonCallMethod_Click(object sender, EventArgs e)
        {
            Form1.form1.ShowText();//the method defined in Form1
            this.Close();
        }
    }
}



---------------------------------------------------------------------------------------------------------


I do strongly recommend to read Sergey's past answers[^] and His excellent tip: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows[^]


这篇关于如何从Form2调用Form1的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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