Click here to Skip to main content
15,916,683 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to convert VC++ to C# code? Pin
Heath Stewart18-Oct-04 7:14
protectorHeath Stewart18-Oct-04 7:14 
GeneralRe: How to convert VC++ to C# code? Pin
momer18-Oct-04 16:51
momer18-Oct-04 16:51 
GeneralText Wrap for ListView in C# Pin
Member 141415918-Oct-04 1:09
Member 141415918-Oct-04 1:09 
GeneralRe: Text Wrap for ListView in C# Pin
Heath Stewart18-Oct-04 7:11
protectorHeath Stewart18-Oct-04 7:11 
GeneralRe: Text Wrap for ListView in C# Pin
Sundar K18-Oct-04 18:52
sussSundar K18-Oct-04 18:52 
GeneralRe: Text Wrap for ListView in C# Pin
Heath Stewart19-Oct-04 5:32
protectorHeath Stewart19-Oct-04 5:32 
GeneralRe: Text Wrap for ListView in C# Pin
Member 141415919-Oct-04 13:35
Member 141415919-Oct-04 13:35 
GeneralWebSerivce output xml Pin
jzb18-Oct-04 0:42
jzb18-Oct-04 0:42 
GeneralRe: WebSerivce output xml Pin
Heath Stewart18-Oct-04 7:04
protectorHeath Stewart18-Oct-04 7:04 
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 

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.