Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tree View 1

Status
Not open for further replies.

seanbo

Programmer
Jun 6, 2003
407
0
0
GB
How can i attach a menu to a node, so that when i right click the node i get a list of other things to click?
 
I think what you're looking for is a Context Menu. You can choose it from the Windows Forms, and then associate it in the TreeView in the TreeView's properties (assuming you have VS installed). Then you can fill it in at design time, or do it at runtime based on the info in this.treeView1.SelectedNode.

Here's the MSDN on it:

 
that's really great slanty, thanks.

how can i make the menu contents dependent on what node they relate too?
 
also, and more importantly, this code:

private void menuItem6_Click(object sender, System.EventArgs e)
{
treeView1.SelectedNode.Remove();
}

deletes the selected node, and is called by right clicking on a node then selecting delete. what i actually want to do is delete the node that was right clicked on. how do i do this? i assume i need to make the node under the mouse pointer selected, then delete it, but how?
 
Ok, I went through this as well and I came up with a solution. Don't know if it's the best one or not, but it works. The first thing you need to do is have have a MouseDown event handler for the TreeView.

in InitializeComponents

this.treeModify.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeMouseDown);

Then:

private void treeMouseDown(object sender, MouseEventArgs e)
{
try
{
if (e.Button == MouseButtons.Right)
{
this.treeModify.SelectedNode = this.treeModify.GetNodeAt(e.X, e.Y );
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message)
}
}

That will make sure that whatever node you right click on will be the SelectedNode when you go to use your ContextMenu. This seems a bit ugly to me, but it was the only way I could get it to work. Any other suggestions are welcome =).
 
ta. i've actually decided to do this...

private void treeView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
myNode = treeView1.GetNodeAt(e.X, e.Y);
}

then use 'myNode' rather than the selected node. that way i don't alter the selected node (which will be handy later).
 
here's a new one for you though. how can i change the order of nodes within their respected parent (increment, decrement, move to top, move to bottom)?
 
Allrighty, this one I haven't really done before, but I reckon you could do something like:

private void moveNode(TreeNode myNode, int NodeToMove, int WhereToMoveIt)
{
myNode[WhereToMoveIt] = (TreeNode)myNode[NodeToMove].Clone();
myNode[NodeToMove].Remove();
}

So if you wanted to move a node to the top say, you would call it as so:
moveNode(myNode, myNode.SelectedNode.Index, 0);

Or something along those lines anyway. That's sort of off the top of my head so I don't recommend cutting and pasting that code directly =), but it seems to me it should work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top