Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Comments
CHill60 23-Apr-14 7:38am    
Is this WinForms or Web?

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
C#
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
C#
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
C#
const int tblank = 2;
const int tChecked = 1;
const int tUnChecked = 0;

Note that you can't use the Checked property of the node now, you would need something like
C#
if (this.treeView1.Nodes[2].Nodes[1].ImageIndex == tChecked)
{
    //node is checked processing
}
else
{
    //node is unchecked processing
}
 
Share this answer
 
v4
Comments
VipinKumar Maurya 25-Apr-14 5:37am    
First I would like to thank you for your kind response
I tried this but the problem is the when I use
NodeMouseClick()
the node is not selected at the same time i.e image does not change at real time. It changes when another node is selected. Also I would like to inform you that on the right hand side of the tree vie I am displaying a pdf document. So the problem I am facing is that when i select the checkbox image then the document get displayed which I dont want. Whereas if we keep checkbox property to true and then perform the same with that check box it doesnt display the document (which I need).
CHill60 25-Apr-14 6:56am    
Ah, yes I'm afraid my "trick" won't work in those circumstances - although I didn't experience the problem with the image not changing (it changed immediately for me). The only other thing I can suggest is either creating a control from scratch or customising the painting of the standard control ... I found this book that might help on google books[^]
this C# code may help you
C#
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;
                    }
                }
            }
        }
 
Share this answer
 
Comments
CHill60 23-Apr-14 7:40am    
The checkboxes will also show at the parent node level (albeit unchecked)
Naz_Firdouse 23-Apr-14 7:58am    
could u plz share what is ur exact requirement? (may be a screenshot)
CHill60 23-Apr-14 8:58am    
You will have to ask the OP that
Naz_Firdouse 23-Apr-14 9:03am    
oops... sorry

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900