在运行时

时间:2015-11-18 11:36:51

标签: c# winforms

我正在使用自定义对话框,我需要在运行时操作布局。 我很难搞清楚如何。

正如您在下面的代码中看到的,我有一个复选框。 当勾选复选框时,我希望禁用textbox2,反之亦然。

有没有人可以告诉我如何实现这个目标?

代码:

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

1 个答案:

答案 0 :(得分:3)

您可以将处理程序附加到y事件:

CheckedChanged

您也可以创建一个命名方法,它被认为是一种很好的做法。但是,它需要使您的AllSettings_cbx.CheckedChanged += (sender, eventArgs) => { textBox2.Enabled = !AllSettings_cbx.Checked; }; 不是本地的。

如果你在设计师中创建了表单,那么这一切都会容易得多,但在运行时却没有。