Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do i take the text from Form2.textbox and paste it into Form1.textbox?

How do i take the text from Form2.textbox and paste it into Form1.textbox?

Scheduled Pinned Locked Moved C#
questiontutorial
16 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _Q12_
    wrote on last edited by
    #1

    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.

    M OriginalGriffO D W 4 Replies Last reply
    0
    • _ _Q12_

      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.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      You have not done even the minimum research on this one. Have some Google Foo[^]

      Never underestimate the power of human stupidity RAH

      _ 1 Reply Last reply
      0
      • _ _Q12_

        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.

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        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[^]

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • M Mycroft Holmes

          You have not done even the minimum research on this one. Have some Google Foo[^]

          Never underestimate the power of human stupidity RAH

          _ Offline
          _ Offline
          _Q12_
          wrote on last edited by
          #4

          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();
          	}
          }
          
          L OriginalGriffO M B 4 Replies Last reply
          0
          • _ _Q12_

            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();
            	}
            }
            
            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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

            _ 1 Reply Last reply
            0
            • L Lost User

              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

              _ Offline
              _ Offline
              _Q12_
              wrote on last edited by
              #6

              f2.Show(); well, maybe in THIS particular case is NOT suppose to work. i dont know and i dont care - i solved my problem. lol

              1 Reply Last reply
              0
              • _ _Q12_

                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();
                	}
                }
                
                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                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.

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                _ 1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  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.

                  _ Offline
                  _ Offline
                  _Q12_
                  wrote on last edited by
                  #8

                  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. :)

                  H P 2 Replies Last reply
                  0
                  • _ _Q12_

                    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();
                    	}
                    }
                    
                    M Offline
                    M Offline
                    Mycroft Holmes
                    wrote on last edited by
                    #9

                    _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

                    1 Reply Last reply
                    0
                    • _ _Q12_

                      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. :)

                      H Offline
                      H Offline
                      HobbyProggy
                      wrote on last edited by
                      #10

                      _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"); }

                      P 1 Reply Last reply
                      0
                      • _ _Q12_

                        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.

                        D Offline
                        D Offline
                        DRAYKKO
                        wrote on last edited by
                        #11

                        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

                        1 Reply Last reply
                        0
                        • _ _Q12_

                          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();
                          	}
                          }
                          
                          B Offline
                          B Offline
                          BBatts
                          wrote on last edited by
                          #12

                          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(); 
                                  }
                              }
                          
                          1 Reply Last reply
                          0
                          • H HobbyProggy

                            _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"); }

                            P Offline
                            P Offline
                            Pete OHanlon
                            wrote on last edited by
                            #13

                            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.

                            H 1 Reply Last reply
                            0
                            • P Pete OHanlon

                              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.

                              H Offline
                              H Offline
                              HobbyProggy
                              wrote on last edited by
                              #14

                              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 modified

                              public 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"); }

                              1 Reply Last reply
                              0
                              • _ _Q12_

                                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. :)

                                P Offline
                                P Offline
                                Pete OHanlon
                                wrote on last edited by
                                #15

                                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.

                                1 Reply Last reply
                                0
                                • _ _Q12_

                                  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.

                                  W Offline
                                  W Offline
                                  WuRunZhe
                                  wrote on last edited by
                                  #16

                                  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.

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups