What event to capture when a TreeVew node is checked/unchecked - c#

I have a node cbNode5 in a TreeView that contains 5 child nodes and I am trying to somehow capture the checking and unchecking of cbNode5 so I can mark the child nodes and check/unchecked to match the parent node. I know how to work though the nodes to check/uncheck the child nodes but what I can not figure out is what, and how to capture, the event that fires when the user checks the checkbox for a nod via either mouse click or keyboard.
I have tried the AfterCheck event but it does not seem to be working (and I do know the line to set the check box to true works as it runs fine under other events):
private void tvSteps_AfterCheck(object sender, TreeViewEventArgs e)
{
tvSteps.Nodes["cbStep1"].Checked = true;
}

Look at the AfterCheck event...
The AfterCheck() event is working fine for me:
private void tvSteps_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Node.Name == "cbNode5")
{
foreach (TreeNode tn in e.Node.Nodes)
{
tn.Checked = e.Node.Checked;
}
}
}
When I check/uncheck cbNode5, its children check/uncheck to match it.
Are you sure the key you are using is correct?

I'd like to note that I resolved this problem by going into my TreeViewForm.Designer.CS, InitializeComponent function and adding the following line into the properties of the treeview object causing the problem:
this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);
Note that your object names will differ from my own.
For some reason the AfterSelect event handler was added programatically to my form's Designer.CS file but my AfterCheck event handler was not.

Try this
TreeView.AfterCheck Event

To accomplish this task, you can handle the TreeList.AfterCheckNode event and set the TreeListNode.Checked property manually. Here is a sample code snippet:
private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
{
if (e.Node.ParentNode != null)
e.Node.ParentNode.Checked = IsAllChecked(e.Node.ParentNode.Nodes);
else
SetCheckedChildNodes(e.Node.Nodes);
}
private void SetCheckedChildNodes(TreeListNodes nodes)
{
foreach (TreeListNode node in nodes)
node.Checked = node.ParentNode.Checked;
}
private bool IsAllChecked(DevExpress.XtraTreeList.Nodes.TreeListNodes nodes)
{
bool value = true;
foreach (TreeListNode node in nodes)
{
if (!node.Checked)
{
value = false;
break;
}
}
return value;
}

Related

In C#, how to set all tree nodes checked true just after treeview loaded [duplicate]

This question already has an answer here:
How to Check or Uncheck All Child Nodes in TreeView
(1 answer)
Closed 3 years ago.
I have a TreeView in a WindowForm build in C#. In that treeview, files are being loaded successfully. I've also implemented checkboxes, showing in front of each tree node.
Now, I want all checkboxes to be checked true by default after all directories are loaded in treeview.
I tried the following code but no luck. It just checks true only the root node, not all the sub-nodes.
The first method btnDirectoryPath_Click(object sender, EventArgs e) is for button click which selects the path of the directory being loaded. From here, I am calling setAllCheckedTrue(TreeView treeView1)
private void btnDirectoryPath_Click(object sender, EventArgs e)
{
folderBrowserDialog1.SelectedPath = txtDirectoryPath.Text;
DialogResult drResult = folderBrowserDialog1.ShowDialog();
if (drResult == System.Windows.Forms.DialogResult.OK)
{
txtDirectoryPath.Text = folderBrowserDialog1.SelectedPath;
// Setting Inital Value of Progress Bar
progressBar1.Value = 0;
// Clear All Nodes if Already Exists
treeView1.Nodes.Clear();
toolTip1.ShowAlways = true;
if (txtDirectoryPath.Text != "" && Directory.Exists(txtDirectoryPath.Text))
{
//Loading all directories and sub directories and files
LoadDirectory(txtDirectoryPath.Text);
//setting all checkboxes true by default on loading.
setAllCheckedTrue(treeView1);
}
else
MessageBox.Show("Select Directory!!");
}
}
private void setAllCheckedTrue(TreeView treeView1)
{
foreach(TreeNode treeNode in treeView1.Nodes)
{
treeNode.Checked = true;
}
}
Following is snapshot of my treeview -
Another doubt is, How to handle on treeview load event? Is there any specific thing in c# to detect treeview load event?
I am a beginner to C#, Please help me out if possible. Thanks!
You need a recursive function to check all the subnodes as well. The TreeView does not do this automatically.
You also have to consider how to handle the update of parent nodes, when you unselect a subnode.
Try something like this for setting the subnodes as well:
private void SetAllCheckedTrue(TreeView treeView1)
{
foreach(TreeNode treeNode in treeView1.Nodes)
{
SetTreeNodeCheckbox(treeNode , true);
}
}
private void SetTreeNodeCheckbox(TreeNode treeNode , bool value)
{
treeNode.Checked = value;
foreach(var subNode in currentNode)
{
SetTreeNodeCheckbox(subNode, value);
}
}
First replace the below code
setAllCheckedTrue(treeView1);
with the below instructions
foreach(TreeNode node in treeView1.Nodes)
{
setAllCheckedTrue (node);
}
This will change the type of parameter sent to function "setAllCheckedTrue" that will become a node and not a treeview, and update the setAllCheckedTrue function with the below function
private void setAllCheckedTrue(TreeNode node)
{
node.Checked = true;
foreach(TreeNode childnode in node.Nodes)
{
setAllCheckedTrue (childnode);
}
}
Cordially

Check Treeview Nodes

I use a treeview of checkboxes.
The root nodes specify the group name.
When the user check the root node I would like to check all its nested notes.
Therefore, I would like to use the CheckChanged event of CheckBox in the treeview.
Is it possible? If so, how?
You should use AfterCheck event and another help procedure, called recursively. Something like this:
private void tvwTest_AfterCheck(object sender, TreeViewEventArgs e)
{
checkNodes(e.Node);
}
private void checkNodes(TreeNode root)
{
foreach (TreeNode node in root.Nodes)
{
node.Checked = root.Checked;
checkNodes(node);
}
}

Treeview.selectednode gives wrong value

I have a treeview which have several childs and many of them have ancestors.
I also have a datagridview along side the treeview. Based on treeview selected node, I bind data in Datagridview.
I use treeView1.SelectedNode to get the node value
My problem is that this expression give the previously selected node.
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
node = e.node;
}
this give me correct node (selected node).
but
node = treeview1.selectednode give the node which I selected earlier.
I want to get the selected node outside the click event (_nodemouseclick).
How can I get It.
I dont want to save node in global variable then use it.
Hope I am clear to explain my question.
Thanks in anticipation.
TreeNode yourGlobalTreeNode;
void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
yourGlobalTreeNode = e.Node;
otherFunction();
anOtherFunction(e.Node);
}
void otherFunction()
{
MessageBox.Show(yourGlobalTreeNode.Text);
}
void anOtherFunction(TreeNode tn)
{
MessageBox.Show(tn.Text);
}
Other Function means you can use this tree_node anywhere using above two methods.
It is because MouseClick event is raised before selectedNode is changed. Try to use SelectedItemChanged event instead
private void TreeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
}

Is there any event which Fires when observes that a node from TreeList had the check mark checked from code?

I have a part of code which analyzes a list with checked nodes ID's, and checks into the a TreeList existing ID's. (I'm using a XtraTreeList control)
I want to calculate amount for each checked node, and I just though to make this when the node is checked.
Is there any event which observes that a node from TreeList was checked from code (programmatic)?
Cause if I check/uncheck a node with the mouse, or with the keyboard BeforeCheckNode and AfterCheckNode events takes Fire, but when i check the node from code - they don't fires.
foreach (TreeListNode item in tln) {
var nodeID = (this.tlServices.GetDataRecordByNode(item) as __ServiceInfo).ID;
if (svc.Select(value => value.Model.service.id).Contains(nodeID)) {
item.Checked = true;
}
else if (item.HasChildren) {
this.FindNode(item.Nodes, svc);
}
}
You can use the TreeList.NodeChanged event:
void treeList1_NodeChanged(object sender, NodeChangedEventArgs e) {
if(e.ChangeType == NodeChangeTypeEnum.CheckedState) {
// do something
}
}
AfterCheckNode is the event.
private void _tree_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
{ TreeListNode node = e.Node as TreeListNode;}

TreeNode Selection Problems in C#

Whenever I click outside the tree nodes text, on the control part, it tigers a node click event- but doesn't highlight the node. I am unsure why this is happening.
I want the node to be selected on a click- when you click the nodes text- not the whitespace- I only assume that the nodes width reaches across the whole Treenode? I have the Treeview on dock.fill mode if that has something to do with it- I tried everything but can't get it to behave correctly.
Maybe someone will know what's going on.
Update:
if (e.Location.IsEmpty)
{
Seems to work better- but still selects the node in the blank place where there is no text- Obviously the node width extends across the whole treeview it seems?
Is there a better way to accomplish what I want? Or is that the best way?
UPDATE: Previous idea isn't working- sigh- I thought it did it but it didn't.
New Problem : I think part of the problem is related to the focus now when I switch from treeview.
UPDATE-
The only code I came up with about disabling right mouse click to select node on beforeSelect event is
if (MouseButtons == System.Windows.Forms.MouseButtons.Right)
{
e.Cancel = true;
}
But it didn't work- any help is appreciated- following suggestions of only answer, for more details.
You should use the treeView.HitTest method to determine which part of the node has been clicked.
private bool IsClickOnText(TreeView treeView, TreeNode node, Point location)
{
var hitTest = treeView1.HitTest(location);
return hitTest.Node == node
&& hitTest.Location == TreeViewHitTestLocations.Label;
}
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if(IsClickOnText(treeView1, e.Node, e.Location))
{
MessageBox.Show("click");
}
}
private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
if (e.Action == TreeViewAction.ByMouse)
{
var position = treeView1.PointToClient(Cursor.Position);
e.Cancel = !IsClickOnText(treeView1, e.Node, position);
}
}
Use the .AfterSelect and/or .BeforeSelect events to handle the selection processing instead of the .Click event. Then it will select the node only when you click on the text, and it won't fire .AfterSelect or .BeforeSelect when you click on the white space.

Categories

Resources