在表单关闭时传递值 [英] Pass value on form close
问题描述
你好每个
i有两种形式形式A 我有一个带文本框的按钮,打开另一个带有网格视图的表单,我在其中添加一些值并得到这些值的总和在文本框中我需要将表格B中的文本框总和支持到文本框中。怎么做?我使用c#。
i尝试一下代码,但是我收到了这个值的形式A
我尝试了什么:
this.ReturnValue1 = txttotal.Text;
// this.ReturnValue2 = DateTime.Now.ToString(); //示例
this.DialogResult = DialogResult.OK;
this.Close();
是的 - 有两种方法,具体取决于您希望它传回的时间。
如果您使用ShowDialog显示第二个表单,则在表单中添加一个属性并在表单后读回已关闭。
如果您想在不关闭孩子的情况下更新主表格,请使用以下活动:在两种表格之间传递信息,第2部分:儿童到父母 [ ^ ]
试试这个,参考在线评论
Form2
public partial class Form2:表单
{
public string SumValue { get ; set ; } // 在表单2中创建一个属性
private void textBox1_TextChanged( object sender,EventArgs e) // 使用属性窗口中的TextChanged事件
{
SumValue = textBox1.Text;
}
Form1
public partial class Form1:表单
{
private void button1_Click( object sender,EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.ShowDialog();
this .textBox1.Text = objForm2.SumValue; // 从form2读取值
}
Hi every
i have two forms in form A i have a button with textbox which open another form with grid view where i add some values and get sum of these values in textbox i need to back textbox sum in form B to textbox In form A ?? how it can be done ? i use c# .
i try apiece of code but i receive this value in form A
What I have tried:
this.ReturnValue1 = txttotal.Text;
// this.ReturnValue2 = DateTime.Now.ToString(); //example
this.DialogResult = DialogResult.OK;
this.Close();
Yes - there are a couple of ways, depending on when you want it passed back.
If you use ShowDialog to display the second form, then add a property to the form and read it back after the form has closed.
If you want to update the "main" form without closing the child, use an event: Transferring information between two forms, Part 2: Child to Parent[^]
try this, refer in line comments
Form2
public partial class Form2 : Form { public string SumValue { get; set; } // create a property in form 2 private void textBox1_TextChanged(object sender, EventArgs e) // use TextChanged event from properties window { SumValue = textBox1.Text; }
Form1
public partial class Form1 : Form { private void button1_Click(object sender, EventArgs e) { Form2 objForm2 = new Form2(); objForm2.ShowDialog(); this.textBox1.Text = objForm2.SumValue; // read the value from form2 }
这篇关于在表单关闭时传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!