使用C#通过在Visual Studio中3个窗口形式之间的数据 [英] passing data between 3 windows forms in visual studio using C#

查看:226
本文介绍了使用C#通过在Visual Studio中3个窗口形式之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows应用程序,它有3种形式:Form1,2,3。我想送从窗口2 文本框的文本 Form1中,然后从是相同的文字Form1中 form3 ,即

I have a windows application which has 3 forms : Form1,2,3. I want to send text of a textbox from form2 to form1 and then that same text from form1 to form3, that is,

FORM2 文本 - > FORM1 - > FORM3

Text from FORM2-->FORM1-->FORM3

  • 表1,有2个按钮,openform2,openform3。
  • 在窗体2有一个文本框form2_textbox,和放大器;一个按钮send_to_form1_button
  • Form3有一个文本框received_from_form1_textbox

现在,

  • 在点击 Form1中窗体2 按钮 openform2 打开后,
  • 在一个字符串输入到文本框的 form2_textbox 窗体2
  • 当按钮 form2_button 这种形式被点击,那么我想<$​​ C $ C> Form1中来接受这个字符串值放大器;将其存储在一个字符串 receivefromform2
  • ,然后显示在该字符串值 form3_textbox Form3
  • on clicking button openform2 on form1, Form2 opens,
  • a string is entered in textbox form2_textbox of Form2,
  • when button form2_button of this form is clicked, then I want Form1 to receive this string value & stores it in a string receivefromform2,
  • and then displays this string value on to form3_textbox of Form3.
public partial class Form1 : Form
{
    string receivefromForm2a;

    public Form1()
    { InitializeComponent(); }

    public void Method_Receive_From_Form2(string receivefromForm2)
    {
        receivefromForm2a = receivefromForm2;
        Form3 f3 = new Form3(receivefromForm2a);
    }

    private void openform3_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();**----this line gives error:No overload for method Form3 takes 0 arguments**

            f3.Show();
        }

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


public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        string loginname = form2_textbox.Text;
    }

    //SENDING VALUE OF TEXTBOX ON FORM2 TO FORM1.

    private void send_to_form1_button_Click(object sender, EventArgs e)
    {
        Form1 f1 = new Form1();
        f1.Method_Receive_From_Form2(form2_textbox.Text);
    }
}


public partial class Form3 : Form
{
    public Form3(string receive_from_Form1)
    {
        InitializeComponent();
        received_from_form1_textbox.Text = receive_from_Form1;
    }
}

出现此错误,因为窗口2 我对象创建过程中给定的参数为 Form1中。 所以,我应该怎么办?有没有其他办法做到这一点,或如何删除这个错误?

This error occurs because in form2 I have given argument for form1 during object creation. So what should I do? Is there any other way to do this or how do I remove this error?

当我包括 f3.Show()的方法 Method_Receive_From_Form2 ,然后没有任何错误。但这种自动使 form3 负载没有任何按钮的点击。但我想 form3 点击 Form1中按钮打开。然后该值被显示在文本框。

When I include the f3.Show() in the method Method_Receive_From_Form2 then there is no error. but this makes the form3 load automatically without any button click. But I want form3 to open by clicking the button on form1. And then the value to be shown in the textbox.

推荐答案

我会建议使用构造函数来使用性质的变化。这将保持正确的事情'包含',它是pretty的简单。

I would recommend a change from using constructors to using properties. This will keep things properly 'contained' and it's pretty simple.

EX:

public partial class Form3 : Form
{
   public String form1Text {get; set;}

   public Form3()
   {
       InitializeComponent();
   }
}


public partial class Form2 : Form
{
   public Form2()
   {
       InitializeComponent();
       string loginname = form2_textbox.Text;
   }

   public String form2Text {get; set;}

   private void send_to_form1_button_Click(object sender, EventArgs e)
   {

       form2Text = form2_textbox.Text;
       this.DialogResult = DialogResult.Ok;
       this.Close();
   }
}

然后在窗体1:

Then in form 1:

public partial class Form1 : Form
{
   string receivefromForm2a;

   public Form1()
   { InitializeComponent(); }       

    private void openform3_Click(object sender, EventArgs e)
    {
        Form3 f3 = new Form3();
        f3.form1Text = receivefromForm2a;
        f3.Show();
    }

private void OPENFORM2_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    if(f2.ShowDialog() == DialogResult.Ok)
    {
      receivefromForm2a = f2.form2Text; //New Property on Form2.
    }
}
}

这篇关于使用C#通过在Visual Studio中3个窗口形式之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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