Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to disable close() button for a dynamically created form.
I able to do so by overriding
C#
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
C#
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

C#
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

C#
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;
        }


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.
Posted
Updated 7-Apr-17 2:09am
Comments
CHill60 7-Apr-17 4:31am    
I'm confused - why have you tagged both ASP.NET and WinForms?
Karthik_Mahalingam 7-Apr-17 5:03am    
:)

1 solution

Simple - create a class for your dynamic form which overrides the CreateParams property:
C#
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:
C#
public static DialogResult CustomDialogBox(string title, string _lblUserName, string _lblPassword, ref string proxyUserName, ref string proxyPassword)
{
    Form frmProxyAuthDialog = new NoCloseForm();
    ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900