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