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 7623327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:40:44+00:00 2026-05-31T04:40:44+00:00

I am doing a project which includes dynamic controls creation and removal from the

  • 0

I am doing a project which includes dynamic controls creation and removal from the WinForm,
So I decided to that part on a small test project.
Test project has two files Form1.cs and NewControls.cs. This program creates additional buttons whenever user clicks an Add button already on the form.And removes the newly created button when it is clicked (self removal button). Also after removal of button other button’s Name, Text and their position are changed according to a local variable (controlIndex).

Form1.cs

public partial class Form1 : Form
{
    static List<NewControl> newControlsList = new List<NewControl>();
    public Form1()
    {
        InitializeComponent();
    }

    private void Add_Click(object sender, EventArgs e)
    {
        newControlsList.Add(new NewControl(newControlsList.Count));
    }

    public static void RemoveButton(object sender, EventArgs e)
    {
        NewControl tempNewControl = (NewControl)(sender as Button).Tag;
        tempNewControl.RemoveControl();
        newControlsList.Remove(tempNewControl);
        MessageBox.Show("Removed!");
        foreach (NewControl tempcontrol in newControlsList)
        {
            tempcontrol.controlIndex = newControlsList.IndexOf(tempcontrol);
            tempcontrol.PlaceControl();
        }
    }
}

NewControl.cs

class NewControl
{
    public int controlIndex = 0;
    Button newButton = new Button();

    public NewControl(int index)
    {
        controlIndex = index;
        PlaceControl();
    }

    public void RemoveControl()
    {
        newButton.Dispose();
        Form1.ActiveForm.Controls.Remove(newButton);
    }

    public void PlaceControl()
    {
        newButton.Tag = this;
        newButton.Name = "btn" + controlIndex.ToString("D2");
        newButton.Text = "btn" + controlIndex.ToString("D2");
        newButton.Size = new Size(100, 20);
        newButton.Left = controlIndex * 100;
        Form1.ActiveForm.Controls.Add(newButton);
        newButton.Click += new EventHandler(Form1.RemoveButton);
    }
}

Program works nearly as expected. Problem is the MessageBox which I used in form1.cs in RemoveButton() fires many time (as opposed to just one time), which implies whole method being executed several times. Actually I pasted that MessageBox for debugging (sort of).

Since I cannot debug the application as when “Form1.ActiveForm.Controls.Add(newButton);” statement is executed, debugger Throws NullReferenceException, as there is not an active form while debugging.
I know that’s like a bonus question but I thought to just put it there. I am a beginner and can’t see the way through both the problems. 1st problem is really important for my original project as it will cause problem when many controls are added.

  • 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-05-31T04:40:46+00:00Added an answer on May 31, 2026 at 4:40 am

    I think it is because you call PlaceControl from Form1.cs AND in the constructor of the NewControl class, Because you say newButton.Click += new EventHandler(Form1.RemoveButton);.
    You are adding EventHandlers, so there can be more of them.
    So when you call placeControl multiple times, you’ve got multiple event handlers, i think.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project that includes files from a different project. I am getting
I have a WiX project that I am working on which includes a Windows
I'm doing a project in Java which includes (x,y) coordinates. I have created a
I'm doing an AutoLisp project which uses long associative structures to do heavy geometrical
I am doing a project in which I require btree or b+tree data structure.
I am doing a project, for which I need to know all the wikipedia
For this university project I'm doing (for which I've made a couple of posts
I use some libraries that I don't want built as part of every project
I'm doing background research for a project to write a software tool that converts
I'm working on a project that includes WebSockets, and data between the server (Node.js)

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.