如何从C#中的窗体返回值?
我有一个主窗体(我们称之为frmHireQuote),它是主MDI窗体(frmMainMDI)的子窗体,当单击按钮时,它通过ShowDialog()显示另一个窗体(frmImportContact) 当用户单击frmImportContact上的“OK”时,我想将一些字符串变量传递回frmHireQuote上的一些文本框 请注意,可能有多个frmHireQuote实例,返回调用此frmImportContact实例的实例显然很重要如何从C#中的窗体返回值?,c#,.net,winforms,parameter-passing,C#,.net,Winforms,Parameter Passing,我有一个主窗体(我们称之为frmHireQuote),它是主MDI窗体(frmMainMDI)的子窗体,当单击按钮时,它通过ShowDialog()显示另一个窗体(frmImportContact) 当用户单击frmImportContact上的“OK”时,我想将一些字符串变量传递回frmHireQuote上的一些文本框 请注意,可能有多个frmHireQuote实例,返回调用此frmImportContact实例的实例显然很重要 这样做的最佳方法是什么?在子表单上创建一些公共属性 public
这样做的最佳方法是什么?在子表单上创建一些公共属性
public string ReturnValue1 {get;set;}
public string ReturnValue2 {get;set;}
然后在子表单中设置该属性确定按钮单击处理程序
private void btnOk_Click(object sender,EventArgs e)
{
this.ReturnValue1 = "Something";
this.ReturnValue2 = DateTime.Now.ToString(); //example
this.DialogResult = DialogResult.OK;
this.Close();
}
然后,在您的frmHireQuote表单中,当您打开子表单时
using (var form = new frmImportContact())
{
var result = form.ShowDialog();
if (result == DialogResult.OK)
{
string val = form.ReturnValue1; //values preserved after close
string dateString = form.ReturnValue2;
//Do something here with these values
//for example
this.txtSomething.Text = val;
}
}
另外,如果您希望取消子窗体的设置,您只需在窗体中添加一个按钮,并将其设置为“取消”,还可以将窗体的属性设置为所述按钮-这将使escape键取消窗体。我通常在窗体/对话框上创建一个静态方法,我可以调用该方法。这将返回成功(确定按钮)或失败,以及需要填写的值
public class ResultFromFrmMain {
public DialogResult Result { get; set; }
public string Field1 { get; set; }
}
在表格上:
public static ResultFromFrmMain Execute() {
using (var f = new frmMain()) {
var result = new ResultFromFrmMain();
result.Result = f.ShowDialog();
if (result.Result == DialogResult.OK) {
// fill other values
}
return result;
}
}
呼唤你的形式
public void MyEventToCallForm() {
var result = frmMain.Execute();
if (result.Result == DialogResult.OK) {
myTextBox.Text = result.Field1; // or something like that
}
}
发现此代码的另一个小问题。。。或者至少当我尝试实现它时,它是有问题的 frmMain中的按钮不返回兼容的值,使用VS2010我添加了以下内容,一切都开始正常工作
public static ResultFromFrmMain Execute() {
using (var f = new frmMain()) {
f.buttonOK.DialogResult = DialogResult.OK;
f.buttonCancel.DialogResult = DialogResult.Cancel;
var result = new ResultFromFrmMain();
result.Result = f.ShowDialog();
if (result.Result == DialogResult.OK) {
// fill other values
}
return result;
}
}
添加两个按钮值后,对话框工作得很好!
感谢这个例子,它真的很有帮助。我只是通过引用将一些东西放入构造函数中,这样子窗体可以更改其值,主窗体可以从子窗体中获取新的或修改的对象。我非常使用MDI,我更喜欢它(可以使用它的地方)而不是多个浮动窗体 但要想从中获得最好的效果,你需要抓住自己的事情。它让你的生活变得轻松多了 一个简单的例子 有自己的中断类型
//Clock, Stock and Accoubts represent the actual forms in
//the MDI application. When I have multiple copies of a form
//I also give them an ID, at the time they are created, then
//include that ID in the Args class.
public enum InteruptSource
{
IS_CLOCK = 0, IS_STOCKS, IS_ACCOUNTS
}
//This particular event type is time based,
//but you can add others to it, such as document
//based.
public enum EVInterupts
{
CI_NEWDAY = 0, CI_NEWMONTH, CI_NEWYEAR, CI_PAYDAY, CI_STOCKPAYOUT,
CI_STOCKIN, DO_NEWEMAIL, DO_SAVETOARCHIVE
}
然后您自己的Args类型
public class ControlArgs
{
//MDI form source
public InteruptSource source { get; set; }
//Interrupt type
public EVInterupts clockInt { get; set; }
//in this case only a date is needed
//but normally I include optional data (as if a C UNION type)
//the form that responds to the event decides if
//the data is for it.
public DateTime date { get; set; }
//CI_STOCKIN
public StockClass inStock { get; set; }
}
然后在命名空间内但在类之外使用委托
namespace MyApplication
{
public delegate void StoreHandler(object sender, ControlArgs e);
public partial class Form1 : Form
{
//your main form
}
现在,手动或使用GUI,让MDI父窗体响应子窗体的事件
但是使用owr参数,可以将其简化为单个函数。您可以设置中断,这有利于调试,但也可以用于其他方面
只要让所有MDI父事件代码指向一个函数
calendar.Friday += new StoreHandler(MyEvents);
calendar.Saturday += new StoreHandler(MyEvents);
calendar.Sunday += new StoreHandler(MyEvents);
calendar.PayDay += new StoreHandler(MyEvents);
calendar.NewYear += new StoreHandler(MyEvents);
一个简单的切换机制通常足以将事件传递到适当的表单。如果您想将数据从
form1
传递到form2
而不想像新的表单一样传递(sting“data”)代码>
你喜欢吗
表格一
using (Form2 form2= new Form2())
{
form2.ReturnValue1 = "lalala";
form2.ShowDialog();
}
在表格2中添加
public string ReturnValue1 { get; set; }
private void form2_Load(object sender, EventArgs e)
{
MessageBox.Show(ReturnValue1);
}
如果您想交换form1
中的某些内容,也可以像这样使用form1
中的值
刚刚好
textbox.Text =form2.ReturnValue1
首先,您必须在form2(子级)中定义属性。您将在form2中更新此属性,也将从form1(父级)更新此属性:
从form1(父级)调用form2(子级):
我在设置值的表单中引发事件,并在需要处理值更改的表单中订阅该事件。委托是将数据从一个表单发送到另一个表单的最佳选项
public partial class frmImportContact : Form
{
public delegate void callback_data(string someData);
public event callback_data getData_CallBack;
private void button_Click(object sender, EventArgs e)
{
string myData = "Top Secret Data To Share";
getData_CallBack(myData);
}
}
public partial class frmHireQuote : Form
{
private void Button_Click(object sender, EventArgs e)
{
frmImportContact obj = new frmImportContact();
obj.getData_CallBack += getData;
}
private void getData(string someData)
{
MessageBox.Show("someData");
}
}
也使用了这种方法,效果很好。然而,在result类前面加上frm
可能会导致混淆(我读到它时也是这样做的)…我只是重复使用了问题中的名称(+快速键入)。我会改个更好的名字。有几个重要的缺点。未释放使用ShowDialog()显示的表单。您必须在此处使用using语句。ShowDialog()返回DialogResult值,不要忽略它,否则当用户取消对话框时,您将尝试使用对话框属性。干杯,我已经解释了。迷你评论:集合需要附加分号。看起来像这样。不需要关闭()?不管怎么说,它是在没有它们的情况下关闭的!您能否详细说明一下您的答案,并可能提供一个代码示例来帮助未来的用户?
using (Form2 formObject= new Form2() )
{
formObject.ShowDialog();
string result = formObject.Response;
//to update response of form2 after saving in result
formObject.Response="";
// do what ever with result...
MessageBox.Show("Response from form2: "+result);
}
public partial class frmImportContact : Form
{
public delegate void callback_data(string someData);
public event callback_data getData_CallBack;
private void button_Click(object sender, EventArgs e)
{
string myData = "Top Secret Data To Share";
getData_CallBack(myData);
}
}
public partial class frmHireQuote : Form
{
private void Button_Click(object sender, EventArgs e)
{
frmImportContact obj = new frmImportContact();
obj.getData_CallBack += getData;
}
private void getData(string someData)
{
MessageBox.Show("someData");
}
}