繁体   English   中英

C#类中的句柄控制事件的麻烦

[英]Trouble of Handle Control Event in Class C#

我在课堂上做了一个包含诸如form,button等的 然后我在课堂上创建它们的处理事件。 看下面我的课程代码:

        public static class InputBox
    {
        static Form form = new Form();
        static Label label = new Label();
        static TextBox textBox = new TextBox();
        static Button buttonOk = new Button();
        static Button buttonCancel = new Button();
        public static DialogResult Show(string title, string promptText, ref string value)
        {
            form.Text = title;
            label.Text = promptText;
            textBox.Text = value;

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

            label.SetBounds(9, 20, 372, 13);
            textBox.SetBounds(12, 36, 372, 20);
            buttonOk.SetBounds(228, 72, 75, 23);
            buttonCancel.SetBounds(309, 72, 75, 23);

            label.AutoSize = true;
            textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
            buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

            form.ClientSize = new Size(396, 107);
            form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });

            form.FormBorderStyle = FormBorderStyle.FixedDialog;
            form.StartPosition = FormStartPosition.CenterScreen;

            buttonOk.Click += new EventHandler(buttonOk_Click); //this is the handle code
            buttonCancel.Click += new EventHandler(buttonCancel_Click);//this is the handle code

            form.MinimizeBox = false;
            form.MaximizeBox = false;
            form.AcceptButton = buttonOk;
            form.CancelButton = buttonCancel;

            DialogResult dialogResult = form.ShowDialog();
            value = textBox.Text;
            return dialogResult;
        }

        static void buttonCancel_Click(object sender, EventArgs e)
        {
            form.Close();
        }

        static void buttonOk_Click(object sender, EventArgs e)
    {
        MessageBox.Show("If u click this, You Will Get many Messages as many as you call the class"); 
    }
    }

然后,我用自己的表单代码这样称呼它:

string strbuffer; // Global Variable

        InputBox.Show("Hi..?", "What Is Your Name?",ref strbuffer);
            //do something...
            InputBox.Show("Hi..?", "What Is Your Age?",ref strbuffer);
            //do something
            InputBox.Show("Hi..?", "How Many Cars do you have?",ref strbuffer);
            //do something

如果我调用该类的次数与调用该类的次数相同,那么旧处理程序将永远不会被处置,因此处理程序控件中的代码将与您调用该类的次数一样多。

我猜想当不再使用该类时该变量不会被处理掉,所以每次调用它时,该变量始终会累积

解决这种情况的最佳方法是什么?

我不确定您要问的是什么,但是如果它的事件相关问题,我建议取消分配事件。

static void buttonCancel_Click(object sender, EventArgs e)
{
        form.Close();
    buttonOk.Click -= new EventHandler(buttonOk_Click); 
        buttonCancel.Click -= new EventHandler(buttonCancel_Click);
}

static void buttonOk_Click(object sender, EventArgs e)
{
    MessageBox.Show("If u click this, You Will Get many Messages as many as you call the class"); 
    buttonOk.Click -= new EventHandler(buttonOk_Click); 
    buttonCancel.Click -= new EventHandler(buttonCancel_Click);
}

在这里,我将表单,标签,文本框等的静态变量移至Show方法中。 这意味着您每次调用此方法时都会得到一个新的实例。 因此,该事件尚未附加。

然后,事件处理程序必须更改为不引用静态表单(该静态表单不再存在),因此我们从按钮获取了对表单实例的引用。

public static class InputBox
        {

    public static DialogResult Show(string title, string promptText, ref string value)
    {
        Form form = new Form();
        Label label = new Label();
        TextBox textBox = new TextBox();
        Button buttonOk = new Button();
        Button buttonCancel = new Button();

        form.Text = title;
        label.Text = promptText;
        textBox.Text = value;

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

        label.SetBounds(9, 20, 372, 13);
        textBox.SetBounds(12, 36, 372, 20);
        buttonOk.SetBounds(228, 72, 75, 23);
        buttonCancel.SetBounds(309, 72, 75, 23);

        label.AutoSize = true;
        textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
        buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
        buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

        form.ClientSize = new Size(396, 107);
        form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });

        form.FormBorderStyle = FormBorderStyle.FixedDialog;
        form.StartPosition = FormStartPosition.CenterScreen;

        buttonOk.Click += new EventHandler(buttonOk_Click); //this is the handle code
        buttonCancel.Click += new EventHandler(buttonCancel_Click);//this is the handle code

        form.MinimizeBox = false;
        form.MaximizeBox = false;
        form.AcceptButton = buttonOk;
        form.CancelButton = buttonCancel;

        DialogResult dialogResult = form.ShowDialog();
        value = textBox.Text;
        return dialogResult;
    }

    static void buttonCancel_Click(object sender, EventArgs e)
    {
        (sender as Button).FindForm().Close();
    }

    static void buttonOk_Click(object sender, EventArgs e)
{
    MessageBox.Show("If u click this, You Will Get many Messages as many as you call the class"); 
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM