开发者

passing data between 3 windows forms in visual studio using C#

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,

Text from FORM2-->FORM1-->FORM3

  • Form 1, has 2 buttons , openform2, openform3.
  • Form2 has a textbox form2_textbox, & a button send_to_form1_button
  • Form3 has a textbox received_from_form1_textbox

Now,

  • 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;
    }
}

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?

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 the开发者_StackOverflow中文版n the value to be shown in the textbox.


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();
   }
}

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.
    }
}
}


create nonparametrized constructor for form3 like in form2:

public Form3() { InitializeComponent(); }

This usually to create abstract methods in forms and/or delegates for updating textboxes and sharing data between forms. Or create some data holder.


Create a Constructor which has an argument of type string in Form3.cs.

public Form3()
{
   InitializeComponent();
}
public Form3(string text):this()
{
   this.txtName.text=text;
}


That error is thrown because Form3 has no default Constructor anymore since you defined one with a string parameter. you need to create a default Constructor like this public Form3(){}.

But Instead of doing all this mess you can handle events of you both forms. Like if Form1 is the main Form then something like this can be done:

In Form1

public string textFromForm2 = string.Empty;
private void openform3_Click(object sender, EventArgs e)
{
    Form3 f3 = new Form3();
    f3.Controls["received_from_form1_textbox"].Text = textFromForm2 ;
    f3.Show();
}

private void OPENFORM2_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    //I am binding the event to a handler which will save text
    //you should check for null for f2.Controls returned any thing or not, i am leaving it for now
    f2.Controls["send_to_form1_button"].Click += (s,e)=>{
                 txtFromForm2 = f2.Controls["form2_textbox"].Text;
          };
    f2.Show();
}

Update

if you don't want to use Lambadas then bind events like this:

First you will need a reference to the Form2 so declare in your class like this:

Form2 f2;

then bind the event (in place of the lambada i have given before)

f2.Controls["send_to_form1_button"].Click  += new Eventhandler(click_handler);

then somewhere in Form1 class:

protected void click_handler(object sender, EventArgs e)
{
     if(f2 != null)
         txtFromForm2 = f2.Controls["form2_textbox"].Text;
}

similarly for 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 openform2_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.Show();
    }

    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();
    }
}

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

    //SENDING VALUE OF TEXTBOX ON FORM2 TO FORM1.

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

public partial class Form3 : Form
{
    public Form3(string receive_from_Form1)
    {
        InitializeComponent();

        received_from_form1_textbox.Text = receive_from_Form1;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜