Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change size of ToolStripMenuItem

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?

like image 543
snarf Avatar asked Nov 15 '10 17:11

snarf


3 Answers

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);
    }
}
like image 125
zafar shakeel Avatar answered Sep 29 '22 18:09

zafar shakeel


This comments in an article on CodeProject explains the nature of my issue:

While you can handle the Paint event for a ToolStripMenuItem, ToolStripMenuItem isn't intended to be "owner drawn". If you want to handle the drawing of a particular tool strip item, the recommended means is to create your own ToolStripItem-derived type. See ToolStripItem Class[^] for an example.

like image 32
snarf Avatar answered Sep 29 '22 18:09

snarf


  1. You can set width (not height) of your menu item by using spaces, and then draw on empty space with OnPaint
  2. If you need to place picture into drop down why use ToolStripMenuItem? For example you can place ToolStripControlHost with Image embedded. And do not forget that you can change drop down layout by using LayoutStyle and LayoutSettings properties (for example from stack to table layout)
  3. You can not only specify size for your menu item, but you can also specify size for drop down where it located. For example when drop down is opening (OnOpening, Opening, DropDownOpending many ways to react) you can set minimum width (or height, or both) by using ToolStripDropDown.MinimumSize property.

In general ToolStrip is most properly architectured control in the WinForms namespace. It has almost unlimited possibilities and very extensible.

Update: According to your comment. I cannot say much, because while ToolStrip is most architectured it is also commented very good, and many particular things need to be discovered. I still added #3 to my answer, but many things can be discovered only by trying, and by using Reflector of course.

like image 37
arbiter Avatar answered Sep 29 '22 16:09

arbiter