C# 将值从另一个窗体设置为对象
我正在用C语言编写一个WindowsFormProject,其中包含以下代码:C# 将值从另一个窗体设置为对象,c#,C#,我正在用C语言编写一个WindowsFormProject,其中包含以下代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.For
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
}
}
这是Form1的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.textBox1.Text = "prova";
f1.Refresh();
}
}
}
结束这是Form2的代码
目标是在表单1的文本框中写入文本,我将访问修饰符设置为public,但不起作用
有什么解决办法吗?试试这个
表格2:
public Form _parent;
public Form2(Form Parent)
{
InitializeComponent();
this._parent = Parent;
}
private void button1_Click(object sender, EventArgs e)
{
this._parent.textBox1.Text = "prova";
}
public Form1 _parent;
public Form2(Form1 parent) //<-- parent is the reference for the first form ("Form1" object)
{
InitializeComponent();
this._parent = parent;
}
private void button1_Click(object sender, EventArgs e)
{
this._parent.textBox1.Text = "prova"; // you won't get an exception here because _parant is from type Form1 and not Form
}
在表格1中:
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(this);
f.Show();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(this); //<- "this" is the reference for the current "Form1" object
f.Show();
}
请注意,在Form2中的“button1_Click”中,您正在创建一个“Form1”的新实例,因此Form2并不真正“知道”已准备好打开的Form1
您应该将此对象引用(Form1)传递给Form2,
与Marcio的解决方案类似,但您应该传递“Form1”,而不是传递继承类“Form”
表格2:
public Form _parent;
public Form2(Form Parent)
{
InitializeComponent();
this._parent = Parent;
}
private void button1_Click(object sender, EventArgs e)
{
this._parent.textBox1.Text = "prova";
}
public Form1 _parent;
public Form2(Form1 parent) //<-- parent is the reference for the first form ("Form1" object)
{
InitializeComponent();
this._parent = parent;
}
private void button1_Click(object sender, EventArgs e)
{
this._parent.textBox1.Text = "prova"; // you won't get an exception here because _parant is from type Form1 and not Form
}
public Form1\u家长;
public Form2(Form1 parent)//我认为@krillgar的可能重复项不是同一个问题我有以下错误:错误1'System.Windows.Forms.Form'不包含'textBox1'的定义,并且找不到接受'System.Windows.Forms.Form'类型的第一个参数的扩展方法'textBox1'(是否缺少using指令或程序集引用?)c:\users\cristian\documents\visual studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form2.cs 24 26 WindowsFormsApplication2