110,546
社区成员
发帖
与我相关
我的任务
分享
public Form1()
{
InitializeComponent();
TreeNode root = new TreeNode("Root");
root.ToolTipText = "What is it?";
root.ContextMenuStrip = this.contextMenuStrip2;
this.treeView1.Nodes.Add(root);
TreeNode node = new TreeNode("Hello");
node.ToolTipText = "Hello - can you see me?";
root.Nodes.Add(node);
node.ContextMenuStrip = this.contextMenuStrip1;
node = new TreeNode("World");
node.ToolTipText = "World";
root.Nodes.Add(node);
node.ContextMenuStrip = this.contextMenuStrip1;
this.treeView1.ExpandAll();
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.treeView1 = new System.Windows.Forms.TreeView();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.contextMenuStrip2 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.contextMenuStrip1.SuspendLayout();
this.contextMenuStrip2.SuspendLayout();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.treeView1.Location = new System.Drawing.Point(12, 12);
this.treeView1.Name = "treeView1";
this.treeView1.ShowNodeToolTips = true;
this.treeView1.Size = new System.Drawing.Size(157, 202);
this.treeView1.TabIndex = 0;
this.treeView1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseUp);
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripMenuItem1});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(129, 26);
//
// contextMenuStrip2
//
this.contextMenuStrip2.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripMenuItem2});
this.contextMenuStrip2.Name = "contextMenuStrip2";
this.contextMenuStrip2.Size = new System.Drawing.Size(125, 26);
//
// toolStripMenuItem1
//
this.toolStripMenuItem1.Name = "toolStripMenuItem1";
this.toolStripMenuItem1.Size = new System.Drawing.Size(128, 22);
this.toolStripMenuItem1.Text = "Hello World";
//
// toolStripMenuItem2
//
this.toolStripMenuItem2.Name = "toolStripMenuItem2";
this.toolStripMenuItem2.Size = new System.Drawing.Size(124, 22);
this.toolStripMenuItem2.Text = "What is it?";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Location = new System.Drawing.Point(175, 12);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(245, 333);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(63, 290);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(122, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Drag and drop me";
this.button1.UseVisualStyleBackColor = true;
this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
this.button1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.button1_MouseMove);
this.button1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp);
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Checked = true;
this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBox1.Location = new System.Drawing.Point(12, 328);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(137, 17);
this.checkBox1.TabIndex = 4;
this.checkBox1.Text = "Enable SHOW function";
this.checkBox1.UseVisualStyleBackColor = true;
//
// label1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label1.Location = new System.Drawing.Point(12, 217);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(157, 108);
this.label1.TabIndex = 5;
this.label1.Text = "重现方法:\r\n1. 选中下面的选项。\r\n2. 左击一个结点,右击显示菜单。\r\n3. 再选中同一个结点,菜单隐掉。\r\n4. 看看问题有没有重现,否则重现2, 3。";
//
// label2
//
this.label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label2.Location = new System.Drawing.Point(6, 16);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(233, 135);
this.label2.TabIndex = 1;
this.label2.Text = "问题描述:\r\n想要在TreeView控件上右击菜单时针对不同的结点显示不同的右击菜单,所以只能在右击事件中动态赋值给控件的ContextMenuStrip属性,并" +
"调用Show方法才能正常显示(否则要下一次右击时才显示刚赋给的新菜单)。 \r\n如此以来,整个程序的鼠标事件就不正常了:原来的Tooltip都不能正常显示;拖揣失" +
"效,最小化程序后恢复才能正常。";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(432, 357);
this.Controls.Add(this.treeView1);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.checkBox1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.contextMenuStrip1.ResumeLayout(false);
this.contextMenuStrip2.ResumeLayout(false);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip2;
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem2;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
namespace PopupMenu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TreeNode root = new TreeNode("Root");
root.ToolTipText = "What is it?";
this.treeView1.Nodes.Add(root);
TreeNode node = new TreeNode("Hello");
node.ToolTipText = "Hello - can you see me?";
root.Nodes.Add(node);
node = new TreeNode("World");
node.ToolTipText = "World";
root.Nodes.Add(node);
this.treeView1.ExpandAll();
}
private void treeView1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
TreeViewHitTestInfo info = treeView1.HitTest(e.X, e.Y);
TreeNode node = info.Node;
if (node == null)
{
if (treeView1.ContextMenuStrip != null)
{
treeView1.ContextMenuStrip.Hide();
}
treeView1.ContextMenuStrip = null;
return;
}
treeView1.SelectedNode = node;
if (node.Text == "Root")
{
treeView1.ContextMenuStrip = this.contextMenuStrip2;
}
else
{
treeView1.ContextMenuStrip = this.contextMenuStrip1;
}
if (this.checkBox1.Checked)
{
treeView1.ContextMenuStrip.Show(treeView1, e.Location);
}
}
}
private bool _drag = false;
private Point _original;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
_drag = true;
_original = e.Location;
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (_drag)
{
button1.Left += e.X - _original.X;
button1.Top += e.Y - _original.Y;
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
_drag = false;
}
}
}