C类手柄控制事件的故障#

时间:2012-12-19 13:55:35

标签: c# visual-studio visual-studio-2008

我在中创建了一个包含表单,按钮等的类。然后我在课堂上创建了他们的句柄事件。看看下面的课程代码:

        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

如果我调用Class的次数与我调用它一样多,那么旧的处理程序将永远不会被处置,因此处理程序控制中的代码将被调用,就像调用类一样多

我想这个变量在类不再使用时不会被处理掉,所以变量总是在每次调用它时累积

解决此案的最佳方法是什么?

2 个答案:

答案 0 :(得分:0)

我不确定你在问什么,但如果它的事件相关问题,我会建议你解除分配事件。

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

答案 1 :(得分:0)

在这里,我将表单,标签,文本框等的静态变量移动到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"); 
}
}