简体   繁体   English

在 C# 中从 Form2 更改 Form1 的 TextBox 文本

[英]Changing TextBox Text of Form1 From Form2 in C#

I'm new in C# programming.我是 C# 编程的新手。 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?我有一个初学者级别的问题:如何使用 form1 中的按钮更改 form 2 对象中 textbox1 的文本属性?

Here's my code in form1:这是我在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:这是在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";

        }
    }
}

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当您使用控件工具箱将文本框或任何与此相关的控件添加到 Winform 时,该控件将被添加为私有,因此无法在创建它的类之外访问它。虽然只是添加了一个公共属性,但很容易修复允许您获取和设置文本框值

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.然后从 Form2 您可以调用 form1.TextBoxText = "blah blah" 来设置值。

Code is creating new Form1 every-time you click the button, which is not you want I believe.每次单击按钮时,代码都会创建新的Form1 ,我相信这不是您想要的。

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 .您需要做的是在Form2创建一个事件,然后在Form1订阅该事件,这样您就可以监听来自Form2更改并更新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.现在在Form1订阅Form2事件。

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();
        }
    }
}
 //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();
         }
    }
 //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();
        }


     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM