hi there, im having trouble trying to pass strings from my 2nd windows form to my 1st windows forms...
i have two simple forms, the first just has just a button on which opens the 2nd form so the 2 forms are open at the same time. On this 2nd form i then have a text box and another button, the user will enter some text into the textbox and then press the button. Once this button is pressed i would like the text to be passed to the first form. I have tried using the following code...
Form1:
public partial class Form1 : Form
{
string text;
public Form1(string receivedText)
{
text = receivedText;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
this.Hide();
f.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
if (text == "")
{
}
else
{
label1.Text = text;
}
}
}
Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f = new Form1(textBox1.Text);
this.Hide();
}
}
This does not seem to work though and i get the error..." 'Pass.Form1' does not contain a constructor that takes '0' arguments.
Please help!
Loading
Sam HobbsPosted Mar 3, 2010, 12:50 PM
kiran kumarPosted Mar 2, 2010, 9:55 AM
mark the answer if it helps you
Syed ShakeerPosted Mar 2, 2010, 9:52 AM
1)First Create a Static Class and declare a Variable as follows:-
Static Class statclass
{
public string statstring=String.Empty;
}
2)assing the textbox value to statstring in 2Form as Follows:-
private void button1_Click(object sender, EventArgs e)
{
statclass.statstring =textbox1.Text;
Form1 f = new Form1(textBox1.Text);
f.Show();
}
3)Dispalying the 2Form text value in Form1 as Follows:-
write the below code in FormLoad
Label1.Text=statclass.statstring ;
Raaj KumarPosted Mar 2, 2010, 9:47 AM
public Form1()
{
InitializeComponent();
}
So you will have what you need. Now there wont be any problem.
Regards,
raaj
Phil SavillePosted Mar 2, 2010, 9:22 AM
if this makes sense
Raaj KumarPosted Mar 2, 2010, 9:14 AM
It seems that in Program.cs file the problem happens. Since it would be like
Application.Run(new Form1());
and the actual constructor is
public Form1(string receivedText)
{
text = receivedText;
InitializeComponent();
}
which expects a parameter. So change the form name to Form2 like this,
Application.Run(new Form2()); (inside Program.cs where this is the entry point of the application)
If needed I can provide you a sample application of what you required.
Regards,
raaj