如何在C#中将值传递给正在运行的表单 [英] how can i pass value to a running form in C#
问题描述
我有两种形式:Form1和Form2
Form1开始运行Application.Run()
和Form1有一个datagridview如果用户开始编辑DataGridView
i运行Form2就像这样
Form2 addStock = new Form2(productNo ,stockString,this);
addStock.Activate();
addStock.Show();
在Form2完成任务后,当我单击表单2上的一个按钮时,我希望将值返回到Form1并显示在Form1上的DataGridView中并关闭Form2
i尝试过传递Form1的对象
Form2 addStock = new Form2(productNo,stockString,this);
但是在Form2上我无法访问Form1的DataGridview与该对象
我该怎么做才能完成这个?
Hi,
I have 2 forms : Form1 and Form2
Form1 start running with Application.Run()
and Form1 have a datagridview if user Start Edit the DataGridView
i run Form2 like this
Form2 addStock = new Form2(productNo, stockString, this);
addStock.Activate();
addStock.Show();
After Form2 finish the task, When i click a button on Form 2 i want to get value return to Form1 and show in the DataGridView on Form1 and close the Form2
i tried to pass the Form1's object
Form2 addStock = new Form2(productNo, stockString, this);
but on Form2 i can't access the Form1's DataGridview with that object
how can i do to accomplish this?
推荐答案
试试这个链接
在表单之间传递数据 [ ^ ]
http://dotprogramming.blogspot.in/2013/08/passing-values-from-datagridview.html [ ^ ]
< a href =http://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form-in-c-sharp-winforms-application> http://stackoverflow.com / questions / 1559770 / send-values-from-one-form-to-another-form-in-c-sharp-winforms-application [ ^ ]
这些链接将是defini tely给你一些想法。
希望这对你有帮助。
Try this links
Passing Data Between Forms[^]
http://dotprogramming.blogspot.in/2013/08/passing-values-from-datagridview.html[^]
http://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form-in-c-sharp-winforms-application[^]
These links will definitely give you some idea.
Hope this will help you.
由于Form2实例是从Form1创建的,最简单的方法是在Form2中创建一个属性检索数据。
然后你要么显示表格
1)ShowDialog - 表格显示,当它从OK按钮关闭或者你的Form1代码将继续从那一点开始并且可以获取数据:
Since the Form2 instance is created from Form1, the easiest way is to create a property in Form2 which retrieves the data.
You then either display the form with
1) ShowDialog - the form displays, and when it closes from the OK button or otherwise your Form1 code will continue from that point and can fetch the data:
Form2 f2 = new Form2()
if (f2.ShowDialog == DialogResult.OK)
{
string myData = f2.Data;
...
}
2)显示 - 显示表单,但您需要为FormClosed事件添加处理程序并检索数据那里:
2) Show - the form displays, but you need to add a handler to the FormClosed event and retrieve the data there:
Form2 f2 = new From2();
f2.FormClosed += new FormClosedEventHandler(ClosedForm);
f2.Show();
...
private void Closed(object sender, FormClosedEventArgs e)
{
Form2 f2 = sender as Form2;
if (f2 != null)
{
string myData = f2.Data;
...
}
}
in form1 set modifires for gridview = public
表示DataGridview>属性> modifires = public
in form1 set modifires for gridview = public
means DataGridview>properties>modifires=public
这篇关于如何在C#中将值传递给正在运行的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!