Click here to Skip to main content
15,895,142 members
Home / Discussions / C#
   

C#

 
GeneralTreeView DragDrop image Pin
dorothyDorothy17-Oct-04 23:39
dorothyDorothy17-Oct-04 23:39 
GeneralRe: TreeView DragDrop image Pin
Heath Stewart18-Oct-04 6:52
protectorHeath Stewart18-Oct-04 6:52 
GeneraldateTime parser Pin
Rob Tomson17-Oct-04 23:27
Rob Tomson17-Oct-04 23:27 
GeneralRe: dateTime parser Pin
J4amieC18-Oct-04 0:28
J4amieC18-Oct-04 0:28 
GeneralTreeView/ListView item selection and context menu Pin
EnkelIk17-Oct-04 21:44
EnkelIk17-Oct-04 21:44 
GeneralRe: TreeView/ListView item selection and context menu Pin
petst17-Oct-04 22:06
petst17-Oct-04 22:06 
GeneralRe: TreeView/ListView item selection and context menu Pin
afinnell18-Oct-04 5:35
afinnell18-Oct-04 5:35 
GeneralRe: TreeView/ListView item selection and context menu Pin
Heath Stewart18-Oct-04 6:41
protectorHeath Stewart18-Oct-04 6:41 
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 particular ContextMenu (presumably, certain types of nodes all reference the same instance of a ContextMenu to save resources) by assigning an instance to the TreeNode.Tag property. Conversely, you could implement the popup of specific ContextMenus based on data in the TreeNode (like that from the Text or Tag properties; or perhaps you extend TreeNode with your own derivative).

Leave TreeView.ContextMenu unassigned. Instead, handle the MouseUp event (or, if you're extending TreeView, override the OnMouseUp protected method for better control and faster execution; just be sure to call base.OnMouseUp).

This gives you translated mouse coordinates (to the client, or control) that you can use in a call to TreeView.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 the TreeNode.Tag property. Again - reuse references to distinct ContextMenus 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]
Generalcapturing any window in ur application Pin
Member 143169617-Oct-04 21:18
Member 143169617-Oct-04 21:18 
GeneralRe: capturing any window in ur application Pin
Heath Stewart18-Oct-04 6:32
protectorHeath Stewart18-Oct-04 6:32 
GeneralSending a string to Crystal Report Pin
myNameIsRon17-Oct-04 21:06
myNameIsRon17-Oct-04 21:06 
GeneralRe: trying to write to a log file in destructor Pin
Colin Angus Mackay17-Oct-04 20:56
Colin Angus Mackay17-Oct-04 20:56 
GeneralRe: trying to write to a log file in destructor Pin
Gavin Jeffrey17-Oct-04 21:41
Gavin Jeffrey17-Oct-04 21:41 
GeneralRe: trying to write to a log file in destructor Pin
Colin Angus Mackay17-Oct-04 23:01
Colin Angus Mackay17-Oct-04 23:01 
GeneralGetting all the section names from an ini file Pin
Member 64506717-Oct-04 20:32
Member 64506717-Oct-04 20:32 
GeneralRe: Getting all the section names from an ini file Pin
Colin Angus Mackay17-Oct-04 20:50
Colin Angus Mackay17-Oct-04 20:50 
GeneralRe: Getting all the section names from an ini file Pin
Heath Stewart18-Oct-04 6:28
protectorHeath Stewart18-Oct-04 6:28 
QuestionHow my remote sever popup information Pin
ChandruIT17-Oct-04 20:02
ChandruIT17-Oct-04 20:02 
AnswerRe: How my remote sever popup information Pin
afinnell18-Oct-04 5:57
afinnell18-Oct-04 5:57 
Questionhow to rread a line from a txt file to an array Pin
tom_dx17-Oct-04 10:20
tom_dx17-Oct-04 10:20 
AnswerRe: how to rread a line from a txt file to an array Pin
Kiran Satish17-Oct-04 10:32
Kiran Satish17-Oct-04 10:32 
GeneralRe: how to rread a line from a txt file to an array Pin
Christian Graus17-Oct-04 10:53
protectorChristian Graus17-Oct-04 10:53 
GeneralRe: how to rread a line from a txt file to an array Pin
Kiran Satish17-Oct-04 10:56
Kiran Satish17-Oct-04 10:56 
AnswerRe: how to rread a line from a txt file to an array Pin
Christian Graus17-Oct-04 10:54
protectorChristian Graus17-Oct-04 10:54 
GeneralRe: how to rread a line from a txt file to an array Pin
tom_dx17-Oct-04 12:52
tom_dx17-Oct-04 12:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.