Windows Develop Bookmark and Share   
 index > Windows Forms General > Show and positionate context menu on Node in Treeview
 

Show and positionate context menu on Node in Treeview

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)

{

if (e.Node.Level == 0)

{ if (e.Button == MouseButtons.Right)

{

MenuItem[] menuItems = new MenuItem[]{new MenuItem("Smazat"), };

ContextMenu Menu = new ContextMenu(menuItems);

Menu.Show(treeView1, new System.Drawing.Point(10, 10));

}

This code I have show it of course in 10,10 - could anyone help me to positionate it to X,Y of mouse or to selected node? Thank you!

EDIT: Showing soluted, thank you folks.

Blizna  Wednesday, April 09, 2008 3:59 PM
Hi

Just add a ContextMenuStrip control to your form. Then select the control and add all the menu items needed with the Items property. Then set the 'ContextMenuStrip' property in the control you want to watch the menu, with the new ContextMenuStrip you added.

If you want to create it dinamically, you can try this:

public YourClass() // constructor of your class
{
ContextMenuStrip MyCMS = New ContextMenuStrip();

MyCMS.Items.Add( - what you want -)

ObjectYouWantToSetTheCMS.ContextMenuStrip = MyCMS
}

Regards.
Borja Serrano  Wednesday, April 09, 2008 4:13 PM
Hi

Just add a ContextMenuStrip control to your form. Then select the control and add all the menu items needed with the Items property. Then set the 'ContextMenuStrip' property in the control you want to watch the menu, with the new ContextMenuStrip you added.

If you want to create it dinamically, you can try this:

public YourClass() // constructor of your class
{
ContextMenuStrip MyCMS = New ContextMenuStrip();

MyCMS.Items.Add( - what you want -)

ObjectYouWantToSetTheCMS.ContextMenuStrip = MyCMS
}

Regards.
Borja Serrano  Wednesday, April 09, 2008 4:13 PM

I tried but I dont know how to show the menu :/
Blizna  Wednesday, April 09, 2008 4:19 PM
Where you're trying to add the context menu? (form, panel, textbox..)
Borja Serrano  Wednesday, April 09, 2008 4:22 PM

I want to let user click with right mouse button on TreeView and show popup menu with choice "Delete node".. But I would be grateful for code that allow me to show the menu. Thanks!

EDIT: Found problem (case sensitive button except Button Smile

Blizna  Wednesday, April 09, 2008 4:28 PM
No code is needed to do this task. Just try to drop a ContextMenuStrip control from the toolbox to your form. Then go to properties window, to Item property, and there add all the menu items that you want. Then select the treeview control and go to properties window, see the ContextMenuStrip property, and select there the ContextMenuStrip control you added before.

This is with the designer. Also you can try the code I posted before to do it dinamically
Borja Serrano  Wednesday, April 09, 2008 4:40 PM

Thank you Borja! If you know how to positionate it to selected node (main post is edited), your help is really appreciated, thank you!
Blizna  Wednesday, April 09, 2008 4:55 PM
Hi

Try to put a MyCMS.Show() into a MouseDown event, when event arg is right button. Example:

Private Sub TreeView_MouseDown(sender As Object, e as System.Windows:Forms.MouseDownEventArgs) Handles MyNode.MouseDown
If e.Button = Button.Right Then
MyCMS.Location = (set the proper location for the context menu, cause default is 0,0)
MyCMS.Show()
End If
End Sub
Borja Serrano  Thursday, April 10, 2008 7:04 AM

You can use google to search for other answers

Custom Search

More Threads

• Passing events on to covered controls
• MaskedTextBox question
• Encrypt Configuration Block Settings?
• RichTextBox scrolling
• Setting the default values for custom controls
• I cannot find control OleDbDataAdapter in the toolbox
• Minimize form on start up?
• .NET RichTextBox Bugs
• FileSystemWatcher, get username who made an action?
• Using Tab key between Textboxes