-1

I'm new in C# programming. I have a beginner level question: How do I change the text property of the textbox1 in my form 2 object using a button in my form1?

Here's my code in form1:

namespace DoubleForms
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show();
        }
    }
}

This is in form2:

namespace DoubleForms
{
    public partial class Form2 : Form
    {


        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 frm1 = new Form1();
            frm1.textBox1.Text = "Test";

        }
    }
}
Sam
  • 1
  • 1
  • 3
  • 1
    Possible duplicate of [How to update textbox in form1 from form2?](http://stackoverflow.com/questions/7969582/how-to-update-textbox-in-form1-from-form2) – raidensan Apr 23 '16 at 06:29

4 Answers4

1

When you add a text box or any control for that matter to a Winform using the controls toolbox the control gets added as private so it can't be accessed outside of the class it's created in. Easy enough to fix though just added a public property that lets you get and set the text box value as such

namespace DoubleForms
{
    public partial class Form1 : Form
    {
        // NEW CODE
        public string TextBoxText 
        { 
            get { return this.textBox1.Text; }
            set { this.textBox1.Text = value; }
         }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show();
        }
    }
}

Then from Form2 you can just call form1.TextBoxText = "blah blah" to set the value.

Dmitry K.
  • 972
  • 6
  • 16
0

Code is creating new Form1 every-time you click the button, which is not you want I believe.

What you need to do is create an event in Form2 and then subscribe to that event in Form1, that way you can listen changes from Form2 and update Form1.

namespace DoubleForms
{
    public partial class Form2 : Form
    {
        public event EventHandler Updated;  // define an event handler

        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
             if(Updated != null)
             {
                  Updated(sender, new EventArgs()); //Raise a change.
             }
        }
    }
}

Now in Form1 subscribe to Form2 event.

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

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Updated += (se,ev)=> textBox1.Text = "Test"; // update textbox
            frm2.Show();
        }
    }
}
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
0
 //this code worked for me
 //in form2 put following code prevent form from opening multiple times  
 public partial class Form2 : Form
     {
         public Form2()
         {
              InitializeComponent();
         }
        private static Form2 Instance;
        public static Form2 GetInstance()
            {
                if (Instance ==null || Instance.IsDisposed)
           {
                Instance = new Form2();
            }
            else
             {
                Instance.BringToFront();
            }
                  return Instance;
         }

  // in form1

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


         private void Button2_Click(object sender, EventArgs e)
         {
             Form2 form2 = Form2.GetInstance();
             form2.textBox1.Text = textBox1.Text;
            form2.Show();
         }
    }
lee
  • 1
  • 2
0
 //this code worked for me
 //in form2 put following code prevent form from opening multiple times  
 public partial class Form2 : Form
     {
        public Form2()
        {
         InitializeComponent();
        }
        private static Form2 Instance;
        public static Form2 GetInstance()
            {
                if (Instance ==null || Instance.IsDisposed)
           {
                Instance = new Form2();
            }
             else
            {
                 Instance.BringToFront();
            }
                 return Instance;
         }

  // in form1

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


         private void Button2_Click(object sender, EventArgs e)
        {
            Form2 form2 = Form2.GetInstance();
            form2.textBox1.Text = textBox1.Text;
            form2.Show();
        }


     }
lee
  • 1
  • 2