Does anyone see anything wrong with this:
this.Controls.Remove(this);
this is a class which extends user control. When I step through this section of code it looks like everything is fine, however nothing happens to the form. I would expect the control to be gone.
As mentioned, you're removing the control from itself...that's not likely what you want. I assume you want to remove the control from it's parent - so you probably want this.Parent.Controls.Remove(this);.
Luckily, since you didn't mention platform, the code is the same for WebForms or WinForms.
Nothing happens because it doesnt find 'this' in the controls collection of 'this'
If your scope is within the Control itself, you would want to do
this.Container.Controls.Remove(this);
but it all depends on what type of control and in what type of container. but the above should work in most cases.
EDIT:
If you know your control belongs to a form, you can do the following, or replace Form with the known container type (i.e. a panel)
((Form)this.Container).Controls.Remove(this);
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