Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我处于不同的表单中时,如何从一个表单访问标签中的文本?_C#_Winforms - Fatal编程技术网

C# 当我处于不同的表单中时,如何从一个表单访问标签中的文本?

C# 当我处于不同的表单中时,如何从一个表单访问标签中的文本?,c#,winforms,C#,Winforms,在不同表单的上下文中,从一个表单访问标签的值时遇到问题。我可以访问第二个表单中的默认标签值,但是当我更改标签值并尝试发送到另一个表单时,我得到的是标签的默认值,而不是新值 格式1中的代码: public String text1 { get { return label2.Text; } set { label2.Text = value; } } 格式2的代码 private void button3_Click(object sender, Even

在不同表单的上下文中,从一个表单访问
标签的值时遇到问题。我可以访问第二个表单中的默认标签值,但是当我更改标签值并尝试发送到另一个表单时,我得到的是标签的默认值,而不是新值

格式1中的代码:

public String text1
{            
    get { return label2.Text; }
    set { label2.Text = value; }
}
格式2的代码

private void button3_Click(object sender, EventArgs e)
{
    Form1 frm = new Form1();
    MessageBox.Show("" + frm.text1);
}

创建第二个表单时,您需要提交对
Form1
的引用。比如:

// this is where you open Form2 from Form1
private void button_openForm2_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2(this); 
    f2.Show();
}
在表格2中,您有:

// declare this field in your Form2 class:
Form1 f1;

// this is your constructor in Form2
public Form2(Form1 f1)
{
    this.f1 = f1; // some field or property to hold Form1
    // possibly other work to do here
}

private void button3_Click(object sender, EventArgs e)
{
    MessageBox.Show("" + f1.text1);
}

另一种可能是将
text1
设置为静态。

在创建第二个表单时,您需要提交对
Form1
的引用。比如:

// this is where you open Form2 from Form1
private void button_openForm2_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2(this); 
    f2.Show();
}
在表格2中,您有:

// declare this field in your Form2 class:
Form1 f1;

// this is your constructor in Form2
public Form2(Form1 f1)
{
    this.f1 = f1; // some field or property to hold Form1
    // possibly other work to do here
}

private void button3_Click(object sender, EventArgs e)
{
    MessageBox.Show("" + f1.text1);
}

另一种可能是将
text1
设置为静态。

在创建第二个表单时,您需要提交对
Form1
的引用。比如:

// this is where you open Form2 from Form1
private void button_openForm2_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2(this); 
    f2.Show();
}
在表格2中,您有:

// declare this field in your Form2 class:
Form1 f1;

// this is your constructor in Form2
public Form2(Form1 f1)
{
    this.f1 = f1; // some field or property to hold Form1
    // possibly other work to do here
}

private void button3_Click(object sender, EventArgs e)
{
    MessageBox.Show("" + f1.text1);
}

另一种可能是将
text1
设置为静态。

在创建第二个表单时,您需要提交对
Form1
的引用。比如:

// this is where you open Form2 from Form1
private void button_openForm2_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2(this); 
    f2.Show();
}
在表格2中,您有:

// declare this field in your Form2 class:
Form1 f1;

// this is your constructor in Form2
public Form2(Form1 f1)
{
    this.f1 = f1; // some field or property to hold Form1
    // possibly other work to do here
}

private void button3_Click(object sender, EventArgs e)
{
    MessageBox.Show("" + f1.text1);
}
另一种可能是使
text1
静态。

试试这个

private void button3_Click(object sender, EventArgs e)
{
    Form frm = Application.OpenForms["Form1"];

    //Here textbox1 is the control name which has the value in Form1
    MessageBox.Show("" + frm .Controls["textbox1"].Text);
}
试试这个

private void button3_Click(object sender, EventArgs e)
{
    Form frm = Application.OpenForms["Form1"];

    //Here textbox1 is the control name which has the value in Form1
    MessageBox.Show("" + frm .Controls["textbox1"].Text);
}
试试这个

private void button3_Click(object sender, EventArgs e)
{
    Form frm = Application.OpenForms["Form1"];

    //Here textbox1 is the control name which has the value in Form1
    MessageBox.Show("" + frm .Controls["textbox1"].Text);
}
试试这个

private void button3_Click(object sender, EventArgs e)
{
    Form frm = Application.OpenForms["Form1"];

    //Here textbox1 is the control name which has the value in Form1
    MessageBox.Show("" + frm .Controls["textbox1"].Text);
}


顺便说一下:“+frm.text1与frm.text1BTW相同:”+frm.text1与frm.text1BTW相同:“+frm.text1与frm.text1BTW相同:“+frm.text1与frm.text1相同,我应在何处写入表格2 f2=新表格2(此);在表格一?在表格2中,我得到了一些错误。f1=f1;我想你应该在
Form1
的某个地方打开你的
Form2
?那就是你写的
form2f2=newform2(这个)
Form2
中,您需要为
Form1
声明一个字段。我将用例子更新我的答案,只需一秒。Form2 tambahData=newform2(this);当我写“this”->form2(this)时显示错误;你有什么错误?你在
Form2
中有接受正确类型参数的构造函数吗?构造函数,我想我没有,我应该在哪里写Form2 f2=new Form2(这个);在表格一?在表格2中,我得到了一些错误。f1=f1;我想你应该在
Form1
的某个地方打开你的
Form2
?那就是你写的
form2f2=newform2(这个)
Form2
中,您需要为
Form1
声明一个字段。我将用例子更新我的答案,只需一秒。Form2 tambahData=newform2(this);当我写“this”->form2(this)时显示错误;你有什么错误?你在
Form2
中有接受正确类型参数的构造函数吗?构造函数,我想我没有,我应该在哪里写Form2 f2=new Form2(这个);在表格一?在表格2中,我得到了一些错误。f1=f1;我想你应该在
Form1
的某个地方打开你的
Form2
?那就是你写的
form2f2=newform2(这个)
Form2
中,您需要为
Form1
声明一个字段。我将用例子更新我的答案,只需一秒。Form2 tambahData=newform2(this);当我写“this”->form2(this)时显示错误;你有什么错误?你在
Form2
中有接受正确类型参数的构造函数吗?构造函数,我想我没有,我应该在哪里写Form2 f2=new Form2(这个);在表格一?在表格2中,我得到了一些错误。f1=f1;我想你应该在
Form1
的某个地方打开你的
Form2
?那就是你写的
form2f2=newform2(这个)
Form2
中,您需要为
Form1
声明一个字段。我将用例子更新我的答案,只需一秒。Form2 tambahData=newform2(this);当我写“this”->form2(this)时显示错误;你有什么错误?
Form2
中是否有接受正确类型参数的构造函数?我想我没有该构造函数无法隐式将类型“System.Windows.Forms.Form”转换为“myproject”。存在显式转换(是否缺少转换?@user2029546该myproject是什么?项目中是否有来自命名表单的窗口?看起来错误来自于您的其他代码。您正在执行无效的类型转换或赋值,能否粘贴发生错误的确切代码行?private void button3_Click(object sender,EventArgs e){Form1 frm=Application.OpenForms[“Form1”];MessageBox.Show(“+frm.teks1”);}无法将类型“System.Windows.Forms.Form”隐式转换为“myproject”。存在显式转换(是否缺少转换?@user2029546该myproject是什么?项目中是否有来自命名表单的窗口?看起来错误来自于您的其他代码。您正在执行无效的类型转换或赋值,能否粘贴发生错误的确切代码行?private void button3_Click(object sender,EventArgs e){Form1 frm=Application.OpenForms[“Form1”];MessageBox.Show(“+frm.teks1”);}无法将类型“System.Windows.Forms.Form”隐式转换为“myproject”。存在显式转换(是否缺少转换?@user2029546该myproject是什么?项目中是否有来自命名表单的窗口?看起来错误来自于您的其他代码。您正在执行无效的类型转换或赋值,能否粘贴发生错误的确切代码行?private void button3_Click(object sender,EventArgs e){Form1 frm=Application.OpenForms[“Form1”];MessageBox.Show(“+frm.teks1”);}无法将类型“System.Windows.Forms.Form”隐式转换为“myproject”。存在显式转换(是否缺少转换?@user2029546该myproject是什么?项目中是否有来自命名表单的窗口?看起来错误来自于您的其他代码。您正在执行无效的类型转换或赋值,是否可以粘贴