Inherit from a ComboBox control

S

Spurry Moses

Hi,

I'm converting a project from C++Builder to C# and I just need to copy my
class design across. It's looking good so far. To stick with the design I
want to inherit from a ComboBox control (ie MyComboBox). I don't need to
do anything flash with it, just add a few properties and methods. I'd
almost solved this while writing this post but I'll just confirm what I'm
doing is OK.

It was a bit awkward in Visual Studio because all of it's auto-generated
ways of creating controls use them as containers. And the one obvious
option that should have worked "Add inherited Control" failed looking for
"built assemblies". It must not be solution to my problem, I guess ;-)

Anyhow, to get my new control to show up in the toolbox, I have to "Add
User Control" and then delete all the code generated by it and replace it
with this.

public class MyComboBox : System.Windows.Forms.ComboBox
{
public MyComboBox() {
// TODO: Add any initialization after the InitializeComponent call
}
}

From there it basically seems to work. The combo box shows, I can put it
in the form and all seems well. Although I haven't tested it much yet. Is
there anything I'm missing like some Painting events might get screwed if
I don't add some method or anything?

I'll be happy if this is it!
 
C

Claudio Grazioli

Hi,

I'm converting a project from C++Builder to C# and I just need to copy my
class design across. It's looking good so far. To stick with the design I
want to inherit from a ComboBox control (ie MyComboBox). I don't need to
do anything flash with it, just add a few properties and methods. I'd
almost solved this while writing this post but I'll just confirm what I'm
doing is OK.

It was a bit awkward in Visual Studio because all of it's auto-generated
ways of creating controls use them as containers. And the one obvious
option that should have worked "Add inherited Control" failed looking for
"built assemblies". It must not be solution to my problem, I guess ;-)

Anyhow, to get my new control to show up in the toolbox, I have to "Add
User Control" and then delete all the code generated by it and replace it
with this.

public class MyComboBox : System.Windows.Forms.ComboBox
{
public MyComboBox() {
// TODO: Add any initialization after the InitializeComponent call
}
}

From there it basically seems to work. The combo box shows, I can put it
in the form and all seems well. Although I haven't tested it much yet. Is
there anything I'm missing like some Painting events might get screwed if
I don't add some method or anything?

I'll be happy if this is it!

I'd say that's it. You can inherit from a control exactly that way and just
add methods/properties you need. Painting and everything is done by the
parent control so no need to add anything special.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top