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
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();
frmProxyAuthDialog.Text = title;
lblUserName.Text = _lblUserName;
lblPassword.Text = _lblPassword;
buttonOk.Text = "OK";
buttonOk.DialogResult = DialogResult.OK;
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);
lblUserName.AutoSize = true;
lblPassword.AutoSize = true;
txtUserName.Anchor = txtUserName.Anchor | AnchorStyles.Right;
txtPassword.Anchor = txtPassword.Anchor | AnchorStyles.Right;
buttonOk.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;
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.