禁用动态创建表单的关闭按钮 [英] Disable close button for dynamically created form

查看:84
本文介绍了禁用动态创建表单的关闭按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为动态创建的表单禁用close()按钮。

我能够通过覆盖

I want to disable close() button for a dynamically created form.
I able to do so by overriding

CreateParamsforms

方法表单表单通过解决方案资源管理器添加到项目中。但它不适用于通过后面的代码动态创建的表单。即我无法从动态表单中调用

method form forms add to project through solution explorer. But it doesn't work for forms created dynamically through a code behind. I.e. I'm not able to invoke

CreateParams





我尝试了什么:


下面的
是通过覆盖CreateParams方法为表单添加解决方案资源管理器来禁用关闭按钮的工作解决方案



from dynamic form.

What I have tried:

below is the working solution to disable close button by overriding CreateParams method for a form add through solution explorer

public partial class Form1 : Form
    {
        #region Private Variables
        private const int CP_NOCLOSE_BUTTON = 0x200;
        #endregion
        public Form1()
        {
            InitializeComponent();
        }
        protected override CreateParams CreateParams
        {
            get
            {
                var myCp = base.CreateParams;
                myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
                return myCp;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
    }





但是,对于下面提到的动态表格,我怎么能禁用关闭按钮





But, for below mentioned dynamic form, How could I disable close button

public static DialogResult CustomDialogBox(string title, string _lblUserName, string _lblPassword, ref string proxyUserName, ref string proxyPassword)
        {
            Form frmProxyAuthDialog = new Form();
            frmProxyAuthDialog.FormClosing += FrmProxyAuthDialog_FormClosing;
            Label lblUserName = new Label();
            Label lblPassword = new Label();
            TextBox txtUserName = new TextBox();
            TextBox txtPassword = new TextBox();
            txtPassword.PasswordChar = '*';
            Button buttonOk = new Button();
            //Button buttonCancel = new Button();

            frmProxyAuthDialog.Text = title;
            lblUserName.Text = _lblUserName;
            lblPassword.Text = _lblPassword;

            buttonOk.Text = "OK";
            //buttonCancel.Text = "Cancel";
            buttonOk.DialogResult = DialogResult.OK;
            //buttonCancel.DialogResult = DialogResult.Cancel;

            lblUserName.SetBounds(9, 10, 372, 13);
            txtUserName.SetBounds(12, 30, 372, 20);

            lblPassword.SetBounds(9, 52, 372, 13);
            txtPassword.SetBounds(12, 68, 372, 20);
            buttonOk.SetBounds(228, 90, 75, 23);
            //buttonCancel.SetBounds(309, 72, 75, 23);

            lblUserName.AutoSize = true;
            lblPassword.AutoSize = true;

            txtUserName.Anchor = txtUserName.Anchor | AnchorStyles.Right;
            txtPassword.Anchor = txtPassword.Anchor | AnchorStyles.Right;

            buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            //buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

            frmProxyAuthDialog.ClientSize = new Size(396, 120);
            frmProxyAuthDialog.Controls.AddRange(new Control[] { lblUserName, lblPassword, txtUserName, txtPassword, buttonOk });
            frmProxyAuthDialog.ClientSize = new Size(Math.Max(300, lblUserName.Right + 10), frmProxyAuthDialog.ClientSize.Height);
            frmProxyAuthDialog.FormBorderStyle = FormBorderStyle.FixedDialog;
            frmProxyAuthDialog.StartPosition = FormStartPosition.CenterScreen;
            frmProxyAuthDialog.MinimizeBox = false;
            frmProxyAuthDialog.MaximizeBox = false;
            frmProxyAuthDialog.AcceptButton = buttonOk;
            //frmProxyAuthDialog.CancelButton = buttonCancel;

            DialogResult dialogResult = frmProxyAuthDialog.ShowDialog();
            proxyUserName = txtUserName.Text;
            proxyPassword = txtPassword.Text;
            return dialogResult;
        }





如果收盘原因不是用户关闭,我也试图取消关闭表格结束时的表格。但是,如果首先单击关闭按钮,表单结束原因始终是userclosing。



I also tried to cancel form closing on form closing event if closing reason is not userclosing. But, if form closing reason is always userclosing if close button is clicked first.

推荐答案

简单 - 为动态表单创建一个覆盖 CreateParams property:

Simple - create a class for your dynamic form which overrides the CreateParams property:
public class NoCloseForm : Form
{
    private const int CP_NOCLOSE_BUTTON = 0x200;
    
    protected override CreateParams CreateParams
    {
        get
        {
            var myCp = base.CreateParams;
            myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
            return myCp;
        }
    }
}



然后使用该类创建动态表单:


Then use that class to create your dynamic form:

public static DialogResult CustomDialogBox(string title, string _lblUserName, string _lblPassword, ref string proxyUserName, ref string proxyPassword)
{
    Form frmProxyAuthDialog = new NoCloseForm();
    ...


这篇关于禁用动态创建表单的关闭按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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