I have created a user control consists of a checkbox
control and a string which holds another value I would like to save for each checkbox
.
The user is able to select or deselect the checkboxes.
public partial class UserControl1 : UserControl
{
private CheckBox c = new CheckBox();
private string EAType;
}
However, there are a lot of them and the Form layout
looks messy.
Therefore, I would like to classify them by criteria, organizing them in a treeview
.
This is quite easy to set the treeview
to checkbox-treeview
, but, as mentioned, I have another string to store for each.
Is a usercontrol treeview
possible? If yes, how?
Any other ideas are welcome...
Do you need a
UserControl
just to store a string?To add custom information to a
Control
you always haveTag
property and to add custom information to aTreeNode
you haveTreeNode.Tag
property (there you can store what you want, a class or directly your string).In this example I have an hypothetical collection
objects
,Title
property will determineTreeNode
caption and I store another custom value (fromValue
property) intoTreeNode.Tag
property. If you want you can directly store object itself:To access them you'll need to cast to proper type, for example (with strings):
Or, with objects:
Finally note that you may even use other list types:
ListBox
with checkboxes. Each item is anobject
and then it can be anything you want, just overrideToString()
to provide proper display text.ListView
. Same technique we saw forTreeView
, eachListViewItem
has aTag
property you can use to store additional information. If you need just one level (multiple groups with a set of items) it may be more convenient than a tree view because customization is easier.Whichever list you choose or you don't choose and you go with a plain
CheckBox
you don't really need aUserControl
, in every control you have aTag
property for this purpose and for more complex situations you can derive directly from right control (CheckBox
, for example) and add properties/methods you need.UserControl
is (usually) used for composition when you need to have a control made of multiple ones for example to reuse some complex UI.