Rickard
Rickard

Reputation: 419

Create custom dialog and manipulate it's fields during runtime

I am using a custom dialog that I need to manipulate the layout of during runtime. I am having a hard time figuring out how.

As you can see in the code below, I have a checkbox. When I tick the checkbox, I want textbox2 to be disabled, and vice versa.

Is there anyone who can tell me how to achieve this?

The code:

public static DialogResult MasterAndClientName(string title, string promptText, ref string value, out bool AllSettings)
    {
        Form form = new Form();
        Label label1 = new Label();
        Label label2 = new Label();
        TextBox textBox1 = new TextBox();
        TextBox textBox2 = new TextBox();
        CheckBox AllSettings_cbx = new CheckBox();
        Button buttonOk = new Button();
        Button buttonCancel = new Button();
        form.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
        form.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
        AllSettings_cbx.CheckStateChanged += AllSettings_cbx_CheckStateChanged1;

        form.Text = title;
        label1.Text = promptText.Split('_')[0];
        label2.Text = promptText.Split('_')[1];

        AllSettings_cbx.Text = "Import ALL settings?";
        buttonOk.Text = "OK";
        buttonCancel.Text = "Cancel";
        buttonOk.DialogResult = DialogResult.OK;
        buttonCancel.DialogResult = DialogResult.Cancel;

        label1.SetBounds(9, 20, 372, 13);
        label2.SetBounds(9, 60, 372, 13);
        textBox1.SetBounds(12, 36, 372, 20);
        textBox2.SetBounds(12, 76, 372, 20);
        AllSettings_cbx.SetBounds(12, 102, 372, 20);
        buttonOk.SetBounds(228, 102, 75, 23);
        buttonCancel.SetBounds(309, 102, 75, 23);

        label1.AutoSize = true;
        label2.AutoSize = true;
        textBox1.Anchor = textBox1.Anchor | AnchorStyles.Right;
        textBox2.Anchor = textBox2.Anchor | AnchorStyles.Right;
        buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
        buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

        form.ClientSize = new Size(400, 137);
        form.Controls.AddRange(new Control[] { label1, label2, textBox1, textBox2, buttonOk, buttonCancel, AllSettings_cbx });
        form.ClientSize = new Size(Math.Max(400, textBox1.Right + 10), form.ClientSize.Height);
        form.FormBorderStyle = FormBorderStyle.FixedDialog;
        form.StartPosition = FormStartPosition.CenterScreen;
        form.MinimizeBox = false;
        form.MaximizeBox = false;
        form.AcceptButton = buttonOk;
        form.CancelButton = buttonCancel;

        DialogResult dialogResult = form.ShowDialog();
        if (textBox1.Text.Trim().Contains(" "))
            textBox1.Text = textBox1.Text.Trim().Replace(' ', '.');
        if (!textBox1.Text.Trim().Contains("."))
            textBox1.Text = textBox1.Text.Trim() + ".Resident";
        if (textBox2.Text.Trim().Contains(" "))
            textBox2.Text = textBox2.Text.Trim().Replace(' ', '.');
        if (!textBox2.Text.Trim().Contains("."))
            textBox2.Text = textBox2.Text.Trim() + ".Resident";
        value = textBox1.Text + "¤" + textBox2.Text;
        AllSettings = AllSettings_cbx.Checked;
        return dialogResult;
    }

Upvotes: 0

Views: 47

Answers (1)

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

Reputation: 34244

You can attach a handler to CheckedChanged event:

AllSettings_cbx.CheckedChanged += (sender, eventArgs) => {
    textBox2.Enabled = !AllSettings_cbx.Checked;
};

You may also create a named method, it is considered to be a good practice. However, it will require to make your AllSettings_cbx not local.

It all would be much easier, if you have created your form in designer, but not during runtime.

Upvotes: 3

Related Questions