2011-02-25 133 views
1

我有一個c#窗體窗體應用程序,其中包含幾種形式。c#窗體窗體應用程序窗體問題

一般情況下,例如,在Form1,創建和窗口2的一個實例,然後

form1.hide(); 
form2.show(); 

但有時我想以前的形式來顯示和處理當前的形式。我怎樣才能打電話給以前的表格?

在此先感謝。

回答

4

要回答你的問題,你需要在你的視圖中保持對彼此的引用。雖然這可能工作,它是混亂和容易出錯。這聽起來像你所有的控制邏輯可能包含在你的表單類代碼中,我建議你遠離這個並分離你的關注點。

解決您的表單管理問題變得非常簡單,如果您創建一個控制器類,至少可以以您認爲合適的方式管理表單的創建和處理。

所以您的代碼示例實際上從一個控制器類推出,是這樣的:

public class FormsController 
{ 
    private Form form1 = new Form(); 
    private Form form2 = new Form(); 

    public void SwitchForms() 
    { 
     form1.hide(); 
     form2.show(); 
    } 
} 

爲了進一步薰陶結賬MVC architectural pattern與數據,BIZ邏輯和UI清潔工作。

+1

非常真實。我在Program.cs中處理這個問題的建議是MVC方法的一個非常非常簡單的版本,它是處理問題的更好方法。 – digitlworld 2011-02-25 19:01:26

+0

@Digit:由於OP沒有提到模型,所以MVC在這裏有一些延伸,但Program.cs當然可以用作簡單的控制器。在引導之外,我傾向於儘可能少地使用Program.cs,因爲將源代碼編入源代碼會使所有內容變得笨拙混亂,並且變得非常難以重構。 – 2011-02-25 19:05:17

1

您可能會考慮擴展Form以包含一些允許您訪問其他表單的屬性/字段。 Form類可以像大多數其他.Net類一樣繼承。

如果這兩個表格都不是真的應該是另一個的孩子,你也可以考慮在Program.cs文件中做一些管理。

如果您從Form繼承了您的form1的新類並添加了類似closeSecondForm的方法,則可以關閉它並處理第二個表單。

有可能是一堆不同的方式來解決這個問題。這些才一點點。

1

如果您將新表單的所有者設置爲對當前表單的引用,則可以從新表單中引用該所有者。您也可以從舊窗體訂閱新窗體的Closed()事件,並使用代碼來處置它(儘管表單可以通過覆蓋OnClosed來處置它,如果它沒有發生的話)。

+0

哦,我很笨。我忘了父母。 – digitlworld 2011-02-25 19:00:27

0
Form2 myform = new Form2(); 

myform.show(); 
this.hide(); 
0

你可以在Form1做到這一點:

... 
var form2 = new form2(); 
form2.Closing += (form2_Closing); 
this.hide(); 
form2.show(); 
... 


private void form2_Closing(object sender, System.EventArgs e) 
{ 
    this.show(); 
} 
+1

處置太晚,該應用程序將失去焦點到另一個應用程序。 FormClosing是正確的。 – 2011-02-25 19:24:36

+0

提示使用,謝謝。 – 2011-02-25 20:01:58

1

這個邏輯應該在Program.cs處理。 Main()方法初始化Form1。你想控制那裏,而不是將控制權交給表單。

例子:

Program.MyForm1.Show(); 
Program.MyForm2.Hide(); 

如果您打算採取多種形式/複雜的邏輯,我建議把這個:

static class Program 
{ 
    internal static Form1 MyForm1; 
    internal static Form2 MyForm2; 
    /// 
    /// The main entry point for the application. 
    /// 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 

     //Application.Run(new Form1()); 

     // Initialize Form1 
     MyForm1 = new Form1(); 
     MyForm1.FormClosing += new FormClosingEventHandler(MyForm1_FormClosing); 

     // You may want to initialize Form2 on-demand instead of up front like here. 
     MyForm2 = new Form1(); 
     MyForm2.FormClosing += new FormClosingEventHandler(MyForm2_FormClosing); 

     // Show Form1 first 
     MyForm1.Show(); 

     // Now we need to occupy the thread so it won't exit the app. This is normally the job of Application.Run. 
     // An alternative to this is to have a third form you pass on control to. 
     while (true) 
     { 
      Application.DoEvents(); 
      System.Threading.Thread.Sleep(10); 
     } 
    } 

    static void MyForm1_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     // Do something, for example show Form2 
     MyForm2.Show(); 

     // EXAMPLE: We only want to hide it? 
     e.Cancel = true; 
     MyForm1.Visible = false; 
    } 
    static void MyForm2_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     // Do something, for example show Form1 
     MyForm1.Show(); 

     // EXAMPLE: We only want to hide it? 
     e.Cancel = true; 
     MyForm2.Visible = false; 
    } 
} 

由於計劃是靜態的,你可以通過訪問MyForm1MyForm2在該項目中的任何地方到一個單獨的班級。另外考慮使用單個窗體並在其中旋轉用戶控件。

+1

使用'Application.Run()',而不是循環中的DoEvents和Sleep! – Tergiver 2011-02-25 20:05:00