I am using VS 2005 (.NET 2.0) C# working on a Winform with functionality of XML parsing to a TreeView.
I'm planning to enable the checkboxes of the treeview list. Actually, I already enabled them. I found this code here while looking for some properties of TreeView and TreeNode that might be of help. I found TreeNode.Checked Property and TreeView.CheckBoxes Property.
public void HighlightCheckedNodes()
{
int countIndex = 0;
string selectedNode = "Selected customer nodes are : ";
foreach (TreeNode myNode in myTreeView.Nodes[0].Nodes)
{
// Check whether the tree node is checked.
if(myNode.Checked)
{
// Set the node's backColor.
myNode.BackColor = Color.Yellow;
selectedNode += myNode.Text+" ";
countIndex++;
}
else
myNode.BackColor = Color.White;
}
if(countIndex > 0)
MessageBox.Show(selectedNode);
else
MessageBox.Show("No nodes are selected");
}
So I tried it on my test project and it only worked on the parent nodes, not on the child nodes. What I want to do and to happen is that I have to also determine the checked state of the childnodes. I'm working on a project that needs selection of single to multiple child nodes and executing an external program once after checked. Is this possible, with the childnodes? Or are there any other alternatives.
Let me know if I'm unclear on anything. Thank you in advance.
I think you need to change your code to following (It seems to me you have just used code from MSDN). Given code provide a sample answer you could adopt.
// Start from Root type node ; Loop them all
foreach (TreeNode rootNodes in treeView1.Nodes)
{
// Sub note set of current root
foreach (TreeNode childs in rootNodes.Nodes)
{
childs.Text = "Checked"; // Testing for validity : First level of child
/*
* Needs to go further down if we have childs of childs
foreach (TreeNode child_child in childs.Nodes)
{
child_child.Text = "Checked"; // Testing for validity : Second level of child
}
*/
}
}
Explanation :
Given example will change text for following structure (text Checked Applied to childs)
ROOT
|-> Child
|-> Child
ROOT
|-> Child
|-> Child
If you un-comment the inner most foreach you can can apply it to a structure like below ((text Checked Applied to childs))
ROOT
|-> Child
|-> Child
|-> Child
|-> Child
ROOT
|-> Child
|-> Child
In your code you don't progress further down a TreeNode and you have forgotten that each TreeNode could contains it's own child node collection.
Simply said,
myTreeView.Nodes[0].Nodes // <- This is wrong for what you want
Related
How to find the index of every root folder in a treeview?
Let's say there's a treeview with 4 root nodes. They're all on the same level and all of them have child nodes (not displayed):
|-a
|-b
|-c
|-d
Now let's suppose there's a selected node on the branch of the "c" root node. How could I get the index of the "c" node? (In this case, it's the third one between root nodes).
So, given a selected node, how could I get the index of its root node?
To achieve your task, you should find clicked node's parent and then parent's parent etc... So, we need a recursion here.
Take a look at sample code (with comments):
private void Form1_Load(object sender, EventArgs e)
{
//add test data on form load (you can do it on form design, too.
//there are 4 root nodes and each of them has one subnode.
//Additionally, c's first node, called 'c-1', has it's own child.
treeView1.Nodes.Add(new TreeNode("a"));
treeView1.Nodes.Add(new TreeNode("b"));
treeView1.Nodes.Add(new TreeNode("c"));
treeView1.Nodes.Add(new TreeNode("d"));
treeView1.Nodes[0].Nodes.Add(new TreeNode("a-1"));
treeView1.Nodes[1].Nodes.Add(new TreeNode("b-1"));
treeView1.Nodes[2].Nodes.Add(new TreeNode("c-1"));
treeView1.Nodes[3].Nodes.Add(new TreeNode("d-1"));
treeView1.Nodes[2].Nodes[0].Nodes.Add(new TreeNode("c-1-1"));
//expand those nodes to see things clearly
treeView1.ExpandAll();
//subscribe to after select event. When user selects one node, treeView1_AfterSelect method will be called.
//this can be done on form designer too, on properties panel
treeView1.AfterSelect += treeView1_AfterSelect;
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
//this method will be called when you select node
//find topmost parent by calling method FindTopMostParent and passing it selected node
var topmostParent = FindTopMostParent(e.Node);
//here's your index of topmost node (parent or grandparent or grand-grand of selcted node(
var index = treeView1.Nodes.IndexOf(topmostParent);
}
private TreeNode FindTopMostParent(TreeNode node)
{
//first, we check if passed node has parent. If not, return that node, because it's the topmost one
//then, repeat that search for parent again and again until you find that node which does not have parent
while (node.Parent != null)
return FindTopMostParent(node.Parent);
//return parentless node :)
return node;
}
This question already has answers here:
ASP.NET TreeView and loading data on demand
(2 answers)
Closed 6 years ago.
I have a treeview I'm using to display a directory structure. I am trying to reduce load times by loading sub nodes on node expansion. Is there a way to do this?
Below is the code I'm currently using to populate the treeview:
protected void Page_Load(object sender, EventArgs e) {
BuildTree(Request.QueryString["path"]);
}
private void BuildTree(string dirPath)
{
//get root directory
System.IO.DirectoryInfo rootDir = new System.IO.DirectoryInfo(dirPath);
//create and add the root node to the tree view
TreeNode rootNode = new TreeNode(rootDir.Name, rootDir.FullName);
TreeView1.Nodes.Add(rootNode);
//begin recursively traversing the directory structure
TraverseTree(rootDir, rootNode);
}
private void TraverseTree(System.IO.DirectoryInfo currentDir, TreeNode currentNode)
{
//loop through each sub-directory in the current one
foreach (System.IO.DirectoryInfo dir in currentDir.GetDirectories())
{
//create node and add to the tree view
TreeNode node = new TreeNode(dir.Name, dir.FullName);
currentNode.ChildNodes.Add(node);
//recursively call same method to go down the next level of the tree
TraverseTree(dir, node);
}
foreach (System.IO.FileInfo file in currentDir.GetFiles())
{
TreeNode node = new TreeNode(file.Name, file.FullName);
currentNode.ChildNodes.Add(node);
}
}
For loading the nodes on demand, meaning that the children of a node will be loaded only when the parent node is expanded. Do the following steps:
1 - Set the TreeView.ExpandDepth to 0. This eliminates the expansion of the added TreeNode objects in the TreeView and shows the expansion symbol [+] next to each TreeNode that has the TreeNode.PopulateOnDemand property set to true.
2- Set the TreeNode.PopulateOnDemand to True for each branch TreeNode. When the TreeNode.ChildNodes collection is empty, the expansion symbol [+] will be showed only next to TreeNode objects that has TreeNode.PopulateOnDemand property set to true.
3- Handle the TreeView.TreeNodePopulate event to poulate branch nodes on expansion. This event will be fired when a TreeNode - with the TreeNode.PopulateOnDemand set to true - has been expanded right before the TreeView.TreeNodeExpanded event gets fired.
Source: ASP.NET TreeView and loading data on demand
I have a treelist in my form with checkboxes enabled. I need to add anything checked to a list so I can write that list out. If I check any parent nodes (or highest level nodes) it includes them. If I select any parent nodes it also selects its children nodes (this is intentional), and displays those. But If I check any child nodes only, it won't add them to my list.
//check to see if there are any nodes checked
bool nodeHasCheck = false;
foreach (TreeNode n in nodes)
{
if (n.Checked)
{
nodeHasCheck = true;
break;
}
GetExtendedFeatures(n.Nodes);
}
//only return stuff if something's checked
if (nodeHasCheck == true)
{
foreach (TreeNode n in nodes)
{
if (n.Checked)
{
//n.BackColor = Color.Black;
nodeList.Add(n.Text);
}
GetExtendedFeatures(n.Nodes);
}
It also appears that if I select 2 parent nodes, the recursion that occurs (Think that's the right term) is causing it to find the first checked node, then starts over, and adds that same checked node a second time before it hits the second set of nodes.
I provided my node test, hopefully it's enough to identify why it's not detecting child nodes selected without the parent node selected.
UPDATED - 3/18/13
My button click code:
private void btnGenerate_Click(object sender, EventArgs e)
{
ScanNodes(treeView1.Nodes[0]);
}
private void ScanNodes(TreeNode parent)
{
foreach (TreeNode node in parent.Nodes)
{
if (node.Checked)
{
nodeList.Add(node.Text.ToString());
}
if (node.Nodes.Count > 0)
{
ScanNodes(node);
}
}
var message = string.Join(Environment.NewLine, nodeList);
message = message.Replace(Environment.NewLine, ", ");
MessageBox.Show(message);
nodeList.Clear();
}
To get a List of all the selected nodes in a TreeView you can use the following:
Supose the list is named nodeList:
//We First declare a recursive method to loop through all nodes,
//we need to pass a root node to start
private void ScanNodes(TreeNode parent)
{
foreach (TreeNode node in parent.Nodes)
{
if (node.Checked)
{
nodeList.Add(node.Text);
}
if (node.Nodes.Count > 0)
{
ScanNodes(node);
}
}
}
With that set up You just need to call the ScanNodes method and pass the root node of your TreeView:
ScanNodes(treeView1.Nodes[0]);
Regards,
I have treeView control in my web page which has three parent nodes Parent1, Parent2, Parent3. Then how i will add the child at runtime.
Thanks.
You can add parent and child dynamically by creating the objects of TreeNode.
i.e.
foreach (TreeNode objTN in TreeView1.Nodes)
{
if (objTN.Text == "You Parent Name")
{
TreeNode child = new TreeNode();
child.Text = "childText";
objTN.ChildNodes.Add(child);
}
}
Please refer link Adding children to a treenode dynamically in c# for more details.
I've added checkboxes to my treeview, and am using the AfterSelect event (also tried AfterChecked).
My tree view is like this
1 State
1.1 City1
1.2 City2
1.3 City3
2 State
2.1 City1
2.2 City2
2.3 City3
etc.
I'm trying to run an event, so when a checkbox is clicked, the tag is added to an array ready for processing later. I also need to use it so if a state is clicked, it selects all the cities under that leaf.
treeSections.AfterSelect += node_AfterCheck;
private void node_AfterCheck(object sender, TreeViewEventArgs e)
{
MessageBox.Show("testing");
}
The above code works on the treeview if it has no heirarchy. But don't work on the treeview with the states and cities unless the text/label for each leaf is double clicked.
Any ideas?
I suggest using the combination of TreeView.NodeMouseClick and TreeView.KeyUp events... the click event will provide you the clicked node via event args and with the keyup you can use the currently selected node. Follow the example below...
//This is basic - you may need to modify logically to fit your needs
void ManageTreeChecked(TreeNode node)
{
foreach(TreeNode n in node.Nodes)
{
n.Checked = node.Checked;
}
}
private void convTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
ManageTreeChecked(e.Node);
}
private void convTreeView_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
ManageTreeChecked(convTreeView.SelectedNode);
}
}
Using the node given each event you can now cycle through the Nodes collection on that node and modify it to be checked/unchecked given the status of the checked status of the node you acted upon.
You can even get fancy enough to uncheck a parent node when all child nodes are unchecked. If you desire a 3-state treenode (All Checked, Some Checked and None Checked) then you have to create it or find one that has been created.
Enjoy, best of luck.
Some code for you to consider :
Not considered here :
what to about which node is selected when checking, when a child node selection forces a parent node to be selected (because all other child nodes are selected).
could be other cases related to selection not considered here.
Assumptions :
you are in a TreeView with a single-node selection mode
only two levels of depth, as in OP's sample ("heavy duty" recursion not required)
everything done with the mouse only : extra actions like keyboard keypress not required.
if all child nodes are checked, parent node is auto-checked
unchecking any child node will uncheck a checked parent node
checking or unchecking the parent node will set all child nodes to the same check-state
...
// the current Node in AfterSelect
private TreeNode currentNode;
// flag to prevent recursion
private bool dontRecurse;
// boolean used in testing if all child nodes are checked
private bool isChecked;
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
// prevent recursion here
if (dontRecurse) return;
// set the currentNode
currentNode = e.Node;
// for debugging
//Console.WriteLine("after check node = " + currentNode.Text);
// select or unselect the current node depending on checkstate
if (currentNode.Checked)
{
treeView1.SelectedNode = currentNode;
}
else
{
treeView1.SelectedNode = null;
}
if(currentNode.Nodes.Count > 0)
{
// node with children : make the child nodes
// checked state match the parents
foreach (TreeNode theNode in currentNode.Nodes)
{
theNode.Checked = currentNode.Checked;
}
}
else
{
// assume a child node is selected here
// i.e., we assume no root level nodes without children
if (!currentNode.Checked)
{
// the child node is unchecked : uncheck the parent node
dontRecurse = true;
currentNode.Parent.Checked = false;
dontRecurse = false;
}
else
{
// the child node is checked : check the parent node
// if all other siblings are checked
// check the parent node
dontRecurse = true;
isChecked = true;
foreach(TreeNode theNode in currentNode.Parent.Nodes)
{
if(theNode != currentNode)
{
if (!theNode.Checked) isChecked = false;
}
}
if (isChecked) currentNode.Parent.Checked = true;
dontRecurse = false;
}
}
}