Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for adding controls at run-time

When adding controls to a form at runtime, you can do either of the following:

Button btn = new Button();
//...
this.Controls.Add(btn);

or

Button x = new Button();
//...
btn.Parent = this;

I had assumed that they were the same, and it was just down to personal preference which way to do it, but someone at work mentioned that the second method is worse as the button will not get disposed when the form is disposed (assuming no event handlers have been added and are being held onto).

This didn't make a lot of sense to me so I had a look online, but couldn't find anything to say one way or the other.

Does someone know the answer or can point me in the right direction?

like image 841
Pondidum Avatar asked Jun 09 '09 15:06

Pondidum


People also ask

How to add controls dynamically in asp net using c#?

Inside one of the td tags, you have to place checkboxlist, for prompting the user to what controls they want to generate. So, we had taken almost 5 controls as – Textbox, button, label, dropdown, and radiobutton. After that, we will place one textbox to prompt the user to choose how many controls they want to add.

How do I delete a control in Visual Studio?

To remove controls from a collection programmatically In Visual Basic, use the RemoveHandler Statement keyword; in C#, use the -= operator. Use the Remove method to delete the desired control from the panel's Controls collection. Call the Dispose method to release all the resources used by the control.


2 Answers

Since speculating is a waste of time, I grabbed my copy of Reflector and had a look at the actual code. The Parent property calls the ParentInternal property, which in turn calls value.Controls.Add(this)

/* this code is part of the .NET Framework was decompiled by Reflector and is copyright Microsoft */
    internal virtual Control ParentInternal
    {
        get
        {
            return this.parent;
        }
        set
        {
            if (this.parent != value)
            {
                if (value != null)
                {
                    value.Controls.Add(this);
                }
                else
                {
                    this.parent.Controls.Remove(this);
                }
            }
        }
    }

Based on this code, the methods are equivalent and this is strictly a matter of preference.

like image 79
Jack Bolding Avatar answered Sep 21 '22 00:09

Jack Bolding


I can see where the button disposal could be an issue if you're writing an application that is opening and closing a lot of forms during its duration. You would need to make sure you have some proper disposal code in hand to make sure the application doesn't suck up too much memory.

That aside, I like the first statement because it more clearly explains what your code is doing. You're creating a new button, and you're adding it to the existing controls on the page. You can read right past this when debugging/refactoring and understand what is going on. In the second group of code, this is slightly more vague. If you brushed over the the initial button declaration and saw the btn.Parent = this statement, you could be led to believe that you were reassigning the button to a new form, or something to that effect.

It does sound a bit nitpicky, but lately I've been helping some co-workers by showing them some of my code and I'm coming to find that while there is definitely more than 1 way to skin a cat, sometimes there a certain way of skinning it that explains itself much better when looking at things in the future.

like image 26
Dillie-O Avatar answered Sep 24 '22 00:09

Dillie-O