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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With