Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. ContextMenuStrip dynamic item widths

ContextMenuStrip dynamic item widths

Scheduled Pinned Locked Moved C#
question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DarrenShultz
    wrote on last edited by
    #1

    I am attempting to populate a ContextMenuStrip (or any ToolStripDropDown for that matter) with ToolStripMenuItems that have AutoSize set to False and a specific Height and Width. However, I cannot get the ContextMenuStrip to AutoSize (which is set to True on the ContextMenuStrip). The Height appears to be resized, but the Width cuts off the menu items. I have tried using varying values for Margin, Padding, AutoSize, Width, Height... etc... but to no avail. Any ideas?

    private void AddMenuItem (ContextMenuStrip oContextMenuStrip, string sText)
    {
    oContextMenuStrip.AutoSize = true;

    ToolStripMenuItem oMenuItem = new ToolStripMenuItem(sText);
    oMenuItem.AutoSize = false;
    oMenuItem.Width = 300;
    oMenuItem.Height = 40;

    oContextMenuStrip.Items.Add(oMenuItem);
    }

    A 1 Reply Last reply
    0
    • D DarrenShultz

      I am attempting to populate a ContextMenuStrip (or any ToolStripDropDown for that matter) with ToolStripMenuItems that have AutoSize set to False and a specific Height and Width. However, I cannot get the ContextMenuStrip to AutoSize (which is set to True on the ContextMenuStrip). The Height appears to be resized, but the Width cuts off the menu items. I have tried using varying values for Margin, Padding, AutoSize, Width, Height... etc... but to no avail. Any ideas?

      private void AddMenuItem (ContextMenuStrip oContextMenuStrip, string sText)
      {
      oContextMenuStrip.AutoSize = true;

      ToolStripMenuItem oMenuItem = new ToolStripMenuItem(sText);
      oMenuItem.AutoSize = false;
      oMenuItem.Width = 300;
      oMenuItem.Height = 40;

      oContextMenuStrip.Items.Add(oMenuItem);
      }

      A Offline
      A Offline
      April Fans
      wrote on last edited by
      #2

      You can adjust width by yourself, use Graphics.MeasureString to get the width of the menu item and set width manually.

      April Comm100 - Leading Live Chat Software Provider

      D 1 Reply Last reply
      0
      • A April Fans

        You can adjust width by yourself, use Graphics.MeasureString to get the width of the menu item and set width manually.

        April Comm100 - Leading Live Chat Software Provider

        D Offline
        D Offline
        DarrenShultz
        wrote on last edited by
        #3

        Yes, I had tried that. However, it did not accomplish what I wanted to achieve. The menu item will be completely custom drawn, but there is some text that I'll need to assure will fit. So, I calculated the width of a space in the menu item font, measured that text that will be rendered to it (though text measuring in .NET is sometimes inaccurate), and set the menu item text to all spaces (see code below). However, every menu item in that drop down will have the same height now if there is more than one line. And, we have no way of making the menu item an exact dimension since we can only affect width/height using spaces and newlines in the menu item text. Ideally, I'd like to know if there is a clean way to resolve such drop-down menu and menu item dimension inconsistencies. Perhaps a "DoWhatIWant" flag? :)

        private void AutoSizeCustomMenuItem(ToolStripMenuItem oMenuItem, string sText)
        {
        int iPreferredHeight = 50;
        int iPreferredWidth = 0;
        int iPadding = 50;
        SizeF oSizeSpace = SizeF.Empty;

        using (Graphics oGraphics = oMenuItem.Owner.CreateGraphics())
        {
        iPreferredWidth = (int)oGraphics.MeasureString(sText, oMenuItem.Font).Width + iPadding;
        oSizeSpace = oGraphics.MeasureString(" ", oMenuItem.Font);
        }

        int iSpacesWidth = (int)Math.Ceiling((double)(iPreferredWidth / oSizeSpace.Width));
        int iSpacesHeight = (int)Math.Ceiling((double)(iPreferredHeight / oSizeSpace.Height));
        string sSpacesWidth = new String(' ', iSpacesWidth);

        StringBuilder sbMenuItemText = new StringBuilder();
        if (iSpacesHeight > 0)
        {
        string sLineDivider = String.Empty;
        for (int i = 0; i < iSpacesHeight; i++)
        {
        sbMenuItemText.Append(sLineDivider);
        sbMenuItemText.Append(sSpacesWidth);
        sLineDivider = "\r\n";
        }
        }
        oMenuItem.Text = sbMenuItemText.ToString();
        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups