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

Posted on 2011-05-11
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
    LVL 10

    Expert Comment

    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.
    LVL 1

    Author Comment


    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

    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

    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.
    LVL 1

    Author Closing Comment

    Thank you!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video discusses moving either the default database or any database to a new volume.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now