我有一个带有ToolStripDropDownButton的ToolStrip,我在我的代码中在运行时添加了ToolStripMenuItems。我需要一个默认的ContextMenuStrip并将其分配给每个菜单项,这样当用户右键单击一个菜单项时,他将获得该上下文菜单条。 有可能吗?
感谢您的帮助。
答案 0 :(得分:0)
似乎我找到了一些有用的东西,这显然不是很完美,但应该给你一个不错的起点。我假设你有一个名为 contextmenustrip1 的ContextMenuStrip,其中一些下拉项目一个是 aaaToolStripMenuItem 。然后创建一个私有布尔字段,
private static bool clickReleased=false;
在 aaaTool ... 的MouseDown事件中,编写以下代码;
if (e.Button == MouseButtons.Right)
clickReleased = true;
然后在MouseUp事件中,写下这个;
if (e.Button == MouseButtons.Right)
{
if (clickReleased)
{
contextMenuStrip1.Show(Cursor.Position);
clickReleased = false;
}
}
希望这有帮助。
答案 1 :(得分:0)
为了节省阅读费用,我还在这里添加了solutuin:
void MenuItemContext(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) return;
ToolStripMenuItem mID = (ToolStripMenuItem)sender;
ContextMenu tsmiContext = new ContextMenu();
MenuItem Item1 = new MenuItem();
MenuItem Item2 = new MenuItem();
Item1.Text = "Item1";
Item2.Text = "Item2";
tsmiContext.MenuItems.Add(Item1);
tsmiContext.MenuItems.Add(Item2);
Item1.Click += new EventHandler(Item1_Click);
Item2.Click += new EventHandler(Item2_Click);
hndPass = mID.Text;
tsmiContext.Show(menuStrip1, menuStrip1.PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y)));
}
private String hndPass;
void Item1_Click(object sender, EventArgs e)
{
MenuItem mID = (MenuItem)sender;
MessageBox.Show("You clicked " + mID.Text + " in the context menu of " + hndPass);
}
void Item2_Click(object sender, EventArgs e)
{
MenuItem mID = (MenuItem)sender;
MessageBox.Show("You clicked " + mID.Text + " in the context menu of " + hndPass); ;
}
玩得开心( - :