如何在VB.net中为树视图的最内层节点分配复选框? [英] How to assign checkboxes to the innermost nodes of the treeview in VB.net?

查看:122
本文介绍了如何在VB.net中为树视图的最内层节点分配复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想将复选框分配给树的最里面的节点。

我搜索了很多但没有得到必要的解决方案。

提前谢谢。

I want to assign check boxes only to the innermost nodes of the tree.
I googled a lot but did not got a required solution.
Thanks in advance.

推荐答案

你可以从头开始构建一个TreeView控件(这里有几个例子,包括CodeProject)。



我刚刚使用的一个技巧如下......



创建3个图像 - 一个空白的框,一个勾选(选中)框和一个(小)空白图像。

将这些添加到ImageList控件并将TreeView的ImageList属性设置为此ImageList控件。



在填充TreeView时,将 SelectedImageIndex ImageIndex 值设置为空白图像的索引(在我的情况下为2) ,以及子节点与其中一个已检查或未经检查的图像的等效属性 - 两者都是相同的值。



例如我填充了我的样本
Well you could build a TreeView control from scratch (there are several examples available including here on CodeProject).

One "trick" I've just used is as follows...

Create 3 images - a blank "box", a ticked (checked) box, and a (small) blank image.
Add these to an ImageList control and set your TreeView's ImageList property to this ImageList control.

When populating your TreeView set the SelectedImageIndex and ImageIndex values to the index of the blank image (2 in my case), and the equivalent properties of the child nodes to one of the checked or unchecked images - NB both the same value.

For example I populated my sample like this
for (int i = 0; i < 10; i++)
{
    this.treeView1.CheckBoxes = false;
    TreeNode tn = this.treeView1.Nodes.Add(i.ToString());
    tn.ImageIndex = tBlank;
    tn.SelectedImageIndex = tblank;
    for (int j = 0; j < 5; j++)
    {
        tn = this.treeView1.Nodes[i].Nodes.Add(i.ToString() + ":" + j.ToString());
        tn.ImageIndex = tChecked;
        tn.SelectedImageIndex = tChecked;
    }
}



现在您需要自己处理检查/取消选中...例如


Now you need to handle the check/uncheck yourself ... for example

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
            TreeNode tn = e.Node;
            if (tn.Level != 0)   // i.e. if a child node is clicked
            {
                tn.SelectedImageIndex = (tn.SelectedImageIndex == tUnChecked) ? tChecked : tUnChecked;
                tn.ImageIndex = tn.SelectedImageIndex;
            }



我已经定义了


I've already defined

const int tblank = 2;
const int tChecked = 1;
const int tUnChecked = 0;



请注意,您现在无法使用节点的 Checked 属性,您需要类似的东西


Note that you can't use the Checked property of the node now, you would need something like

if (this.treeView1.Nodes[2].Nodes[1].ImageIndex == tChecked)
{
    //node is checked processing
}
else
{
    //node is unchecked processing
}


这个C#代码可以帮助你

this C# code may help you
foreach (TreeNode parent in TreeView1.Nodes)
        {
            foreach (TreeNode child in parent.ChildNodes)
            {
                for (int k = 0; k < dt.Tables[0].Rows.Count; k++)
                {
                    if (child.Value == "XXX")
                    {
                        child.Checked = true;
                        break;
                    }
                }
            }
        }


这篇关于如何在VB.net中为树视图的最内层节点分配复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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