我有一个主窗体(让我们称之为frmHireQuote),这是一个主MDI窗体(frmMainMDI)的子窗体,当单击按钮时,通过ShowDialog()显示另一个窗体(frmImportContact)。

当用户单击frmImportContact上的“确定”时,我想将一些字符串变量传递回frmHireQuote上的一些文本框。

注意,frmHireQuote可能有多个实例,回到调用frmImportContact实例的实例显然很重要。

做这件事最好的方法是什么?


当前回答

我通常在form/dialog上创建一个静态方法,我可以调用它。这将返回成功(ok -按钮)或失败,以及需要填写的值。

 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
   }
}

其他回答

我通常在form/dialog上创建一个静态方法,我可以调用它。这将返回成功(ok -按钮)或失败,以及需要填写的值。

 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
   }
}

我在设置值的表单中引发一个事件,并在需要处理值更改的表单中订阅该事件。

我只是通过引用在构造函数中放入一些东西,所以子表单可以改变它的值,主表单可以从子表单中获得新的或修改过的对象。

如果你想从form1传递数据到form2,而不像new form那样传递(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中的value

刚刚成型

textbox.Text =form2.ReturnValue1

我经常使用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,让MDIparent响应子表单的事件。

但使用自己的Args,你可以将其简化为单个函数。您可以提供中断中断,这对调试很好,但在其他方面也很有用。

只要让你所有的mdiparent事件代码指向一个函数,

        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);

简单的切换机制通常足以将事件传递给适当的表单。