TreeView/ListView item selection and context menu
-
Hi all. I have a feeling there is a very simple solution to my problem, but I just can't figure it out. Here goes: I have a form with a treeview (and a listview with the same problem), where I present different items. I have a context menu connected to the view giving the user the option to, for instance, edit or delete the selected item. The problem is that if the user has selected an item and then right clicks on another item and choose to edit, it's the first item that 'opens' to be edited. I would want the right click to FIRST select the item that was being clicked and THEN popping up the context menu so that the user edits or deletes the item that was being right clicked. Any tips? Thanks /EnkelIk
-
Hi all. I have a feeling there is a very simple solution to my problem, but I just can't figure it out. Here goes: I have a form with a treeview (and a listview with the same problem), where I present different items. I have a context menu connected to the view giving the user the option to, for instance, edit or delete the selected item. The problem is that if the user has selected an item and then right clicks on another item and choose to edit, it's the first item that 'opens' to be edited. I would want the right click to FIRST select the item that was being clicked and THEN popping up the context menu so that the user edits or deletes the item that was being right clicked. Any tips? Thanks /EnkelIk
-
Hi all. I have a feeling there is a very simple solution to my problem, but I just can't figure it out. Here goes: I have a form with a treeview (and a listview with the same problem), where I present different items. I have a context menu connected to the view giving the user the option to, for instance, edit or delete the selected item. The problem is that if the user has selected an item and then right clicks on another item and choose to edit, it's the first item that 'opens' to be edited. I would want the right click to FIRST select the item that was being clicked and THEN popping up the context menu so that the user edits or deletes the item that was being right clicked. Any tips? Thanks /EnkelIk
It will require you to write code to select the node when the user right-clicks. It is fairly easy to write and I believe CodeProject has some examples of this. Search the .NET Controls section. The default controls don't support the feature you want. - Drew
-
Hi all. I have a feeling there is a very simple solution to my problem, but I just can't figure it out. Here goes: I have a form with a treeview (and a listview with the same problem), where I present different items. I have a context menu connected to the view giving the user the option to, for instance, edit or delete the selected item. The problem is that if the user has selected an item and then right clicks on another item and choose to edit, it's the first item that 'opens' to be edited. I would want the right click to FIRST select the item that was being clicked and THEN popping up the context menu so that the user edits or deletes the item that was being right clicked. Any tips? Thanks /EnkelIk
This is a problem with your code that can easily be solved. Right-clicking a node should NOT select it. This is counter-intuitive as it does not follow the Windows UI Guidelines that exist to present a common user interface experience. If you change the behavior your liable to confuse users who are used to the way "everything else works". Make sure that either tie a
ContextMenu
to a node by referencing a particularContextMenu
(presumably, certain types of nodes all reference the same instance of aContextMenu
to save resources) by assigning an instance to theTreeNode.Tag
property. Conversely, you could implement the popup of specificContextMenu
s based on data in theTreeNode
(like that from theText
orTag
properties; or perhaps you extendTreeNode
with your own derivative). LeaveTreeView.ContextMenu
unassigned. Instead, handle theMouseUp
event (or, if you're extendingTreeView
, override theOnMouseUp
protected method for better control and faster execution; just be sure to callbase.OnMouseUp
). This gives you translated mouse coordinates (to the client, or control) that you can use in a call toTreeView.GetNodeAt
:protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
Point pt = new Point(e.X, e.Y);
TreeNode node = GetNodeAt(pt);
if (node != null)
{
ContextMenu cm = node.Tag as ContextMenu;
if (cm != null)
cm.Show(this, pt);
}
}There's an example of getting the reference to the
ContextMenu
you want assigned to theTreeNode.Tag
property. Again - reuse references to distinctContextMenu
s so you don't hot system resources and otherwise frustrate the user when you start getting lots of tree nodes or have large context menus. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]