This is my second question for the same problem but made this example much simpler for me to understand your answers better.
I’m getting “No overload for method ‘form2’ takes ‘0’ arguments”
Button on form1 takes you to form2 and also takes the value of the textbox over. Form2 displays value of textbox in label. Then I have a back button on form2 that takes you back to form1.
Im getting the error when I use the below code the second time:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
this.Hide();
}
Here is all the code for this example:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(textBox1.Text);
frm2.Show();
this.Hide();
}
}
public partial class Form2 : Form
{
public string txtbox;
public Form2(string txtbox)
{
InitializeComponent();
this.txtbox = txtbox;
}
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = txtbox;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
this.Hide();
}
}
Can someone please explain this and how to get this right. I’m still noob so please be detailed.
You don’t have a default constructor in
Form2, your only available constructor inForm2requires a string parameter.You may define a parameterless constructor in your form.
The other option is to utalize the existing constructor and pass it some string value, in your button click event.