What's the proper way to delete a componente from a parent form?

Posted on 2011-05-11
Medium Priority
Last Modified: 2012-05-11

I have a Winforms project with several base forms from which I'm inheriting other forms from.

In one of these base form there is (was) a Listbox, that I wanted to remove.

So, I simply hit delete and tried to recompile.
Now, the child form is complaining about the missing Listbox and the error source comes from the *.designer.cs file.

I guess I can fix that manually and remove all the reference.

But what's the correct way to do this?

Question by:fischermx
  • 2
  • 2
LVL 10

Expert Comment

ID: 35742351
I don't think there's a "correct" way to do this as much as there are multiple ways to do it.  If the ListBox is on the base form and you don't need it on the child form I'd remove it in code instead of the designer, e.g. this.Controls.Remove(myListBox) in the constructor.  Messing with designer files gets tricky and you could end up in a situation where the form stops rendering in the designer altogether - seen this happen before and a huge pain.

If the ListBox is not required on your child form, however, I'd question whether inheritance is architected correctly.

Author Comment

ID: 35742550

Yes, I didn't explain well.

I just temporarily put a Listbox to debug while I was learning how form inheritance work in .Net Winforms.
So, this was a one time operation.

But since I'm still learning and not finished to design the base forms of this application, while I'm testing, I might need to remove another control at some point.

So, my question is, really, how to remove a control -forever- without getting weird errors in child forms.
LVL 11

Accepted Solution

saragani earned 1200 total points
ID: 35743945
You probably have some event registered to the old listbox (maybe selection changed).
There is no easy way... Once you remove the ListBox by hitting delete, you need to compile, and look for all the errors which are probably because of code trying to use the old listbox.
(For example: listbox1.SelectionChangted += .... )

The other option is to search all code lines that contain the name of your listbox and delete it. Your search should also find the lines that define the control in the designer (so it will also remove it from your form).
LVL 10

Assisted Solution

Nash2334 earned 800 total points
ID: 35745617
The ListBox would only be modifiable inside of the child form if the protection on the base form was changed from private to protected, public, etc. in the designer.cs file of the base form.  Simply toggle protection level back to private in the base form and the compiler should point you to all issues in inherited forms, where you can manually delete the references.

If you changed a property or added an event to a child form, the designer would generate code for the inherited control.  The only way to get rid of this reliably is manually; removing events can be done in the designer pretty easily, but properties will sometimes leave vestigial code behind, even if toggled back to defaults.

Author Closing Comment

ID: 35805637
Thank you!

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question