Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

deriving from Windows Forms controls

Hi all,

Can anyone suggest the best way for me to derive classes from a control (specifically a ComboBox)

What I want is a subclass - lets call it MyComboBox - which simply adds a method to fill the combo box by using a supplied data structure:

class MyComboBox : ComboBox
{
    public void FillComboBox( MyDataStruct foo )
    {
    ...
    }
}

Now that I have this class, what's the best way to add it to a form which was created with the Form Designer in VS .NET? I could add a ComboBox to the form, then manually change to constructor from ComboBox() to MyComboBox(), but there has to be a better solution than that, right?

TIA,

Pete

Pete Hodgson
Friday, April 11, 2003

Pete,

Create the derived control in a class library project and then add the class library to your toolbox - you should see your new derived control in the toolbox, which you can then drag & drop onto a form.

Regards,
GiorgioG

GiorgioG
Saturday, April 12, 2003

Thanks Giorgio, I'll give that a try.

Is that the only way to do it? It seems a little over the top to create a class library project just for a class with one new method! Obviously for a full blown project it makes sense, but in this case.....

Thanks again,

Pete

Pete Hodgson
Saturday, April 12, 2003

Yep - to the best of my knowledge this is the only way to get Visual Studio .NET's visual designer stuff working consistantly with custom/derived controls.  You can manually change the object(s) constructor as you stated, the problem I've found is that VS.NET likes to manage that part of the code and often overwrites things I've modified.

In your situation, I agree that creating a class library might seem to be overdoing things, however - it's the easiest way I know of to do what you're trying to accomplish.

If you have any questions, feel free to instant message me on AIM - my screen name is "Giorgio Galante"

-Giorgio

GiorgioG
Sunday, April 13, 2003

Well, I did as you suggested and created a control library and add added the controls to the toolbox and it works fine.

One caveat that may be of use to anyone else doing the same: To add the control library to the toolbox you need to select the .dll - which means choosing between the debug and release versions of the library. As far as I know this choice is permanent, so even if you build a release version of your main project it will still be using the Debug version of you control library (assuming you included the debug .dll in the toolbox).

Pete Hodgson
Sunday, April 13, 2003

I'm going to take a guess (I'll try it later) and say that if you add the class library project to your solution and add a reference to the class library project rather than the debug assembly(dll) itself to your asp.net application project that you should be ok.  If my explaination makes no sense, I'll elaborate further - (hint: add reference -> project tab - I didn't even notice this tab for a good 3 months.)

GiorgioG
Sunday, April 13, 2003

I already had a reference setup. This puts UserControl-derived controls in the toolbox, but not my ComboBox-derived control. No idea why :| That's why I added it via the .dll

Possibly I have to set some attributes within my ComboBox-derived class to tell VS. NET designer that it should be included? Who knows!

Pete Hodgson
Sunday, April 13, 2003

Regarding bin\Debug and bin\Release, you could always modify the project that both debug and release builds build to the same directory. We do this for web projects already that are directly mapped into IIS, because ASP.NET will only look for binaries in ~/bin, not ~/bin/Debug or ~/bin/Release. It works fine for us...

Brad (dotnetguy.techieswithcats.com)
Sunday, April 13, 2003

*  Recent Topics

*  Fog Creek Home