Click to See Complete Forum and Search --> : Right Click in TreeView not responding


bixel
January 3rd, 2010, 06:06 PM
C# WinForms.. trying to get a contextMenu to show in TreeView when I right click - but nothing is happening


private void mainView_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (mainView.SelectedNode == null)
{
Point ClickPoint = new Point(e.X, e.Y);
Point ScreenPoint = mainView.PointToScreen(ClickPoint);
Point FormPoint = this.PointToClient(ScreenPoint);
cntxtMenuTreeNPCCreate.Show(this, FormPoint);
}
}
}

bixel
January 3rd, 2010, 06:14 PM
Solved!

I initially created the Event in the TreeView's MouseClick Event, however I doubleclicked the TreevView's MouseDown Event to create the method and copied the same code over. Apparently it works like a charm in MouseDown - but will not register in MouseClick


private void mainView_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (mainView.SelectedNode == null)
{
Point ClickPoint = new Point(e.X, e.Y);
Point ScreenPoint = mainView.PointToScreen(ClickPoint);
Point FormPoint = this.PointToClient(ScreenPoint);
cntxtMenuTreeNPCCreate.Show(this, FormPoint);
}
}
}

Arjay
January 3rd, 2010, 07:08 PM
The problem with using the mouse down or the mouse click events is that it won't display the context menu when the user uses the keyboard SHIFT+F10 command.

So why not hook up the context menu using the TreeView.ContextMenuStrip property?

If you need a dynamic context menu (where the menu items change depending on the selected tree item), then override the AfterSelect event handler and change the context menu strip items based on the selected node.