curiouswebster
asked on
C# WinForms: Using both a Context Menu and the AfterSelect event
I want to open a dislog when the user clicks a certain node type in a TreeView. And I want a right click to open a context menu. I have each working, but they collide with each other.
How can I solve this problem?
I do not want a left click to open the Context Menu. This is what create the collission.
Thanks!
How can I solve this problem?
I do not want a left click to open the Context Menu. This is what create the collission.
Thanks!
So you want two different behaviors for a RIGHT click on a TreeView node based on the node type?
ASKER
No, I want to open the dialog AfterSelect on a left click, based on node type (not all node types have a corresponding dialog). I want to display the Context Menu on a right click.
Hmmm...ok.
"I do not want a left click to open the Context Menu. This is what create the collission."
I don't see how these collide. Left click does not normally open a context menu. How are you assigning/displaying the context menu? What is in AfterSelect()?
"I do not want a left click to open the Context Menu. This is what create the collission."
I don't see how these collide. Left click does not normally open a context menu. How are you assigning/displaying the context menu? What is in AfterSelect()?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
I tried following code and it is not showing any collision
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
private ContextMenuStrip contextMenuStrip1;
private IContainer components;
private ToolStripMenuItem toolStripMenuItem2;
private ToolStripMenuItem toolStripMenuItem3;
private ToolStripMenuItem toolStripMenuItem4;
private TreeView treeView1;
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Windows.Forms.TreeNode treeNode1 = new System.Windows.Forms.TreeNode("Node1");
System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Node3");
System.Windows.Forms.TreeNode treeNode3 = new System.Windows.Forms.TreeNode("Node2", new System.Windows.Forms.TreeNode[] {
treeNode2});
System.Windows.Forms.TreeNode treeNode4 = new System.Windows.Forms.TreeNode("Node0", new System.Windows.Forms.TreeNode[] {
treeNode1,
treeNode3});
System.Windows.Forms.TreeNode treeNode5 = new System.Windows.Forms.TreeNode("Node6");
System.Windows.Forms.TreeNode treeNode6 = new System.Windows.Forms.TreeNode("Node5", new System.Windows.Forms.TreeNode[] {
treeNode5});
System.Windows.Forms.TreeNode treeNode7 = new System.Windows.Forms.TreeNode("Node4", new System.Windows.Forms.TreeNode[] {
treeNode6});
this.treeView1 = new System.Windows.Forms.TreeView();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItem3 = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItem4 = new System.Windows.Forms.ToolStripMenuItem();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeView1.Location = new System.Drawing.Point(0, 0);
this.treeView1.Name = "treeView1";
treeNode1.Name = "Node1";
treeNode1.Text = "Node1";
treeNode2.Name = "Node3";
treeNode2.Text = "Node3";
treeNode3.Name = "Node2";
treeNode3.Text = "Node2";
treeNode4.Name = "Node0";
treeNode4.Text = "Node0";
treeNode5.Name = "Node6";
treeNode5.Text = "Node6";
treeNode6.Name = "Node5";
treeNode6.Text = "Node5";
treeNode7.Name = "Node4";
treeNode7.Text = "Node4";
this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
treeNode4,
treeNode7});
this.treeView1.Size = new System.Drawing.Size(284, 262);
this.treeView1.TabIndex = 0;
this.treeView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseDown);
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripMenuItem2,
this.toolStripMenuItem3,
this.toolStripMenuItem4});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(81, 70);
//
// toolStripMenuItem2
//
this.toolStripMenuItem2.Name = "toolStripMenuItem2";
this.toolStripMenuItem2.Size = new System.Drawing.Size(80, 22);
this.toolStripMenuItem2.Text = "1";
//
// toolStripMenuItem3
//
this.toolStripMenuItem3.Name = "toolStripMenuItem3";
this.toolStripMenuItem3.Size = new System.Drawing.Size(80, 22);
this.toolStripMenuItem3.Text = "2";
//
// toolStripMenuItem4
//
this.toolStripMenuItem4.Name = "toolStripMenuItem4";
this.toolStripMenuItem4.Size = new System.Drawing.Size(80, 22);
this.toolStripMenuItem4.Text = "3";
//
// Form1
//
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false);
}
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
if (int.Parse(treeView1.SelectedNode.Text.Substring(treeView1.SelectedNode.Text.Length - 1)) % 2 == 0)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
contextMenuStrip1.Show(treeView1, e.Location);
}
else
{
MessageBox.Show(this,"Test", "Test");
}
}
}
}
}