470,624 Members | 2,316 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,624 developers. It's quick & easy.

User Control v Inherited form

Hi,

I'm a C++ developer coming fresh to c# for GUI development. For the project
I'm working on, I need a specialised control (a treeview derivative) which
would be very useful in other applications I might need to write.

Back in my day ;-) I would have written a user control for use from within
VB, which I could still do, however - I also have the (more appealing from
my perspective) option of deriving a control from the .NET base control.

What is the popular wisdom on these two alternatives?

thanks,

AD

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 17 '05 #1
2 4683
Hi,

I use the user control in order to merge several controls and to handle the
UI threads within the same environment. Now, If I need to upgrade the
functionality of a base control I will create a component that inherits from
that base class.

One tip, in order to your control appear on the toolbox first add a
usercontrol and then change the inheritence your treeview. Also C# is giving
you the opportunity to design your own from scratch inheriting from component.

hope this helps
Salva
"adbarnet" wrote:
Hi,

I'm a C++ developer coming fresh to c# for GUI development. For the project
I'm working on, I need a specialised control (a treeview derivative) which
would be very useful in other applications I might need to write.

Back in my day ;-) I would have written a user control for use from within
VB, which I could still do, however - I also have the (more appealing from
my perspective) option of deriving a control from the .NET base control.

What is the popular wisdom on these two alternatives?

thanks,

AD

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 17 '05 #2
Hi adbarnet,

"adbarnet" wrote:
Hi,

I'm a C++ developer coming fresh to c# for GUI development. For the project
I'm working on, I need a specialised control (a treeview derivative) which
would be very useful in other applications I might need to write.

Back in my day ;-) I would have written a user control for use from within
VB, which I could still do, however - I also have the (more appealing from
my perspective) option of deriving a control from the .NET base control.

What is the popular wisdom on these two alternatives?

thanks,
AD


I have created several specialised controls by deriving from UserControl, it
adds an extra level of indirection as the specialised control(s) would be
embedded in the UserControl instance. This has the benefit that it shields
the user from how you made the control (although not entirely perhaps), and
prevents abuse of the control (invalid entries etc). If you would like to
still expose the underlying control(s) (in your case a treeview), you could
expose a property that does this.

Any properties you make in the UserControl derived class are also displayed
in the properties tab for an instance of the control on a form.

You can add the user control to the form designer toolbox (contextmenu/add
controls); or if the control is in the same project I think it is added to
the User Control tab in the toolbox. If the IDE doesn't do it automatically
then you can still add it yourself, drag the control on the form and set some
properties.

I once derived from DataGrid to make my specialised control, but I noticed
that all initialization code from the control in InitializeComponent, was
added to the InitializeComponent method for the form that contained the
control. I didn't really like that because that was a lot of code (you would
know if you use DataGrid). I still don't know the reason for this. I could
have put the initialization somewhere else but that didn't seem a like good
idea. I ended up making a UserControl for my specialized control, but more or
less for another reason. My control was actually a logging table with no
other real functionality, so there was no need to directly expose the
underlying DataGrid.

So IMHO your choice should depend on the nature of the control you're
making. If it enriches or extends functionality you should derive from the
control directly. If it restricts functionality to a specific user-interface
then you should make it a UserControl.

Hope this helps,
Tom T.
Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Terrance | last post: by
reply views Thread by Jim | last post: by
2 posts views Thread by Ryan Liu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.