C#CancelButton关闭对话框? [英] C# CancelButton closes dialog?

查看:426
本文介绍了C#CancelButton关闭对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(VS2005,.NET 2.0)



我有一个显示为使用的ShowDialog()方法对话框的形式。窗体的CancelButton属性设置为窗体上按钮。即使我该按钮的的DialogResult设置为无,点击按钮仍然关闭对话框。我不希望这种事情发生 - 我希望能够控制对话框是否关闭与否



这问题不与形式的出现的AcceptButton - 与该按钮的的DialogResult设置为none,我可以做什么样的处理是必要的,然后再决定是否要手动设置窗体的DialogResult的导致其关闭。



我以为CancelButton属性是为了向索利表示要点击按钮,如果按下Escape(多为的AcceptButton情况应该只是一个指示按钮,咔嚓按下时输入)。我错在这?我错过了一些其他的原因,我的形式正在缩小?或者这是一个错误。



编辑:代码添加。这是对话框表单(form 2)取消按钮(按钮1)。
上的取消按钮仅窗体的CancelButton,它不具备的DialogResult设置为取消,但按下按钮仍然关闭窗体

 私人无效的InitializeComponent()
{
this.button1 =新System.Windows.Forms.Button();
this.SuspendLayout();
//
//按钮1
//
this.button1.Name =Button1的;
this.button1.Text =Button1的;
//
//窗体2
//
this.CancelButton = this.button1;
this.Controls.Add(this.button1);
this.Name =窗体2;
this.Text =窗体2;
this.ResumeLayout(假);
}


解决方案

另外注意的是,表单可能按Alt + F4并按下X键和方式都不会触发取消按钮点击事件被关闭。



除非你也处理这些情况不会吧。最好遵循slurdge的意见和防止形式从的FormClosing事件结束



编辑:
还要注意,如果你改变了按钮的DialogResult回无在属性窗口中,您将其更改为默认值。如果该值是属性那么它不会在*了.Designer.cs是持久的默认值。即使它被持续的形式初始化代码被置于最后在*了.Designer.cs和将覆盖没有用,因为该行的取消:



  this.CancelButton = this.button1; 

你可以在反射检查前行做到这一点:

 公共无效set_CancelButton(IButtonControl值)
{
base.Properties.SetObject(PropCancelButton,值);
如果(!(值= NULL)及及(value.DialogResult == DialogResult.None))
{
value.DialogResult = DialogResult.Cancel;
}
}

您可以将其改回无在构造函数之后在InitializeComponent()调用。


(VS2005, .Net 2.0)

I have a form that is displayed as a dialog using the ShowDialog() method. The form's CancelButton property is set to a button on the form. Even if I set that button's DialogResult to None, clicking the button still closes the dialog. I don't want this to happen - I want to be able to control whether the dialog closes or not.

This issue doesn't occur with the form's AcceptButton - with that button's DialogResult set to none, I can do what processing is necessary, then decide whether or not to manually set the form's DialogResult to cause it to close.

I thought the CancelButton property was meant soley to indicate the button that should be "clicked" if Escape is pressed (much as the AcceptButton is only supposed to indicate the button to "click" when Enter is pressed). Am I wrong in this? Have I missed some other reason my form is closing? Or is this a bug?

Edit: Code added. This is the dialog form (form 2) with the cancel button (button 1). The cancel button is only the form's CancelButton, it does not have DialogResult set to Cancel, but pressing the button still closes the form

    private void InitializeComponent()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.Name = "button1";
        this.button1.Text = "button1";
        // 
        // Form2
        // 
        this.CancelButton = this.button1;
        this.Controls.Add( this.button1 );
        this.Name = "Form2";
        this.Text = "Form2";
        this.ResumeLayout( false );
    }

解决方案

Also beware that the form may be closed by pressing Alt+F4 and pressing the X button and both ways will not trigger the cancel button click event.

Unless you are also handling these situations wouldn't it be best to follow the advice of slurdge and prevent the form from closing in the FormClosing event.

Edit: Also note that if you change the DialogResult of the button back to None in the Properties windows, you are changing it to the default value. If the value is the default value of the property then it will not be persisted in the *.Designer.cs. Even if it were persisted the form initialization code is placed last in the *.Designer.cs and would override the None with Cancel because of the line:

this.CancelButton = this.button1;

As you can check in Reflector the previous line does this:

public void set_CancelButton(IButtonControl value)
{
    base.Properties.SetObject(PropCancelButton, value);
    if ((value != null) && (value.DialogResult == DialogResult.None))
    {
        value.DialogResult = DialogResult.Cancel;
    }
}

You can change it back to None in the constructor after the InitializeComponent() call.

这篇关于C#CancelButton关闭对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆