Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs
1。Handling OK and Cancel
??? 對于modal form ,如果 DialogResult 屬性被開發者顯式地設置了除了None以外的值,則該對話框會被自動關閉(會自動調用 Close() 方法)。
??? 如果想讓用戶在按 Enter 以及 Esc 時相當于按了 OK 和 Cancel 按鈕(符合一般的操作習慣),要設置對話框的 AcceptButton?以及 CancelButton 屬性(可以直接在相應Form的屬性欄中設置,在 Misc 分類中)。
????
????this.AcceptButton?=?this.okButton;
????this.CancelButton?=?this.cancelButton;
????
}
??? 一般來說,如果設置了AcceptButton?以及 CancelButton 屬性,則這兩個按鈕的的envent handler 就不需要了。
???絕對要注意的是,需要手寫類似如下的語句,寫在Form的constructor中的 InitializeComponent() 后面:
??????? this.okButton.DialogResult?=?DialogResult.OK;
??????? this.cancelButton.DialogResult?=?DialogResult.Cancel;
不能寫在 InitializeComponent() 中,因為該方法是 IDE 自動生成的。
2。對于Modeless Form Data
?? Modeless Form 在關閉的時候怎么把相應的信息帶回調用它的 Form 呢?答案就是 .Net 中的event
??
??//?當 Accept 按鈕被按下時候的 Event
??public?event?EventHandler?Accept;
??void?acceptButton_Click(object?sender,?EventArgs?e)?{
????//?Accept 按鈕的實踐處理方法
????if(?Accept?!=?null?)?Accept(this,?EventArgs.Empty);
??}
??void?closeButton_Click(object?sender,?EventArgs?e)?{
????this.Close();
??}
}
//以下代碼在主窗口對應的cs文件
void?showProperties_Click(object?sender,?EventArgs?e)?{
??PropertiesDialog?dlg?=?new?PropertiesDialog();
??dlg.Accept?+=?new?EventHandler(Properties_Accept);
??dlg.Show();
}
//?Client?handles?event?from?form?to?access?accepted?values
void?Properties_Accept(object?sender,?EventArgs?e)?{
??PropertiesDialog?dlg?=?(PropertiesDialog)sender;
??this.Text?=?dlg.Text;
}
轉載于:https://www.cnblogs.com/anf/archive/2005/02/27/109799.html
總結
以上是生活随笔為你收集整理的Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 过年结束了!
- 下一篇: [导入]在IE耗时操作中加入进度条或进度