C# ContextMenuStrip 子菜单属性

发布于 2024-11-28 14:44:19 字数 1164 浏览 5 评论 0原文

谁能告诉我如何获取 ContextMenuStrip 中子菜单的属性?

我知道我可以创建一个表单并将上下文菜单条拖放到其上。如果我然后将一些项目添加到条中:

列出项目

  • - 红色
  • - 蓝色
  • 标记
  • - 绿色
  • - 橙色

然后我编写以下代码:

public partial class Form3 : Form
{
    public Form3()
    {
        InitializeComponent();
        this.contextMenuStrip1.AutoSize = false;
        this.contextMenuStrip1.Height = 300;
        this.contextMenuStrip1.Width = 150;
    }

    /// <summary>
    /// Handles the MouseClick event of the Form1 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
    private void Form3_MouseClick_1(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            Point pt = this.PointToScreen(e.Location);
            this.contextMenuStrip1.Show(pt);
        }
    }
}

显示笔和标记的顶级菜单将位于非自动调整大小的条上 150 * 300 但如果我将鼠标悬停在“笔”上以获取子菜单“红色”和“蓝色”,该子菜单将显示在自动调整大小的条上!

如何获取子菜单属性以便设置其高度?

Can anyone tell me how to get at the properties for the submenu in a ContextMenuStrip?

I know I can create a form and drop a context menu strip onto it. If I then add some items to the strip:

List item

  • Pens
  • -- Red
  • -- Blue
  • Markers
  • -- Green
  • -- Orange

I then write the following code:

public partial class Form3 : Form
{
    public Form3()
    {
        InitializeComponent();
        this.contextMenuStrip1.AutoSize = false;
        this.contextMenuStrip1.Height = 300;
        this.contextMenuStrip1.Width = 150;
    }

    /// <summary>
    /// Handles the MouseClick event of the Form1 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
    private void Form3_MouseClick_1(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            Point pt = this.PointToScreen(e.Location);
            this.contextMenuStrip1.Show(pt);
        }
    }
}

The Top level menu showing Pens and Markers will be on a non autosized strip 150 * 300 but if i hover over Pens to get the sub menu, Red and Blue, this submenu will show up on a autosized strip!

How do I get at the sub menu properties so I can set it's height?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪歌 2024-12-05 14:44:19

对于您的问题

如何获取子菜单属性,以便我可以告诉它我的高度
想要它!

使用 Items

ContextMenuStrip1.Items[0].Height=200;

对于 items[0] 的任何子项:

foreach(ToolStripItem item in (ContextMenuStrip1.Items[0] as ToolStripDropDownItem).DropDownItems)
{
    item.AutoSize=false;
    item.Height=200;
}

For your question about

How do I get at the sub menu properties so I can tell it what height I
want it!

Use Items:

ContextMenuStrip1.Items[0].Height=200;

and for any sub-itmes of items[0]:

foreach(ToolStripItem item in (ContextMenuStrip1.Items[0] as ToolStripDropDownItem).DropDownItems)
{
    item.AutoSize=false;
    item.Height=200;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文