Passing strings between windows forms
-
hi there, im having trouble trying to pass strings between my windows forms... i have 2 simple forms, the first has a textbox and a button on it, and the second has a label on it. When the user inputs something into the textbox on the first form and presses the button i would like the text in this textbox to be passed to the 2nd form and be displayed as the label text. Any help is much appreciated.
-
hi there, im having trouble trying to pass strings between my windows forms... i have 2 simple forms, the first has a textbox and a button on it, and the second has a label on it. When the user inputs something into the textbox on the first form and presses the button i would like the text in this textbox to be passed to the 2nd form and be displayed as the label text. Any help is much appreciated.
There are many ways to do this, but the most flexible is for the first form to signal an event that is handled by the second, passing the string as an argument.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
hi there, im having trouble trying to pass strings between my windows forms... i have 2 simple forms, the first has a textbox and a button on it, and the second has a label on it. When the user inputs something into the textbox on the first form and presses the button i would like the text in this textbox to be passed to the 2nd form and be displayed as the label text. Any help is much appreciated.
Dave
Tip: Passing values between objects using events (C#)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
hi there, im having trouble trying to pass strings between my windows forms... i have 2 simple forms, the first has a textbox and a button on it, and the second has a label on it. When the user inputs something into the textbox on the first form and presses the button i would like the text in this textbox to be passed to the 2nd form and be displayed as the label text. Any help is much appreciated.
depends. 1. Is the form2 being opened when the button is pressed in the form1? in that case, you should pass the string through constructor of the form2. somthing like:
class Form1:Form
{
button1_Click
{
Form2 form2 = new Form2("My Text");
}
}class Form2:Form
{
public Form2(string caption)
{
this.Text = caption;
}
}2. If the two forms are open together, then it is a bit tricky. You will need to either implement custom events or use static class with timer.
-
depends. 1. Is the form2 being opened when the button is pressed in the form1? in that case, you should pass the string through constructor of the form2. somthing like:
class Form1:Form
{
button1_Click
{
Form2 form2 = new Form2("My Text");
}
}class Form2:Form
{
public Form2(string caption)
{
this.Text = caption;
}
}2. If the two forms are open together, then it is a bit tricky. You will need to either implement custom events or use static class with timer.
first is easy, but for second option I would like to continue ur idea :) if both forms are declared globally so each form can use their resources.... so at reload display the desired text.
Syed Shahid Hussain
-
first is easy, but for second option I would like to continue ur idea :) if both forms are declared globally so each form can use their resources.... so at reload display the desired text.
Syed Shahid Hussain
Bad idea! Firstly, there are no global variables in C# - for good reason! Secondly, exposing form internals so that another form or class can access them is bad practice, as it means that the two classes are tied together - you cannot safely change one of them without potentially affecting the other. It is much better to keep internals private, so that nothing outside the class relies on them.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
Bad idea! Firstly, there are no global variables in C# - for good reason! Secondly, exposing form internals so that another form or class can access them is bad practice, as it means that the two classes are tied together - you cannot safely change one of them without potentially affecting the other. It is much better to keep internals private, so that nothing outside the class relies on them.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
the two forms are open at the same time which is why i am having trouble...i've tried passing them using the constructor but this doesn't seem to work and i just get the error coming back saying..." 'Pass.Form1' does not contain a constructor that takes '0' arguments"
-
the two forms are open at the same time which is why i am having trouble...i've tried passing them using the constructor but this doesn't seem to work and i just get the error coming back saying..." 'Pass.Form1' does not contain a constructor that takes '0' arguments"
heres an update of my problem since i have tried to change it a little... 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.
-
heres an update of my problem since i have tried to change it a little... 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.
you probably instantiate form1 with no paramters (not in the code you posted here but at start of your main app?) . Change the text new Form1() to new Form1("") or something. BTW: if you just doubleclick on the error message it should point you to the row where the error occurred.
-
you probably instantiate form1 with no paramters (not in the code you posted here but at start of your main app?) . Change the text new Form1() to new Form1("") or something. BTW: if you just doubleclick on the error message it should point you to the row where the error occurred.
ahhhh thankyou yes that worked, should really have noticed that, thankyou
-
heres an update of my problem since i have tried to change it a little... 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.
There are a number of things wrong here! What happens if I click the button in form1, then the button in form2, then the button in form1 again, and so on? Because you are creating a new instance of the other form when you press the button and hiding the current form, you just end up creating an infinite series of hidden forms. When you eventually close the displayed form, you application does not exit, but the user can't see anything on screen, and has no way to get back to any form from which he could actually close the app. If you must do it that way, then Form1:
private void button1\_Click(object sender, EventArgs e) { Form2 f = new Form2(); Hide(); // You don't need This.Hide() here - it is obvious what is hiding. f.ShowDialog(); labelIWantDataIn.Text = f.Data; Show(); }
Form2:
private void button1\_Click(object sender, EventArgs e) { Close(); }
And provide a property to access the textbox text. I would still do it via an event though!
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
hi there, im having trouble trying to pass strings between my windows forms... i have 2 simple forms, the first has a textbox and a button on it, and the second has a label on it. When the user inputs something into the textbox on the first form and presses the button i would like the text in this textbox to be passed to the 2nd form and be displayed as the label text. Any help is much appreciated.
try this code: Method 1: on page 1: Session["Value"]=textBox1.text.tostring(); on page 2: retrieve value by using label1.text=Session["value"].tostring(); Method 2: On page 1: response.redirect("default2.aspx?value='"+textbox1.text+"'"); on page 2: string str=request.queryString("value").tostring(); label1.text=str;