Hi adbarnet,
adbarnet said:
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.