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. Passing strings between windows forms

Passing strings between windows forms

Scheduled Pinned Locked Moved C#
winformshelp
12 Posts 7 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.
  • P Offline
    P Offline
    Phil Saville
    wrote on last edited by
    #1

    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.

    OriginalGriffO D S D 4 Replies Last reply
    0
    • P Phil Saville

      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.

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

      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

      "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
      • P Phil Saville

        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.

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        Here[^] you go

        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)

        1 Reply Last reply
        0
        • P Phil Saville

          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.

          S Offline
          S Offline
          Som Shekhar
          wrote on last edited by
          #4

          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.

          S 1 Reply Last reply
          0
          • S Som Shekhar

            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.

            S Offline
            S Offline
            Syed Shahid Hussain
            wrote on last edited by
            #5

            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

            OriginalGriffO 1 Reply Last reply
            0
            • S 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

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

              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

              "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

              P 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                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

                P Offline
                P Offline
                Phil Saville
                wrote on last edited by
                #7

                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"

                P 1 Reply Last reply
                0
                • P Phil Saville

                  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"

                  P Offline
                  P Offline
                  Phil Saville
                  wrote on last edited by
                  #8

                  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.

                  R OriginalGriffO 2 Replies Last reply
                  0
                  • P Phil Saville

                    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.

                    R Offline
                    R Offline
                    Rasepretrep2
                    wrote on last edited by
                    #9

                    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.

                    P 1 Reply Last reply
                    0
                    • R Rasepretrep2

                      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.

                      P Offline
                      P Offline
                      Phil Saville
                      wrote on last edited by
                      #10

                      ahhhh thankyou yes that worked, should really have noticed that, thankyou

                      1 Reply Last reply
                      0
                      • P Phil Saville

                        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.

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

                        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

                        "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
                        • P Phil Saville

                          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.

                          D Offline
                          D Offline
                          DX Roster
                          wrote on last edited by
                          #12

                          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;

                          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