I have made a class that contain such as form,button, etc in class. then i create the handle events of them in class. Take a look at my class code below:
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");
}
}
then, i call it like this in my own form code:
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
if i call the Class as many as i call it, the old handler will never get dispose so the code in handler control will be excuted as many as you call the class
i guess the variable doesn’t get dispose when the class is unused anymore so the variable always accumulate every times you call it
What would be the best way to solve this case?
I am not sure what you are asking but if its event related problem I would suggest deallocating the event.