(工作中需要用到Wizard,找了一圈發現.NET居然沒有提供現成的方便的實現,在找到的資料裏,這篇文檔提供的方案相對來說是可靠且使用簡單)
目標
1、以類似MFC的方式創建Wizard
2、用類似設計對話框的方式設計每一頁
這篇文章提供了兩個類用於創建Wizard,另外有兩個類用於創建Wizard97風格
主要的類
WizardForm和WizardPage類似CPropertySheet和CPropertyPage
WizardForm
WizardForm派生自Form,它負責管理WizardPage集合。另外,它提供了缺省的Wizard界面。它的主要方法如下:
public void SetFinishText( string ) | 設置Finish按鈕的文字 |
public void SetWizardButtons( WizardButtons ) | 啓用或者禁用嚮導按鈕 |
創建你自己的嚮導類的第一件事就是擴展這個類,通常從該類繼承,編輯屬性或者添加額外的按鈕。
WizardPage
WizardPage派生自UserControl。提供了嚮導相關的方法,如 OnSetActive, OnWizardNext等。它的主要方法如下:
protected WizardForm Wizard { get; } | 獲得當前頁所屬的 WizardForm |
protected internal virtual bool OnKillActive() | 該函數在當前頁失去焦點時被調用,可以在此處做數據校驗 |
protected internal virtual bool OnSetActive() | 該函數在當前頁獲得焦點時被調用 |
protected internal virtual string OnWizardBack() | 當點擊Back按鈕時被調用。Return WizardForm.DefaultPage to advance to the previous page, WizardForm.NoPageChange to prevent the page from changing, or the value of the Name property of the page to be displayed. |
protected internal virtual string OnWizardNext() | 當Next按鈕被點擊時調用。Return WizardForm.DefaultPage to advance to the next page, WizardForm.NoPageChange to prevent the page from changing, or the value of the Name property of the page to be displayed. |
protected internal virtual bool OnWizardFinish() | 當Finish按鈕被點擊時調用。如果返回true則關閉嚮導,反之阻止關閉 |
一旦你對 WizardForm 的定製完成,你可以創建一個或多個 WizardPage 的派生類。爲了把這些page和wizard關聯,把這些page加入相應WizardForm的Controls collection。
使用WizardForm 和 WizardPage
1、創建派生自WizardPage的類
2、創建派生自WiazardForm的類
3、添加Page,如下
Controls.AddRange( new Control[] {
new FirstPage(),
new SecondPage(),
new ThirdPage(),
new FourthPage()
} );