C# 将一些信息转移到以前打开的表单
我有两张表格C# 将一些信息转移到以前打开的表单,c#,.net,winforms,C#,.net,Winforms,我有两张表格form1和form2我通过按钮打开form2,然后添加一些信息 private void button1_Click(object sender, EventArgs e) { string Name = TxtNewName.Text; string City = TxtNewCity.Text } 我正在由构造器打开表格 private void openForm2_Click(object sender, Eve
form1
和form2
我通过按钮打开form2
,然后添加一些信息
private void button1_Click(object sender, EventArgs e)
{
string Name = TxtNewName.Text;
string City = TxtNewCity.Text
}
我正在由构造器打开表格
private void openForm2_Click(object sender, EventArgs e)
{
Form2 newform = new Form2();
newform.Show();
}
我想将此名称
和城市
变量转移到以前打开的表单中。当我关闭此变量时,这两个值在已打开的表单中更新,表单1中的字段名称相同
请帮助我以第二种形式定义2个公共属性
public string GetName { get {return TxtNewName.Text;} }
public string GetCity { get {return TxtNewCity.Text;} }
调用第二个表单后,您可以访问它们
Form2 form2 = new Form2();
form2.ShowDialog();
string name = form2.GetName;
string city = form2.GetCity;
编辑:
…如果我想在关闭form2后直接设置form1中带有名称和城市的文本框的文本属性
this.Text = form2.GetName;
this.city = form2.GetCity;
在第二个表单中定义2个公共属性
public string GetName { get {return TxtNewName.Text;} }
public string GetCity { get {return TxtNewCity.Text;} }
调用第二个表单后,您可以访问它们
Form2 form2 = new Form2();
form2.ShowDialog();
string name = form2.GetName;
string city = form2.GetCity;
编辑:
…如果我想在关闭form2后直接设置form1中带有名称和城市的文本框的文本属性
this.Text = form2.GetName;
this.city = form2.GetCity;
您应该公开包含form2中的值的属性
public string Name { get { return TxtNewName.Text; } }
public string City { get { return TxtNewCity.Text; } }
而且
private void openForm2_Click(object sender, EventArgs e)
{
Form2 newform = new Form2();
newform.ShowDialog();
var city = newform.City;
var name = newform.Name;
}
您应该公开包含form2中的值的属性
public string Name { get { return TxtNewName.Text; } }
public string City { get { return TxtNewCity.Text; } }
而且
private void openForm2_Click(object sender, EventArgs e)
{
Form2 newform = new Form2();
newform.ShowDialog();
var city = newform.City;
var name = newform.Name;
}
因为需要将值传递给第一个表单,所以我建议使用参数构造函数来获取第一个表单 设置所需的属性,以获取传递给
Form1
Form2 newform = new Form2(this); // passing instance of Form1
newform.Show();
现在您可以访问
Form1
并分配所需的值。因为您需要将值传递给第一个表单,我建议使用参数构造函数来获取第一个表单
设置所需的属性,以获取传递给Form1
Form2 newform = new Form2(this); // passing instance of Form1
newform.Show();
现在您可以访问
Form1
并分配所需的值。您正在使用winform吗?你是怎么打电话/开表格的是的,我在工作winform@V4Vendetta查看更新的que。您正在使用winform吗?你是怎么打电话/开表格的是的,我在工作winform@V4Vendetta请参阅更新的que。这是否有效?它将如何到达下一行this.Text=form2.GetName
除非您关闭表单,否则实际上是他要求的“当我关闭此表单时,我想将此名称和城市变量转移到以前打开的表单”可以工作,但我直接使用Textboxname.Text
属性。这行吗?它将如何到达下一行this.Text=form2.GetName
除非您关闭表单,否则实际上是他要求的“当我关闭此表单时,我想将此名称和城市变量转移到以前打开的表单”可以正常工作,但我直接使用Textboxname.Text
属性。