Windows Form, Ok, Cancel button
1. 为button设置DialogResult property为非None值, 可以关闭父窗口,并使父窗口的DialogResult property返回相应的值。
http://msdn.microsoft.com/en-us/library/system.windows.forms.button.dialogresult(v=vs.110).aspx
If theDialogResultfor this property is set to anything other thanNone, and if the parent form was displayed through theShowDialogmethod, clicking the button closes the parent form without your having to hook up any events. The form'sDialogResultproperty is then set to theDialogResultof the button when the button is clicked.
For example, to create a "Yes/No/Cancel" dialog box, simply add three buttons and set theirDialogResultproperties toYes,No, andCancel.
如果button click里需要做额外的操作或者检查,比如不满足某条件就不关闭父窗口,可以设置DialogResult 为none。
private void buttonOk_Click(object sender, EventArgs e) { if (this.comboBox1.SelectedItem == null) { MessageBox.Show("Please select the target project!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); this.DialogResult = DialogResult.None; } }
2.Form.AcceptButton, CancelButton Property设置子为子Button, 用户按下Enter, Esc键会触发相应的Button。相当于为Button设置Enter, Esc快捷键。
优质内容筛选与推荐>>