tags:

views:

460

answers:

5

Hello guys, (maybe girls)

I have a question.

I put a treeview control on my form and added some nodes.

    public Form1()
    {
        InitializeComponent();

        treeView1.Nodes.Add("root node #1");
        treeView1.Nodes.Add("root node #2");
        treeView1.Nodes.Add("root node #3");
        treeView1.Nodes.Add("root node #4");
        treeView1.Nodes.Add("root node #5");
    }

I want to change standard right-click behavior. When I right-clicked on a tree Node then Treeview changed for a while selectedIndex. I don't want it. How can I fix standard behavior ?

In ideal, it would be: right click on a treenode text --> context menu appears, right click anywhere outside treenode text --> (absolutely) nothing happens.

+3  A: 
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
 if (e.Button == MouseButtons.Right)
 {
    // put your logic here like
    // ContextMenu1.Show();
 }
}
Asad Butt
No,no, no, all the answers above is not what I want to know. I know about nodemouseclick event. I asked about standard behavior of treeview. Why does treeview change selectedIndex when I right-clicked it ?I explain again, more carefully. Sorry for my bad english. I hope you will undestand what i want. When I right clicked a space near a node (a space which located right from the node) treeview changed a selectedIndex for a while. It selects a node for a while(while i keep right mouse button pressed). I don't want to select a node which is useless for me. How can I fix it ?
Alexander Stalt
A: 

Override the MouseClick event, and in the event check if the click was right click

    private void treeView1_MouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            //Do something
        }
    }
LnDCobra
No,no, no, all the answers above is not what I want to know. I know about nodemouseclick event. I asked about standard behavior of treeview. Why does treeview change selectedIndex when I right-clicked it ?I explain again, more carefully. Sorry for my bad english. I hope you will undestand what i want. When I right clicked a space near a node (a space which located right from the node) treeview changed a selectedIndex for a while. It selects a node for a while(while i keep right mouse button pressed). I don't want to select a node which is useless for me. How can I fix it ?
Alexander Stalt
A: 

You need to handle the NodeMouseClick event and check the right mouse button was clicked:

treeView1.NodeMouseClick += (o, e) => {
    if(e.Button == MouseButtons.Right)
    {
        //show menu...
    }
};
Lee
No,no, no, all the answers above is not what I want to know. I know about nodemouseclick event. I asked about standard behavior of treeview. Why does treeview change selectedIndex when I right-clicked it ?I explain again, more carefully. Sorry for my bad english. I hope you will undestand what i want. When I right clicked a space near a node (a space which located right from the node) treeview changed a selectedIndex for a while. It selects a node for a while(while i keep right mouse button pressed). I don't want to select a node which is useless for me. How can I fix it ?
Alexander Stalt
A: 

No,no, no, all the answers above is not what I want to know. I know about nodemouseclick event. I asked about standard behavior of treeview. Why does treeview change selectedIndex when I right-clicked it ?

I explain again, more carefully. Sorry for my bad english. I hope you will undestand what i want. When I right clicked a space near a node (a space which located right from the node) treeview changed a selectedIndex for a while. It selects a node for a while(while i keep right mouse button pressed). I don't want to select a node which is useless for me. How can I fix it ?

Alexander Stalt
A: 

Alex, Try this. The BeforeSelect handlers event args Cancel is tied to the fact that the right mouse is down. This suppresses the firing of the SelectedIndex changed. The MouseDown tracks if the right mouse is depressed and displays the context menu. The display is safe to move to MouseUp instead of MouseDown. The MouseUp clears the flag indicating that the RightMouse button is depressed.

All of this information on how I did this is available on MSDN. The trick is actually reading the names of all of the events -- Yes I know there are a lot -- then making a list of the "interesting ones" in your case you named SelectedIndex changing, and Mouse clicks. That immediately limits the event names you should read in detail on... If you want the text to not highlight while you're right clicking... well that's a different matter entirely and I caution you against it as it's valuable user feedback.

    bool isRBut = false;
    private void treeView1_MouseDown(object sender, MouseEventArgs e)
    {
        isRBut = e.Button == MouseButtons.Right;
        if (isRBut)
        {
            TreeViewHitTestInfo hti =treeView1.HitTest(e.Location);
            if (hti.Location == TreeViewHitTestLocations.Label)                
                contextMenuStrip1.Show(treeView1, new Point(hti.Node.Bounds.Left, hti.Node.Bounds.Bottom));                
        }
    }

    private void treeView1_MouseUp(object sender, MouseEventArgs e)
    {
        isRBut = e.Button == MouseButtons.Right;
    }

    private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
    {
        e.Cancel = isRBut;
    }

Additionally, here's a bit of human language trivia for you. Hopefully this will help you in the future. Phrases such as "No, no, no" are interpreted by native English speakers as very rude. Just do your best to list the behavior you see, and the behavior you want. Even if people misunderstand stick to the facts only and leave out the obvious signs of frustration. This will help you get what you're after. As well on SO if someone has a habit of not accepting answers many members here will have a habit of not providing future answers to such members.

Jason D