首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# WinForms 模式對話框完整解析(二)

     編輯:關於C語言

AcceptButton / CancelButton 屬性

提供鍵盤快捷鍵支援:


// 設計器生成代碼
private Button buttonOK;
private Button buttonCancel;

this.AcceptButton = buttonOK;   // Enter 鍵觸發
this.CancelButton = buttonCancel; // Esc 鍵觸發

快捷鍵行為細節

  • 焦點在 buttonCancel 時按 Enter → 觸發 Cancel(非 OK)
  • 焦點在第三個按鈕時按 Enter → 觸發該按鈕
  • 鍵盤觸發不會顯示按鈕按下視覺效果

模式對話框開啟方式


OptionForm form = new OptionForm();
// form.ShowDialog();           // 無擁有者
form.ShowDialog(this);          // 指定擁有者(推薦)

指定擁有者的好處

  • 對話框置中於擁有者視窗
  • 對話框內可透過 this.Owner 存取主視窗:

// 模式對話框內部
MainForm mainForm = this.Owner as MainForm;
if (mainForm != null) mainForm.RefreshData();

注意:使用無參數 ShowDialog() 時,Owner 為 null。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved