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. Update TextBox from second Form

Update TextBox from second Form

Scheduled Pinned Locked Moved C#
questionhelpannouncement
5 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.
  • W Offline
    W Offline
    wi5nia
    wrote on last edited by
    #1

    hi, I have and application which has 2 forms. The firts form has a textbox and a button which clicked opens a second form, which has just one button which clicked calls a method in the first form. The problem is, that the textbox doesn't update. There are no excpetion, when I set a break point the program goes into the line which updates the textbox but nothing happens. What is wrong?

    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

        public void add()
        {
            textBox1.Text = "Test";
        }
    
        private void button1\_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
        }
    }
    

    public partial class Form2 : Form
    {
    public Form2()
    {
    InitializeComponent();
    }

        private void button1\_Click(object sender, EventArgs e)
        {
            Form1 f1 = new Form1();
            f1.add();
        }
    }
    
    J D 2 Replies Last reply
    0
    • W wi5nia

      hi, I have and application which has 2 forms. The firts form has a textbox and a button which clicked opens a second form, which has just one button which clicked calls a method in the first form. The problem is, that the textbox doesn't update. There are no excpetion, when I set a break point the program goes into the line which updates the textbox but nothing happens. What is wrong?

      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      }

          public void add()
          {
              textBox1.Text = "Test";
          }
      
          private void button1\_Click(object sender, EventArgs e)
          {
              Form2 f2 = new Form2();
              f2.Show();
          }
      }
      

      public partial class Form2 : Form
      {
      public Form2()
      {
      InitializeComponent();
      }

          private void button1\_Click(object sender, EventArgs e)
          {
              Form1 f1 = new Form1();
              f1.add();
          }
      }
      
      J Offline
      J Offline
      Jason C Bourne
      wrote on last edited by
      #2

      You are instantianting a new Form1, this is not the existing Form1 that opened your second form. When the first form opens the second, it should provide a reference to itself that you can then use in the second form. Optionally, you can check the Owner/ParentForm properties or something like that, depending on how you show the second form. public partial class Form2 : Form { private Form1 _parentForm; public Form2() { InitializeComponent(); } public Form2(Form1 parentForm) { InitializeComponent(); this._parentForm = parentForm; } private void button1_Click(object sender, EventArgs e) { if (this._parentForm != null) { this._parentForm.Add(); } } In this example, your Form1 code is supposed to do something like: Form2 form = new Form2(this); form.Show();

      Jean-Christophe Grégoire

      D 1 Reply Last reply
      0
      • W wi5nia

        hi, I have and application which has 2 forms. The firts form has a textbox and a button which clicked opens a second form, which has just one button which clicked calls a method in the first form. The problem is, that the textbox doesn't update. There are no excpetion, when I set a break point the program goes into the line which updates the textbox but nothing happens. What is wrong?

        public partial class Form1 : Form
        {
        public Form1()
        {
        InitializeComponent();
        }

            public void add()
            {
                textBox1.Text = "Test";
            }
        
            private void button1\_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2();
                f2.Show();
            }
        }
        

        public partial class Form2 : Form
        {
        public Form2()
        {
        InitializeComponent();
        }

            private void button1\_Click(object sender, EventArgs e)
            {
                Form1 f1 = new Form1();
                f1.add();
            }
        }
        
        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        This should really be done with a delegate/event. I prefer using events for stuff like this. Form2 needs a new event that it can raise, and make you Form1 instance subscribe to it after it has instanciated it. If using an event you'll need a class derived from EventArgs to pass the data back. This is the sort of idea.

        // Form1
        using System;
        using System.Windows.Forms;

        namespace WindowsFormsApplication1
        {
        public partial class Form1 : Form
        {
        public Form1()
        {
        InitializeComponent();
        }

            private void button1\_Click(object sender, EventArgs e)
            {
                Form2 frm2 = new Form2();
                frm2.ReadyToUpdate += new EventHandler<Form2.MyEventArgs>(frm2\_ReadyToUpdate);
                frm2.Show();
            }
        
            void frm2\_ReadyToUpdate(object sender, Form2.MyEventArgs e)
            {
                textBox1.Text = e.Text;
            }
        }
        

        }

        //Form2
        using System;
        using System.Windows.Forms;

        namespace WindowsFormsApplication1
        {
        public partial class Form2 : Form
        {
        public event EventHandler<MyEventArgs> ReadyToUpdate;
        public Form2()
        {
        InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
        OnReadyToUpdate(new MyEventArgs("Test"));
        Close();
        }
        protected virtual void OnReadyToUpdate(MyEventArgs e)
        {
        EventHandler<MyEventArgs> eh = ReadyToUpdate;
        if (eh != null)
        eh(this, e);
        }
        public class MyEventArgs : EventArgs
        {
        public MyEventArgs(string text)
        {
        m_Text = text;
        }
        private string m_Text;
        public string Text
        {
        get { return m_Text; }
        }
        }
        }
        }

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

        1 Reply Last reply
        0
        • J Jason C Bourne

          You are instantianting a new Form1, this is not the existing Form1 that opened your second form. When the first form opens the second, it should provide a reference to itself that you can then use in the second form. Optionally, you can check the Owner/ParentForm properties or something like that, depending on how you show the second form. public partial class Form2 : Form { private Form1 _parentForm; public Form2() { InitializeComponent(); } public Form2(Form1 parentForm) { InitializeComponent(); this._parentForm = parentForm; } private void button1_Click(object sender, EventArgs e) { if (this._parentForm != null) { this._parentForm.Add(); } } In this example, your Form1 code is supposed to do something like: Form2 form = new Form2(this); form.Show();

          Jean-Christophe Grégoire

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

          Jean-Christophe Grégoire wrote:

          it should provide a reference to itself

          Although this works and is a common method - it should not be encouraged. Form2 shouldn't know anything about Form1 and no method on it should be tied to an instance of a control that it hasn't created itself. If it needs to broadcast information, it should use an event/delegate - in the same way that every other control does :)

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

          J 1 Reply Last reply
          0
          • D DaveyM69

            Jean-Christophe Grégoire wrote:

            it should provide a reference to itself

            Although this works and is a common method - it should not be encouraged. Form2 shouldn't know anything about Form1 and no method on it should be tied to an instance of a control that it hasn't created itself. If it needs to broadcast information, it should use an event/delegate - in the same way that every other control does :)

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

            J Offline
            J Offline
            Jason C Bourne
            wrote on last edited by
            #5

            I certainly do agree, I didn't want to bother him ;-)

            Jean-Christophe Grégoire

            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