Question with treeview
-
First of all, hi to everybody, it´s my first post :) And sorry for my english I am from Spain. Well I am making a little app that write questions with their answers into an xml file. I am using a treeview to display the data. The question is the top node, and in it there are subnodes each for an answer. I know how to see which node was selected with this code:
if(this.IsNode(tree.GetNodeAt(this.tree.PointToClient(Cursor.Position)))==true)
Where IsNode is:private bool IsNode(TreeNode node) { try { string foo = node.Text; return true; } catch { return false; } }
My question is: How can I see if an subnode is selected or an node? I want to show an menu for answers and another for question. Thanks a lot :) -
First of all, hi to everybody, it´s my first post :) And sorry for my english I am from Spain. Well I am making a little app that write questions with their answers into an xml file. I am using a treeview to display the data. The question is the top node, and in it there are subnodes each for an answer. I know how to see which node was selected with this code:
if(this.IsNode(tree.GetNodeAt(this.tree.PointToClient(Cursor.Position)))==true)
Where IsNode is:private bool IsNode(TreeNode node) { try { string foo = node.Text; return true; } catch { return false; } }
My question is: How can I see if an subnode is selected or an node? I want to show an menu for answers and another for question. Thanks a lot :)You are doing it the hard way. ;) There is an easier way to get what you want, by using the
TreeView.SelectedNode
[^] property. This property returns the currently selectedTreeNode
, ornull
if no node is selected. Now you can test whether the returnedTreeNode
is a subnode. Remember that a root node doesn't have aParent
node, so we test that:public bool IsSubnodeSelected()
{
TreeNode selected = MyTreeView.SelectedNode;
if (selected != null)
{
if (selected.Parent != null)
return true;
}
return false;
}The above method would return
true
if a subnode is selected, andfalse
when a rootnode or no node is selected. - Daniël Pelsmaeker -
You are doing it the hard way. ;) There is an easier way to get what you want, by using the
TreeView.SelectedNode
[^] property. This property returns the currently selectedTreeNode
, ornull
if no node is selected. Now you can test whether the returnedTreeNode
is a subnode. Remember that a root node doesn't have aParent
node, so we test that:public bool IsSubnodeSelected()
{
TreeNode selected = MyTreeView.SelectedNode;
if (selected != null)
{
if (selected.Parent != null)
return true;
}
return false;
}The above method would return
true
if a subnode is selected, andfalse
when a rootnode or no node is selected. - Daniël PelsmaekerThanks a lot, it works but SelectedNode doesn´t work with right click so I use:
private bool IsSubNode() { TreeNode selected =treeview.GetNodeAt(this.treeview.PointToClient(Cursor.Position)); if (selected != null) { if (selected.Parent != null) return true; } return false; }
GetNoteAt with Current Mouse Position works with right click, now subnode works, and I can follow with my little app, thank you :)