Able To Have Generic User Controls in .net 2.0? / Winforms and Generics

P

Peter Nofelt

Hey All,

I'm wondering if I'm able to have create generic user controls in .net
2.0 much like one can create generic classes.

I would like to do this so that I can handle a set a types derived from
the same base type within a user control

Consider the following scenario:

* I create a base user control that contains a generic <ItemType>.
* Note: there exists a where clause referencing a specific base type
(see code below).
* Displays info associated to base class via specific methods.
* I derive another user control from this base control with a concrete
type
* Derived control now displays info associated with the concrete
type.

Please let me know the following:

* If this is possible
* If you have done this, what caveats exist
* Point me to any online references about the topic

Cheers,
peter

Code
=============================
== BASE USER CONTROL ==
namespace GenericWinformTest
{
public partial class UserControl1<ItemType> : UserControl
where ItemType : Animal, new()
{
internal Animal itype;
public UserControl1()
{

InitializeComponent();
itype = new ItemType();
}

private void talk_Click(object sender, EventArgs e)
{
this.textBox1.Text = itype.talk();
//talk
}
}//end class
}//end namespace

== Derived user control ==
namespace GenericWinformTest
{
public partial class dog : UserControl1<type.Dog>
{
public dog()
{

InitializeComponent();

}
}//end class
}//end namespace
 
F

Frans Bouma [C# MVP]

Peter said:
Hey All,

I'm wondering if I'm able to have create generic user controls in .net
2.0 much like one can create generic classes.

I would like to do this so that I can handle a set a types derived
from the same base type within a user control

Consider the following scenario:

* I create a base user control that contains a generic <ItemType>.
* Note: there exists a where clause referencing a specific base
type (see code below).
* Displays info associated to base class via specific methods.
* I derive another user control from this base control with a concrete
type
* Derived control now displays info associated with the concrete
type.

Please let me know the following:

* If this is possible
* If you have done this, what caveats exist
* Point me to any online references about the topic

Cheers,
peter

Code
=============================
== BASE USER CONTROL ==
namespace GenericWinformTest
{
public partial class UserControl1<ItemType> : UserControl
where ItemType : Animal, new()
{
internal Animal itype;
public UserControl1()
{

InitializeComponent();
itype = new ItemType();
}

private void talk_Click(object sender, EventArgs e)
{
this.textBox1.Text = itype.talk();
//talk
}
}//end class
}//end namespace

== Derived user control ==
namespace GenericWinformTest
{
public partial class dog : UserControl1<type.Dog>
{
public dog()
{

InitializeComponent();

}
}//end class
}//end namespace

Keep in mind that the design-time engine of vs.net doesn't support
generics, or very poorly. So if you always have to work with
non-generic types.

FB

--
 

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