2013-09-16 35 views

回答

0

如果你总是使用形式为模式的形式,你可以使用类似的模式这个。

class FormResult 
    { 
     public DialogResult dr {get; private set;} 
     public string LastName {get; private set;} 
     public string FirstName {get; private set;} 
    } 

    class MyForm : whatever 
    { 
     static public FormResult Exec(string parm1, string parm2) 
{ 
     var result = new FormResult(); 
     var me = new MyForm(); 
     me.parm1 = parm1; 
     me.parm2 = parm2; 
     result.dr = me.ShowDialog(); 
     if (result.dr == DialogResult.OK) 
     { 
     result.LastName = me.LastName; 
     result.FirstName = me.FirstName; 
     } 
     me.Close(); // should use try/finally or using clause 
     return result; 
    } 
} 

... rest of MyForm 

这种模式在隔离您使用窗体的“私有”数据的途径,并且可以很容易地 如果您决定添加MORS返回值延长。如果您有更多的一对夫妇的输入参数,你可以捆绑它们放到一个类,并通过该类的实例来Exec方法

0

只是通过它通过在每个级别使用特性:

//Form1 needs a property you can access 
public class Form1 
{ 
    private String _myString = null; 
    public String MyString { get { return _myString; } } 

    //change the constructor to take a String input 
    public Form1(String InputString) 
    { 
     _myString = InputString; 
    } 
    //...and the rest of the class as you have it now 
} 

public class Form2 
{ 
    private String _myString = null; 
    public String MyString { get { return _myString; } } 

    //same constructor needs... 
    public Form2(String InputString) 
    { 
     _myString = InputString; 
    } 
} 

最终,您的呼叫将变为:

String strToPassAlong = "This is the string"; 

Form1 f1 = new Form1(strToPassAlong); 
f1.MdiParent = this; 
f1.Show(); 

Form2 f2= new Form2(f1.MyString); //or this.MyString, if Form2 is constructed by Form1's code 
f2.ShowDialog(); 

现在,沿途的每个表格都有您传递的字符串的副本。

相关问题