简体   繁体   中英

tree view selected item

i have tree node in my app that was created by dynamically.when right click what ever node i want to get node text that was clicked.i use that value for my futher processing .i have tried using selected node property and it not work some times.

thanks in advance

Code:

public void CmsAppList_RightClicked(object sender, MouseEventArgs e) 
{
     AddUser _addUser = new AddUser(this.Text); // i want to get that value to this constructor
     _addUser.ShowDialog();
}

This is a known issue with most of the list controls, to work around this add an event handler to treeview's MouseDown event and set the selected node on right click, as follows. My treeview name is treeView1 just change accordingly.

    private void treeView1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
            treeView1.SelectedNode = treeView1.GetNodeAt(e.Location);
    }

Now selectednode should always reflect the node you clicked last.

Edit (To correct This is known issue and saying This is by design and right )

I've been thinking about updating this answer, becuase I realized what I said about this being known issue is wrong on multiple list controls.

Let's say it is known issue and we fix it then right click and left click have similar behavior except that right click does everything(that left click does) and then opens up context menu. This sounds fine till we carefully look at context menu(right click) nature, lets assume you select 15 files and you click (left) on 16th file the selection is lost :) so you select 15 files and the right click the context menu and operations are applicable to all the 15 files selected.

This is the reason right click shouldn't change the selection in any view (in listbox or treeview or explorer widnow). If it does then multiselect and control+select features would break, and ultimately special meaning for right click being context specific actions would be lost. That is the reason the right click wouldn't select the clicked node and that is right. The code provided above is for for specific purpose of enabling right click selection. And consider the UX impact with this kind of work around or aberrant behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM