提供鍵盤快捷鍵支援:
// 設計器生成代碼
private Button buttonOK;
private Button buttonCancel;
this.AcceptButton = buttonOK; // Enter 鍵觸發
this.CancelButton = buttonCancel; // Esc 鍵觸發
buttonCancel 時按 Enter → 觸發 Cancel(非 OK)
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。