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. referencing a control on another form

referencing a control on another form

Scheduled Pinned Locked Moved C#
design
4 Posts 3 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.
  • K Offline
    K Offline
    kornstyle
    wrote on last edited by
    #1

    I know that this is extremely simple. I want to display the text of a textBox on one form on the text of a label on another form. The code that I have works, but what gets displayed on form2.label is the text of form1.textBox at Design Time. Here is what I got. /// Code on the first form. private void button1_Click(object sender, System.EventArgs e) { Form2 form2 = new Form2(); form2.Show(); } /// Code on the second form. private void button1_Click(object sender, System.EventArgs e) { Form1 form1 = new Form1(); label1.Text = form1.textBox1.Text; }

    T L 2 Replies Last reply
    0
    • K kornstyle

      I know that this is extremely simple. I want to display the text of a textBox on one form on the text of a label on another form. The code that I have works, but what gets displayed on form2.label is the text of form1.textBox at Design Time. Here is what I got. /// Code on the first form. private void button1_Click(object sender, System.EventArgs e) { Form2 form2 = new Form2(); form2.Show(); } /// Code on the second form. private void button1_Click(object sender, System.EventArgs e) { Form1 form1 = new Form1(); label1.Text = form1.textBox1.Text; }

      T Offline
      T Offline
      tdciDoug
      wrote on last edited by
      #2

      put a function in form2 to get your value, and then call it from form1. Set your label there. Have a wonderful evening Doug Wright Developer, TDCI

      1 Reply Last reply
      0
      • K kornstyle

        I know that this is extremely simple. I want to display the text of a textBox on one form on the text of a label on another form. The code that I have works, but what gets displayed on form2.label is the text of form1.textBox at Design Time. Here is what I got. /// Code on the first form. private void button1_Click(object sender, System.EventArgs e) { Form2 form2 = new Form2(); form2.Show(); } /// Code on the second form. private void button1_Click(object sender, System.EventArgs e) { Form1 form1 = new Form1(); label1.Text = form1.textBox1.Text; }

        L Offline
        L Offline
        Luis Alonso Ramos
        wrote on last edited by
        #3

        kornstyle wrote: private void button1_Click(object sender, System.EventArgs e) { Form1 form1 = new Form1(); label1.Text = form1.textBox1.Text; } You are creating a new form everytime you try to get the text (but you never make it visible). That's why you get the text set at design time. You need to somehow get a reference to the existing Form1.

        /// Code on the first form.
        
        private void button1_Click(object sender, System.EventArgs e)
        {
        Form2 form2 = new Form2(this);
        form2.Show();
        }
        
        
        /// Code on the second form.
        
        Form _form1 = null;
        public Form2(Form form1)
        {
            _form1 = form1;
        }
        private void button1_Click(object sender, System.EventArgs e)
        {
        label1.Text = _form1.textBox1.Text;
        } 
        

        -- LuisR


        Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

        K 1 Reply Last reply
        0
        • L Luis Alonso Ramos

          kornstyle wrote: private void button1_Click(object sender, System.EventArgs e) { Form1 form1 = new Form1(); label1.Text = form1.textBox1.Text; } You are creating a new form everytime you try to get the text (but you never make it visible). That's why you get the text set at design time. You need to somehow get a reference to the existing Form1.

          /// Code on the first form.
          
          private void button1_Click(object sender, System.EventArgs e)
          {
          Form2 form2 = new Form2(this);
          form2.Show();
          }
          
          
          /// Code on the second form.
          
          Form _form1 = null;
          public Form2(Form form1)
          {
              _form1 = form1;
          }
          private void button1_Click(object sender, System.EventArgs e)
          {
          label1.Text = _form1.textBox1.Text;
          } 
          

          -- LuisR


          Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

          K Offline
          K Offline
          kornstyle
          wrote on last edited by
          #4

          Got it. Thanks a lot.

          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