2013-04-07 37 views
-1

父窗體的方案是這我有一個父窗體,每次我點擊search button,一個搜索表單出現和父窗體只是禁止,但可以看出啓用從子窗體

問題是這樣的,每次我點擊exit button搜索表單時,搜索表單退出,但父窗體MainForm仍然被禁用

的代碼在搜索表單exit button

MainForm mainForm = new MainForm(); 
mainForm.Enabled = true; 
this.Hide(); 

回答

1

你的代碼創建MainForm類的新實例。這個新實例不是最初推出搜索表單的實例。 Obvioulsy第一個實例仍然被禁用,而新的啓用,但你不能看到它,因爲沒有顯示

通常這個問題可以通過各種方式解決。

  • 傳遞打開的搜索表單 到搜索表單
  • 形式(MainForm的)的情況下注冊的主實例時搜索窗體關閉
  • 創建和提高你自己的事件被通知信號輸出到外部形式 搜索結束

如何登記MainForm的通知SearchForm結束
的啓動搜索表單的MainForm的代碼應該更改爲如下所示 編輯此方法要求您有效關閉SearchForm。一個簡單的隱藏不會關閉的形式,因此沒有結束活動將由SearchForm

SearchForm f = new SearchForm(); 
// Here the current (this) instance of the MainForm requires 
// to be notified of the closing of the search form 
f.Closing += new System.ComponentModel.CancelEventHandler(OnSearchClosing); 
this.Enabled = false; 
f.Show(); 
.... 

得到提升,並添加該代碼來獲得通知後,關閉搜索表單

// When the search form closes you get this event 
// Here `this` is the correct instance of the MainForm (the one disabled) 
private void OnSearchClosing(object sender, System.ComponentModel.CancelEventArgs e) 
{ 
    this.Enabled = true; 
} 

如何通過調用SearchForm實例的MainForm實例
第二種方法在我看來不太可取,因爲您會強制搜索表單對主窗體進行引用,並且您將從SearchForm中操作MainForm。
這樣,你傳遞的MainForm到SearchForm的基準,並使用該參考重新啓用的MainForm

// In the calling code of the search form pass the reference to the caller 
SearchForm f = new SearchForm(this); 
f.Show(); 

在SearchForm的形式構造函數接收全局類變量

參考和商店
public class SearchForm:Form 
{ 
    private MainForm _callerForm; 
    public void SearchForm(MainForm callingForm) 
    { 
     InitializeComponent(); 
     _callerForm = callingForm; 
     _callerForm.Enabled = false; 
    } 

    // ... somewhere in this class 
    _callerForm.Enabled = true; 
    this.Hide(); 
    // .... 
} 

如何創建和引發自定義事件
有具有第一個的好處和留下通知的責任在日的手,第三種方法Ë開發(這意味着你可以提高自己的事件,並取其感興趣的事件可以處理自己的業務)

在SearchForm創建自定義事件

在SearchForm.cs

// Build a delegate that returns nothing and receive nothing 
// Define an event based on that delegate 
public delegate void OnSearchEnded(); 
public event OnSearchEnded SearchEnded; 

... somewhere in this class 
// If some external entity choose to be notified when whe have finished search 
// raise the event to which this external entity has subscribed (MainForm) 
if(SearchEnded != null) 
    SearchEnded(); 
    .... 

在MainForm.cs

SearchForm f = new SearchForm(); 
// Here the current (this) instance of the MainForm infom the SearchForm 
// that it wants to be notified when the search end 
f.SearchEnded += new SearchForm.OnSearchEnded(EnableThisForm); 
this.Enabled = false; 
f.Show(); 
.... 

// Here the main form receives the notification from the Search Form 
public void EnableThisForm() 
{ 
    this.Enabled = true; 
} 
+0

那麼我應該怎麼做從子窗體調用mainForm.Enabled? – eaponz 2013-04-07 08:30:38

+0

所有這些都在主表格中? – eaponz 2013-04-07 08:38:17

+0

它仍然無法正常工作。 – eaponz 2013-04-07 08:41:52

1

一個更優雅的解決辦法是讓搜索形成模態對話框。無論何時打開模式對話框,其父窗體都會自動禁用。

您可以通過更改創建搜索表單到您的父窗體上的代碼做到這一點:

searchform.ShowDialog(this); 

,直到搜索窗體關閉將禁用父窗體,然後父窗體會自動重新 - 啓用。

+0

哈哈......沒有想到它。好想法。謝了哥們。 – eaponz 2013-04-07 09:40:53

0

您應該將MainForm傳遞給SearchForm,這樣在關閉搜索表單之前,您將能夠隱藏原始MainForm而不創建新的MainForm。看到代碼波紋管:

public class MainForm:Form 
{ 
    private SearchForm searchForm;//for keeping the search form 
    public MainForm()//your main form constructor 
    { 

    } 
    this.searchBtn.Click+=(s,e)=> 
    { 
     if(this.searchForm == null) 
     { 
      this.searchForm = new SearchForm(this);//creating new searchForm if it does no exist 
      this.searchForm.Show(); 
      this.Enabled = false; 
     } 

    }; 

} 

public class SearchForm:Form 
{ 
    private MainForm mainForm;//this will keep the original mainform 
    public SearchForm(MainForm mainForm) 
    { 
     this.mainForm = mainForm; 
    } 

    this.OnFormClosing+=(s,e)=> 
    { 
     this.mainForm.Enabled = true; 
    }; 
} 

這將關閉搜索表單每次和創建它的一個新的實例,當你點擊搜索按鈕。如果你想隱藏它,你應該使用這個代碼:

public class MainForm:Form 
{ 
    private SearchForm searchForm;//for keeping the search form 
    public MainForm()//your main form constructor 
    { 

    } 
    this.searchBtn.Click+=(s,e)=> 
    { 
     if(this.searchForm == null) 
     { 
      this.searchForm = new SearchForm(this);//creating new searchForm if it does no exist 
      this.searchForm.Show(); 
      this.Enabled = false; 
     } 
     else 
     { 
      this.Enabled = false; 
      this.searchForm.Show(); 
     } 

    }; 

} 

public class SearchForm:Form 
{ 
    private MainForm mainForm;//this will keep the original mainform 
    public SearchForm(MainForm mainForm) 
    { 
     this.mainForm = mainForm; 
    } 

    this.OnFormClosing+=(s,e)=> 
    { 
     e.Cancel = true; 
     this.mainForm.Enabled = true; 
     this.Hide(); 
    }; 
} 

基本上這是要做到這一點,無需複雜的事件等最簡單的方法。我希望這可以解決你的問題,如果不是我在這裏幫助。從clossing事件,以使母體

0
UpgradeLicenseDialog dialogBox = new UpgradeLicenseDialog(); 
dialogBox.FormClosing+=dialogBox_FormClosing; 
dialogBox.StartPosition = FormStartPosition.CenterScreen; 
this.Enabled = false; 
dialogBox.Show();` 

使用...

私人無效dialogBox_FormClosing(對象發件人,System.ComponentModel.CancelEventArgs E) { this.Enabled = TRUE; }