C# treeview node - c#

I have a treeview1:
treenode--
treenode1--
treenode2
treenode3
You can select only node2 and node3 if you want to display them parameters. How can i block selecting node and node1? I try like that,but than you can select all nodes:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
...
}

you will have to use SelectAction Property on the tree node. you will need to set it on the node when you are creating them.
set selectaction=none on node and node1

If it's a win forms TreeView handle the BeforeSelect event and set the Cancel property of your TreeViewCancelEventArgs parameter to false. For web controls see Vinay's answer.

Related

How to get the name of the selected child node in a treeview list in c#?

I am using the command:
selecteddirectory = treeViewDirectory.SelectedNode.Text;
However this always gives the name of the parent node and not the child node selected.
How to go about doing this?
You can call the treeview_AfterSelect event to select the child node.
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
string selectedNodeText = e.Node.Text;
MessageBox.Show(selectedNodeText);
}
Like this:
selecteddirectory = treeViewDirectory.SelectedNode.Name;

Get an Object from a TreeView node in C#

I'm writing an AutoCAD Plug-in that shows all the entities as Nodes in a TreeView. I want to be able to get the object from the Node to be able to work with programmatically. This is my code for when a node is clicked:
private void treeView1_MouseClick(object sender, MouseEventArgs e)
{
TreeNode node = treeView1.SelectedNode;
propertyGrid1.SelectedObject = node;
/*Entity selectedEntity = node.getObject() as Entity; Pseudo-code, need to know how to do this*/
}
TreeNode has a Tag property that's designed to allow you to associate an object with a Node.

Set SelectAction on all tree nodes for ASP.NET TreeView control

I have a TreeView web control on an ASP page. I am programmatically populating all the tree nodes. I want to disable the link on ALL tree nodes. I can do it one at a time, like this (using a string array for simplicity's sake):
for each (string strValue in strValues)
{
TreeNode objNode = new TreeNode(strValue);
objNode.SelectAction = TreeNodeSelectAction.None;
objTreeView.Nodes.Add(objNode);
}
Assume for the sake of argument that I have multiple level of nodes, so there is not a simple way to iterate through all nodes once I am done populating. Is there a property I can set on the TreeView to set SelectAction for all nodes?
TreeView does not support any property to do this. However you can do it by using recursive methods
This should solve your problem:
protected void Page_Load(object sender, EventArgs e)
{
processNode(trvTest.Nodes);
}
private void processNode(TreeNodeCollection nodes)
{
foreach (TreeNode node in nodes)
{
node.SelectAction = TreeNodeSelectAction.None;
if (node.ChildNodes.Count > 0)
processNode(node.ChildNodes);
}
}

How to get Treeview's selected node's path as a treeview

I have a treeview. And i want to get the selected(clicked) node's path as a treeview again.
Is there any solution?
Example treeview;
.node1
.node1.1
.node1.2
.node2
.node2.1
.node2.2
.node2.3
.node2.3.1
.node2.3.2
when i click node2.3.2 than i want to get such a result:
node2
node2.3
node2.3.2
If you mean create another TreeView with the selected node as the root, then you'd clone the node, create a TreeView and then add the cloned node. I have a strange feeling that's not what you mean though....
Is this what you need?
TreeView ConvertTreeNodeToTreeView(TreeNode tn) {
TreeView tv = new TreeView();
tv.Nodes.Add(tn);
return tv;
}
protected void tv_SelectedNodeChanged(object sender, EventArgs e)
{
if (this.tv.SelectedNode != null) {
this.Panel1.Controls.Add(ConvertTreeNodeToTreeView(tv.SelectedNode));
}
}

Change TreeNode image on expand-collapse events

I have a treeView with many nodes. I want that some nodes change their image when node collapsed/expanded. How can I do it ?
Unfortunately, TreeNode don't have properties like ExpandNodeImage, CollapseNodeImage \
TreeView can change very often, so nodes can be deleted/added.. i can delete child nodes and so on...
Maybe, there is a class like ExpandAndCollapseNode ?
1). Add an ImageList Control to your WinForm.
2). Populate the ImageList with the pictures/icons you wish to change/display in response to what the user does at run-time with the TreeView, such as expanding, or collapsing nodes.
3). Assign the 'ImageList Control to the 'ImageList property of the 'TreeView
At this point you may want to make an initial pass over the TreeView, assuming it is populated, assigning the Node.ImageIndex property to point to the Image ... in the ImageList ... you want to use for the Node depending on whether it has children, or whatever.
4). If a user expands a Node, for example, you can use the BeforeExpand Event of the TreeView to change the Node's picture : like so : in this case we use the index of the Picture in the ImageList :
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
e.Node.ImageIndex = 3;
}
5) You can also set the Node's image by using the ImageKey property which is the string name of the Image
6) There lots of other possible Node Picture variations to use : check out : SelectedImageIndex and SelectedImageKey : You can change Node pictures in the BeforeSelect, AfterSelect and BeforeExpand, events also, depending on the effect you are after.
BeforeCollapse
BeforeExpand
AfterCollapse
AfterExpand
Use both ImageIndex & SelectedImageIndex:
private void treeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
e.Node.ImageIndex = 1;
e.Node.SelectedImageIndex = 1;
}
TreeViews have the following events that will be be fired when nodes are collapsed/expaned.
BeforeCollapse
BeforeExpand
AfterCollapse
AfterExpand
It's better to use :
treeNode.SelectedImageIndex = 1;
you can use the events AfterCollapse & AfterExpand (that are avilable on the TreeView itself) to modify the image of a node.
you can get the node using the TreeViewEventArgs input parameter:
private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)
{
e.Node.ImageIndex = 1;
}

Categories