Resize ToolStripMenuItem

I am customizing a menu item in MenuStrip . The problem I am facing is that the menu item insists that the size be based on the text, which I don't want (no text). I can set AutoSize to false and explicitly specify the size, but the containing menu ( ToolStripDropDown ) is still size-based on the text, which makes it too small to contain the entire menu item.

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

+4
source share
3 answers

This comment in an article on CodeProject explains the nature of my problem:

While you can handle the Paint event for the ToolStripMenuItem tool, ToolStripMenuItem is not intended for the "owner". If you want to process a drawing of a specific tool element, the recommended tool is to create your own ToolStripItem type. See ToolStripItem Class [^] for an example.

0
source

This can be achieved by adding a blank image to toolStripItem . Now resize the image to indicate the desired size of your toolStripItem . I did this in my project as shown 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); } } 
+6
source
  • You can set the width (not height) of your menu item using spaces, and then draw an empty space using OnPaint
  • If you need to put a snapshot, why use ToolStripMenuItem? For example, you can host a ToolStripControlHost with an embedded Image. And don't forget that you can change the layout down using LayoutStyle and LayoutSettings Properties (for example, from table to table)
  • You can not only specify the size for your menu item, but you can also specify the size for the drop-down list where it is located. For example, when an expansion is revealed (OnOpening, Opening, DropDownOpending many ways to respond), you can set the minimum width (or height, or both) using the ToolStripDropDown.MinimumSize property.

In general, ToolStrip is the most well-designed control in the WinForms namespace. It has almost unlimited possibilities and is very extensible.

Update:. According to your comment. I can not say much, because while ToolStrip is the most architectural, it is also very well commented, and you need to find a lot of specific things. I have so far added No. 3 to my answer, but many things can only be detected by trying, and with Reflector, of course.

+1
source

All Articles