CheckBox的树形 [英] Treeview of Checkbox

查看:229
本文介绍了CheckBox的树形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个用户控件由复选框控制和持有另一个值,我想保存每个字符串复选框

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?

要自定义信息添加到控制你总是有标签财产和自定义信息添加到树节点你有 TreeNode.Tag 属性(也可以存储你想要什么,类或直接将字符串)。

To add custom information to a Control you always have Tag property and to add custom information to a TreeNode you have TreeNode.Tag property (there you can store what you want, a class or directly your string).

foreach (var obj in objects)
    treeView.Nodes.Add(new TreeNode { Text = obj.Title, Tag = obj.Value });

在这个例子中我有一个假设的集合对象标题属性将决定树节点标题和我存储其他自定义值(从属性)为 TreeNode.Tag 属性。如果你愿意,你可以直接存储对象本身:

In this example I have an hypothetical collection objects, Title property will determine TreeNode caption and I store another custom value (from Value property) into TreeNode.Tag property. If you want you can directly store object itself:

foreach (var obj in objects)
    treeView.Nodes.Add(new TreeNode { Text = obj.Title, Tag = obj });

要访问它们,你需要投以适当的类型,例如(使用字符串):

To access them you'll need to cast to proper type, for example (with strings):

var checkedNodes = treeView.Nodes.Cast<TreeNode>().Where(x => x.Checked);
var selectedValues = checkedNodes.Select(x => Convert.ToString(x.Tag));

或者,使用对象:

var selectedValues = treeView.Nodes
    .Cast<TreeNode>()
    .Where(x => x.Checked)
    .Select(x => (YourObject)x.Tag);



最后请注意,你甚至可以使用其他列表类型:

Finally note that you may even use other list types:


  • 对于您可以使用的ListBox 与复选框值的简单列表。每个项目都是一个对象,然后它可以是你想要的,只是重写任何的ToString()来提供适当的显示文本

  • 对于更复杂的列表(或者你想在团体举办更多的项目),你可以使用的ListView 。同样的技术,我们看到了 TreeView控件,每个 ListViewItem的有一个标签属性,你可以用它来存储更​​多的信息。如果你需要(与一组项目的多组)只是一个层面,可能比树视图更加方便,因为定制是更容易。

  • For a simple list of values you can use ListBox with checkboxes. Each item is an object and then it can be anything you want, just override ToString() to provide proper display text.
  • For a more complex list (or with more items you want to organize in groups) you can use a ListView. Same technique we saw for TreeView, each ListViewItem has a Tag 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.

您选择哪个列表或你没有选择,你去与一个普通的复选框你并不真的需要一个用户控件,在每一个控制你有一个标签属性用于此目的,并为更复杂的情况下,你可以直接从右侧控制(派生复选框,例如),你需要添加属性/方法。 用户控件是用于组成,当你需要为例子来重用某些复杂的UI做多的人的控制(通常情况下)。

Whichever list you choose or you don't choose and you go with a plain CheckBox you don't really need a UserControl, in every control you have a Tag 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.

这篇关于CheckBox的树形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆