Right Click Treeview

G

Guest

Hi all,

I have a treeview with 3 nodes at root level. After the TreeView loads, the first node has focus. I have a context menu that can be activated from the treeview. If I select say the last node via right click to get the context menu, focus moves to the last node the context menu opens, I select rename which calls Node.BeginEdit but focus has sprung back to the first node and it is in a state for editing the lablel.

Thanks in advance for any help
Pete.
 
D

Doug Forster

Hi Pete,

This is normal behaviour for a tree control (nothing to do with .NET). Right
clicking a node simply highlights it without changing focus. You need to
establish what node was clicked yourself (in the button down handler
probably) and ensure the context menu handler targets the correct node.
Alternatively you could code the button down handler to change the focus,
but this would be non-standard behaviour.

Cheers

Doug Forster

trinitypete said:
Hi all,

I have a treeview with 3 nodes at root level. After the TreeView loads,
the first node has focus. I have a context menu that can be activated from
the treeview. If I select say the last node via right click to get the
context menu, focus moves to the last node the context menu opens, I select
rename which calls Node.BeginEdit but focus has sprung back to the first
node and it is in a state for editing the lablel.
 
R

Rhett Gong [MSFT]

Hi Pete,
I agree with the answer from Doug.

Setting the current select node to treeview's selected node could meet your need. You
can workaround this by adding following lines. Here is a simple code, please let me
know if it could resolve your problem.

//--------------------------------------------------------------------------------------
-------------
private void treeView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
sNode = treeView1.GetNodeAt(e.X,e.Y); // get current selected node
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
if(sNode != null)
{
treeView1.SelectedNode = sNode; // set the current selected node to treeview's
selected node

treeView1.LabelEdit = true;
if(!sNode.IsEditing)
{
sNode.BeginEdit();
}
else
{
MessageBox.Show("No node selected");
}
}
}

//--------------------------------------------------------------------------------------
--------------


Have a great day!
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
G

Guest

Thanks guys - just wanted to make sure I had not missed a property that changed the behaviour of the right click.

Will probably implement similar to Rhetts suggestion.

Thanks for both inputs.

Pete
 
R

Rhett Gong [MSFT]

Glad to be of assist. Please feel free to let us know if you need assist. :)


Have a great day,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top