如何复制一个文本框的值从Form1以窗体2?窗体、文本框

2023-09-02 01:37:32 作者:劳资的心禁止访问

我有Form1中,有一个文本框和一个按钮。当用户点击按钮 Form1中窗体2 一个标签控件,承载着 Form1中。

I have Form1 that has a textbox and a button. When user clicks the button in Form1, Form2 opens up with a label control that carries the value of textbox in Form1.

我所做的是设置 Form1中的文本修改为公开,但是当我打电话的文本框的名字 Form1中窗体2 ,我得到一个错误,指出

What i did is set the textbox modifier of Form1 to Public, but when I call the textbox name of Form1 in Form2, I get an error that says

名为txtbx1并不在当前上下文中存在

The name "txtbx1" doesn't exist in the current context

我不知道为什么,因为我已经设置 txtbx1 的修改,以公开

I wonder why since I already set the modifier of txtbx1 to Public.

快速注:我试图在窗体2 Form1的实例为:

Quick Note: i tried to instantiate Form1 in Form2 as:

Form1 f1 = new Form1();

,然后调用

f1.txtbx1.text

奇怪的是Form1中不能被实例化(不高亮发生)。在另一方面,如果我做的窗体2 F2 =新的窗体2();!窗体2被突出显示

这是我如何显示窗体2从Form1的:

This is how i show Form2 from Form1:

        SetSalary salForm = new SetSalary();
        salForm.ShowDialog();

需要注意的是SetSalary重新presents窗体2。

Note that SetSalary represents Form2.

任何帮助将AP preciated。

Any help will be appreciated.

推荐答案

做一个构造窗口2 接受字符串,并呼吁新的窗口2 通过 form1.frm1Textbox.text 来构造器,然后将其设置为 form2.frm2Textbox.text

make a constructor for form2 that accept string and in calling new form2 pass form1.frm1Textbox.text to contructor then set it to form2.frm2Textbox.text

Form2 form = new Form2(frm1Textbox.text);

在窗口2构造

public class Form2 : Form
{
    public Form2(string text)
    {
        frm2Textbox.Text = text; 
    }
}