How do i take the text from Form2.textbox and paste it into Form1.textbox?
-
I have 2 forms --> Form1 and Form2 Both contain a textbox(textbox1). Form2 have a button. When I click on that button i want to take the text from Form2.textbox and paste it into Form1.textbox. How to do that? Thanks.
You have not done even the minimum research on this one. Have some Google Foo[^]
Never underestimate the power of human stupidity RAH
-
I have 2 forms --> Form1 and Form2 Both contain a textbox(textbox1). Form2 have a button. When I click on that button i want to take the text from Form2.textbox and paste it into Form1.textbox. How to do that? Thanks.
You don't. You get strings, and let the form itself decide what to do with it. Exactly what you have to do depends on the relationship between the forms: if Form2 is opened from Form1, then it is a Child,and Form1 is a Parent: Transferring information between two forms, Part 1: Parent to Child[^] Transferring information between two forms, Part 2: Child to Parent[^] Transferring information between two forms, Part 3: Child to Child[^]
-
You have not done even the minimum research on this one. Have some Google Foo[^]
Never underestimate the power of human stupidity RAH
I knew your brother, Sherlock. the solution was interesting but none of you give it right. I give you the credits because you suggested what to search for ... but you did not helped. The solution:
//c# pass value between forms
//Form1
public partial class Form1 : Form
{
Form2 f2;
public Form1()
{
InitializeComponent();
f2 = new Form2();//open and leave it hidden - don't show it yet
}private void button1\_Click(object sender, EventArgs e) { f2.firstValue = textBox1.Text; f2.ShowDialog();//now show the form2 //f2.Show();//does not WORK for microsoft reasons.I ask them.They don't know. label1.Text = f2.secondValue; } }
//Form2
public partial class Form2 : Form
{
public Form2() {InitializeComponent();}
public string firstValue,secondValue;private void Form2\_Load(object sender, EventArgs e) { label1.Text = "Welcome " + firstValue; } private void button1Build\_Click(object sender, EventArgs e) { secondValue = textBox1.Text; this.Hide(); } }
-
I knew your brother, Sherlock. the solution was interesting but none of you give it right. I give you the credits because you suggested what to search for ... but you did not helped. The solution:
//c# pass value between forms
//Form1
public partial class Form1 : Form
{
Form2 f2;
public Form1()
{
InitializeComponent();
f2 = new Form2();//open and leave it hidden - don't show it yet
}private void button1\_Click(object sender, EventArgs e) { f2.firstValue = textBox1.Text; f2.ShowDialog();//now show the form2 //f2.Show();//does not WORK for microsoft reasons.I ask them.They don't know. label1.Text = f2.secondValue; } }
//Form2
public partial class Form2 : Form
{
public Form2() {InitializeComponent();}
public string firstValue,secondValue;private void Form2\_Load(object sender, EventArgs e) { label1.Text = "Welcome " + firstValue; } private void button1Build\_Click(object sender, EventArgs e) { secondValue = textBox1.Text; this.Hide(); } }
-
f2.ShowDialog();//now show the form2
//f2.Show();//does not WORK for microsoft reasons.I ask them.They don't know.I just tried a similar test and the second form shows just fine.
Veni, vidi, abiit domum
-
I knew your brother, Sherlock. the solution was interesting but none of you give it right. I give you the credits because you suggested what to search for ... but you did not helped. The solution:
//c# pass value between forms
//Form1
public partial class Form1 : Form
{
Form2 f2;
public Form1()
{
InitializeComponent();
f2 = new Form2();//open and leave it hidden - don't show it yet
}private void button1\_Click(object sender, EventArgs e) { f2.firstValue = textBox1.Text; f2.ShowDialog();//now show the form2 //f2.Show();//does not WORK for microsoft reasons.I ask them.They don't know. label1.Text = f2.secondValue; } }
//Form2
public partial class Form2 : Form
{
public Form2() {InitializeComponent();}
public string firstValue,secondValue;private void Form2\_Load(object sender, EventArgs e) { label1.Text = "Welcome " + firstValue; } private void button1Build\_Click(object sender, EventArgs e) { secondValue = textBox1.Text; this.Hide(); } }
Please don't do that - making your fields public is a very bad idea: use properties instead. And the Show does work - but you probably don't understand the difference between Show and ShowDialog.
f2.firstValue = textBox1.Text;
f2.ShowDialog();
label1.Text = f2.secondValue;Waits until the Form2 instance is closed before executing the following line.
f2.firstValue = textBox1.Text;
f2.Show();
label1.Text = f2.secondValue;Returns immediately and executes the following line straight away, before your user has any chance to type a new value.
-
Please don't do that - making your fields public is a very bad idea: use properties instead. And the Show does work - but you probably don't understand the difference between Show and ShowDialog.
f2.firstValue = textBox1.Text;
f2.ShowDialog();
label1.Text = f2.secondValue;Waits until the Form2 instance is closed before executing the following line.
f2.firstValue = textBox1.Text;
f2.Show();
label1.Text = f2.secondValue;Returns immediately and executes the following line straight away, before your user has any chance to type a new value.
Ohooo, my Christmas is coming early for me - Now, that is a real helpful answer - you should be proud. I am. And i thank you for clearing it up. The global var, is the best thing to do because they are not using much space (like the properties does with a lot of script - get then set, not good for the coder at all). Maybe they(properties) are good in some VERY particular(i dont really know them) cases but not in all. I think with global var you maintain a lighter and easier code. :)
-
I knew your brother, Sherlock. the solution was interesting but none of you give it right. I give you the credits because you suggested what to search for ... but you did not helped. The solution:
//c# pass value between forms
//Form1
public partial class Form1 : Form
{
Form2 f2;
public Form1()
{
InitializeComponent();
f2 = new Form2();//open and leave it hidden - don't show it yet
}private void button1\_Click(object sender, EventArgs e) { f2.firstValue = textBox1.Text; f2.ShowDialog();//now show the form2 //f2.Show();//does not WORK for microsoft reasons.I ask them.They don't know. label1.Text = f2.secondValue; } }
//Form2
public partial class Form2 : Form
{
public Form2() {InitializeComponent();}
public string firstValue,secondValue;private void Form2\_Load(object sender, EventArgs e) { label1.Text = "Welcome " + firstValue; } private void button1Build\_Click(object sender, EventArgs e) { secondValue = textBox1.Text; this.Hide(); } }
_Q12_ wrote:
I knew your brother, Sherlock.
Actually you didn't - read the book "The Moon is a Harsh Mistress" and you will understand the reference and if you don't I can get Manny to come and belt you with his left arm or simply drop a rock on you!
Never underestimate the power of human stupidity RAH
-
Ohooo, my Christmas is coming early for me - Now, that is a real helpful answer - you should be proud. I am. And i thank you for clearing it up. The global var, is the best thing to do because they are not using much space (like the properties does with a lot of script - get then set, not good for the coder at all). Maybe they(properties) are good in some VERY particular(i dont really know them) cases but not in all. I think with global var you maintain a lighter and easier code. :)
_Q12_ wrote:
The global var, is the best thing to do because they are not using much space (like the properties does with a lot of script - get then set, not good for the coder at all). Maybe they(properties) are good in some VERY particular(i dont really know them) cases but not in all. I think with global var you maintain a lighter and easier code.
Well, actually every Project that contains more than 2 classes is worth the effort to use properties. It's not about the space, i think we are not living in times of "Dude my app is to big for that pc, i need to reduce code", anymore. I prefer properties because i know what is allowed to get out and into a class by restricting it with getters and setter. And not having global vars flying around! If you actually need global vars, use a globals class and share that one to all your other classes. Sorry alot of offtopic ....
if(this.signature != null) { MessageBox.Show("This is my signature"); } else { MessageBox.Show("404-Signature not found"); }
-
I have 2 forms --> Form1 and Form2 Both contain a textbox(textbox1). Form2 have a button. When I click on that button i want to take the text from Form2.textbox and paste it into Form1.textbox. How to do that? Thanks.
There's actually an article here that shows how. Take a look here: [Passing Data Between Forms] If that doesn't work try using searching Google for something like: "Passing data between forms in C#" Good luck. ;)
======================= Every experience in life is a lesson to be learned A. Stevens B.S., Computer Science
-
I knew your brother, Sherlock. the solution was interesting but none of you give it right. I give you the credits because you suggested what to search for ... but you did not helped. The solution:
//c# pass value between forms
//Form1
public partial class Form1 : Form
{
Form2 f2;
public Form1()
{
InitializeComponent();
f2 = new Form2();//open and leave it hidden - don't show it yet
}private void button1\_Click(object sender, EventArgs e) { f2.firstValue = textBox1.Text; f2.ShowDialog();//now show the form2 //f2.Show();//does not WORK for microsoft reasons.I ask them.They don't know. label1.Text = f2.secondValue; } }
//Form2
public partial class Form2 : Form
{
public Form2() {InitializeComponent();}
public string firstValue,secondValue;private void Form2\_Load(object sender, EventArgs e) { label1.Text = "Welcome " + firstValue; } private void button1Build\_Click(object sender, EventArgs e) { secondValue = textBox1.Text; this.Hide(); } }
That's it, but it's not very elegant. You can use properties to return the value of visual objects. Like so. There is no reason to create that form and leave it in memory. It better to use it and then destroy it as soon as you're done.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { using (Form2 f2 = new Form2()) { f2.ShowDialog(); //f2.Show(); //this doesn't work because ShowDialog is required to stop the process and wait for input, show to show the form and moves on label1.Text = f2.TextValue; } } } public partial class Form2 : Form { public string TextValue { get { return textBox1.Text; } set { textBox1.Text = value; } } public string LabelValue { get { return label1.Text; } set { label1.Text = value; } } public Form2() { InitializeComponent(); } }
-
_Q12_ wrote:
The global var, is the best thing to do because they are not using much space (like the properties does with a lot of script - get then set, not good for the coder at all). Maybe they(properties) are good in some VERY particular(i dont really know them) cases but not in all. I think with global var you maintain a lighter and easier code.
Well, actually every Project that contains more than 2 classes is worth the effort to use properties. It's not about the space, i think we are not living in times of "Dude my app is to big for that pc, i need to reduce code", anymore. I prefer properties because i know what is allowed to get out and into a class by restricting it with getters and setter. And not having global vars flying around! If you actually need global vars, use a globals class and share that one to all your other classes. Sorry alot of offtopic ....
if(this.signature != null) { MessageBox.Show("This is my signature"); } else { MessageBox.Show("404-Signature not found"); }
Devils advocate here. If you aren't serialising and you aren't doing anything other than setting a field in your property, then what is the point of your property? They tend to be overused because there is this dogma that they promote encapsulation - they don't, if your class needs to change the property (say to add a multiplier into place), then you can as easily wrap the field up into a property at that point.
-
Devils advocate here. If you aren't serialising and you aren't doing anything other than setting a field in your property, then what is the point of your property? They tend to be overused because there is this dogma that they promote encapsulation - they don't, if your class needs to change the property (say to add a multiplier into place), then you can as easily wrap the field up into a property at that point.
Thanks for this additional thought. This shows me that my explanation was not clear enough. But it is my opinion on using properties, I never wanted to state that this is how you have to di it ;) Addidionally it's my style of writing code. But back to the sense of properties. I use it for restricting the values that go IN and OUT, so not only Setting variables ;) Secondly, yes for only Setting variables it might be oversized, therefore i referenced to a globals class that holds this variables, yeah this is just shifting space requirements around you'd say. An example for what properties are used , imo:
public class Person
{
private string name = "";
private DateTime birth = DateTime.Now;
private int age = 0;
private bool modified = false;public string Name { //Additionally a if clause to check a valid value might be interesting for user inputs set{ this.name = value; this.modified = true; } get{ return this.name; } } //And so on till modified //Because of this i never ever can make a mistake where i cange this value outside of the class public bool Modified { get{ return this.modified; } } //Method for saving itself to database public bool SaveYourSelf() { //Save your self to database if(//Command was successfull//) { this.modified = false; } }
}
//Another class that checks if Person was modifiedpublic class AnotherClass
{
public void checkPersonModified()
{
if (p.Modified)
{
//Do operations, save to database
}
}
}I personally think that in this case, which is a case that occurs not only in very rare programms is the one where using properties does make sense. Correct me if im wrong, but that was the thing i wanted to Point out quoting _Q12_ in his answer where he stated that this is a rare case.
if(this.signature != "") { MessageBox.Show("This is my signature: " + Environment.NewLine + signature); } else { MessageBox.Show("404-Signature not found"); }
-
Ohooo, my Christmas is coming early for me - Now, that is a real helpful answer - you should be proud. I am. And i thank you for clearing it up. The global var, is the best thing to do because they are not using much space (like the properties does with a lot of script - get then set, not good for the coder at all). Maybe they(properties) are good in some VERY particular(i dont really know them) cases but not in all. I think with global var you maintain a lighter and easier code. :)
Global variables/global properties, whatever you choose to use, are generally a poor idea. Now, I know that OOP principles can become almost religious dogma, but it really is a good idea to promote loose coupling and single responsibility. With global variables, you have put strong coupling into place, and it makes it much harder reuse code in other applications because now you have to copy two things around - at least one of which is going to contain cruft that you don't need in your new project. If your argument against fields is that typing the extra get/set is a burden then you shouldn't be in this game because that's just lazy.
-
I have 2 forms --> Form1 and Form2 Both contain a textbox(textbox1). Form2 have a button. When I click on that button i want to take the text from Form2.textbox and paste it into Form1.textbox. How to do that? Thanks.
Well, there are several ways. Simple way is to make property to Form1 class which set values to textbox so when button clicked, you have to set to Form1.XXX Property(You have to get Form1 class instance from From2 class) . Other way is to use message. :) You think more depthly, there are many ways.