Change size of ToolStripMenuItem

snarf picture snarf · Nov 15, 2010 · Viewed 11.8k times · Source

I'm custom drawing a menu item in a MenuStrip. The problem I'm having is that the menu item insists on sizing itself based on the text, which is not what I want (there is no text). I can set AutoSize to false and explicitly specify a size, but the containing menu (ToolStripDropDown) still sizes itself based on the text, which causes it to be too small to contain the entire menu item.

Is there a straightforward way to set the size of a menu item?

Answer

zafar shakeel picture zafar shakeel · Oct 23, 2012

It can be achieved by adding an empty image to the toolStripItem. Now change the size of image to give the desired size to your ToolStripItem. I have done this in my project as below:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

public partial class Form1 : Form
{    
    public Form1()
    {
        Bitmap emptyImage = new Bitmap(48, 48);
        ToolStripMenuItem  m_Item = new ToolStripMenuItem(System.Convert.ToString("All       Programs",emptyImage);
        m_Item.ImageAlign = ContentAlignment.MiddleLeft;
        m_Item.ImageScaling = ToolStripItemImageScaling.None;                        
        m_Item.Name = btnId.ToString();
        MenuStrip menu = new MenuStrip();
        menu.Items.Add(m_Item);
    }
}