Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9009541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:13:29+00:00 2026-06-16T02:13:29+00:00

I have made a class that contain such as form,button, etc in class .

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:13:30+00:00Added an answer on June 16, 2026 at 2:13 am

    I am not sure what you are asking but if its event related problem I would suggest deallocating the event.

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a Silverlight app that contain a class and I want to
I made a music class that will play a song. I have made onResume
This is a C++ class that I have made with n number of pointers.
I have a custom-made view that extends the View class. I would like 2
I have made some a fiddle of such a headline that I need. CSS:
I have a class that I writes iCal events, and it contains nested classes
I have a problem handling a function pointer.I have made a class Control that
i have a Class called Services in the App_code folder that contain Method like
I have a PHP class that can contain an array of its own type
I have made vectors that contain functions, but they had no argument list. Also,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.