从一个文本框中获取文本,并将其插入其他表单的文本框中

时间:2022-11-23 13:10:31

I'm very new to C# language, so please take me easy. What I want to ask is quite simple, but being new, I don't know how to do it.

我对C#语言很陌生,所以请带我一点儿。我想问的很简单,但是新的,我不知道怎么做。

I have 2 forms: Form1 and Form2. Form1 is the "default" one, the one you have when you open the application. I have 2 textboxes in the second form and two buttons (ok and cancel). In the first form I have a button which opens the 2nd form when you click on it and a textbox. I tried to get the text from those 2 textboxes in form 2 and to put it in the textbox from form1, but I didn't managed to do it. I want when I click ok in the second form, the text from those 2 textboxes in form 2 to be put in the textbox from form1 and when I click cancel, to just simply close form2. Can you help me?

我有两种形式:Form1和Form2。 Form1是“默认”的,是您打开应用程序时的默认值。我在第二种形式有两个文本框和两个按钮(确定和取消)。在第一个表单中,我有一个按钮,当您单击它和文本框时打开第二个表单。我试图从表格2中的那两个文本框中获取文本并将其放在form1的文本框中,但我没有设法做到这一点。我希望当我在第二个表单中单击“确定”时,表单2中的两个文本框中的文本将放入form1的文本框中,当我单击“取消”时,只需关闭form2即可。你可以帮我吗?

2 个解决方案

#1


1  

You could create a public property in Form2 that is set by Form1 when you press your button.

您可以在Form2中创建一个公共属性,当您按下按钮时,该属性由Form1设置。

public string TextValueFromForm1 { get; set; }

On the Form Load event, you could then, set the value of your text box to that of the property.

在Form Load事件上,您可以将文本框的值设置为属性的值。

Example of Form 2

表格2的示例

public class Form2 : Form
{
    private TextBox textBox1;
    private TextBox textBox2;

    public string TextValue1 { get; set; }
    public string TextValue2 { get; set; }

    public Form2()
    {
        this.Load += new EventHandler((object sender, EventArgs e) =>
        {
            textBox1.Text = TextValue1;
            textBox2.Text = TextValue2;
        });
    }
}

#2


0  

If I understand your question:
- Form2 has 2 TextBoxes (textBox1 and textBox2) and 2 buttons (btnOK and btnCancel)
- If btnOK is pressed - concatinate values of textBox1 and textBox2 and pass them to Form1
- If btnCancel is pressed - do not pass any data

如果我理解你的问题: - Form2有2个TextBoxes(textBox1和textBox2)和2个按钮(btnOK和btnCancel) - 如果按下btnOK - 结合textBox1和textBox2的值并将它们传递给Form1 - 如果按下btnCancel - 不要通过任何数据

Brief description of my answer:
It can be easily achieved with event handlers, just hook up to OnFormClosing event and read data from predefined property of Form2

我的回答简要说明:使用事件处理程序可以轻松实现,只需连接到OnFormClosing事件并从Form2的预定义属性中读取数据

Some code to illustrate my answer is below

一些代码来说明我的答案如下

Form1.cs

Form1.cs的

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

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

        void frm2_FormClosing(object sender, FormClosingEventArgs e)
        {
            if ((sender as Form2).textData != null)
                textBox1.Text = (sender as Form2).textData;
        }
    }

Form2.cs

Form2.cs

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

        private void btnOK_Click(object sender, EventArgs e)
        {
            textData = textBox1.Text + " " + textBox2.Text;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

#1


1  

You could create a public property in Form2 that is set by Form1 when you press your button.

您可以在Form2中创建一个公共属性,当您按下按钮时,该属性由Form1设置。

public string TextValueFromForm1 { get; set; }

On the Form Load event, you could then, set the value of your text box to that of the property.

在Form Load事件上,您可以将文本框的值设置为属性的值。

Example of Form 2

表格2的示例

public class Form2 : Form
{
    private TextBox textBox1;
    private TextBox textBox2;

    public string TextValue1 { get; set; }
    public string TextValue2 { get; set; }

    public Form2()
    {
        this.Load += new EventHandler((object sender, EventArgs e) =>
        {
            textBox1.Text = TextValue1;
            textBox2.Text = TextValue2;
        });
    }
}

#2


0  

If I understand your question:
- Form2 has 2 TextBoxes (textBox1 and textBox2) and 2 buttons (btnOK and btnCancel)
- If btnOK is pressed - concatinate values of textBox1 and textBox2 and pass them to Form1
- If btnCancel is pressed - do not pass any data

如果我理解你的问题: - Form2有2个TextBoxes(textBox1和textBox2)和2个按钮(btnOK和btnCancel) - 如果按下btnOK - 结合textBox1和textBox2的值并将它们传递给Form1 - 如果按下btnCancel - 不要通过任何数据

Brief description of my answer:
It can be easily achieved with event handlers, just hook up to OnFormClosing event and read data from predefined property of Form2

我的回答简要说明:使用事件处理程序可以轻松实现,只需连接到OnFormClosing事件并从Form2的预定义属性中读取数据

Some code to illustrate my answer is below

一些代码来说明我的答案如下

Form1.cs

Form1.cs的

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

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

        void frm2_FormClosing(object sender, FormClosingEventArgs e)
        {
            if ((sender as Form2).textData != null)
                textBox1.Text = (sender as Form2).textData;
        }
    }

Form2.cs

Form2.cs

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

        private void btnOK_Click(object sender, EventArgs e)
        {
            textData = textBox1.Text + " " + textBox2.Text;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }