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

Remarks

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快捷键。

优质内容筛选与推荐>>
1、LINQ 的演变及其对 C# 设计的影响
2、Java——计算最长英语单词链
3、struts2 action 接受数组参数为Null的问题
4、软件测试中有关界面测试经验总结
5、Firefox What's New 太难找了


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn