Question

I am instantiating my own ToolStripButton and adding it to a ContextMenuStrip. It pops up but the text gets cut off:

string[] layouts = new string[]{"Test 1", "Test 2", "Test 3"};
List<ToolStripButton> items = new List<ToolStripButton>();
foreach (string layout in layouts)
{
    ToolStripButton item = new ToolStripButton(layout, image, LayoutClicked);
    item.AutoSize = true;
    items.Add(item);
}
layoutMenus.Items.Clear();
layoutMenus.Items.AddRange(items.ToArray());
layoutMenus.Show(Cursor.Position.X, Cursor.Position.Y);

Any idea why the text is getting cut off as the AutoSize property is true?

Was it helpful?

Solution

Curious; I can reproduce this... a reall oddity (for me) is that setting the menu's .Width fixes it... but setting it to anything (it seems to ignore the value completely):

layoutMenus.Width = 800; // could be 20, or 100 and would appear the same

See if that works. It does for me, even though it makes no real sense.

Even:

layoutMenus.Width++;
layoutMenus.Width--;

leaves enough space, but

layoutMenus.Width = layoutMenus.Width;

doesn't (presumably it checks for non-changes and ignores whatever side-effects the above has).

OTHER TIPS

After some refactoring of a previously working context menu implementation, I have been facing the same problem. Like @PeteBaughman, I was not able to fix this by changing the ContextMenuStrip.Width - the width in fact did not change (also ContextMenuStrip.MaximumSize was not speficied: (0,0)).

Adding an invisible item as Pete suggested did the trick. After a few more experiments I have found another workaround: A call to ContextMenuStrip.PerformLayout() resulted in the expected behavior.

(Calling SuspendLayoutand ResumeLayout before and after adding the items did not work.)


Update: The real problem might be, that a ToolStripButton is added to a ContextMenuStrip which would normally create a ToolStripMenuItem when called like this:

contextMenuStrip.Items.Add("Hello world"); // Returns a ToolStripMenuItem

I was able to omit the call to PerformLayout() after creating items of the right kind.

2 Years Later, I've run into the same issue. I didn't have MaxSize set and touching the Width property didn't help either. My ContextMenuStrip only contained one item - a grayed out button that said "Not Supported". It got truncated down to "Not Supp".

I ended up having to add a dummy item to the end of the ContextMenuStrip and set its visibility to False. Then, the ContextMenuStrip was wide enough to show the first item that I actually cared about showing.

Link here

I have the same problem. Tried all these things, and it turned out there the MaxSize is set by mistake, like (200, 0). Reset it to (0,0) fix the problem.

To add a bit of detail to the symptom: in my case, it seems that the issue only occurs when there is just one item in the context menu. With multiple items, the width seems to be set correctly.

And calling ContextMenuStrip.PerformLayout fixed this one for me.

I have similar problem. I want to add checkbox to the menu item list. In order to do that, I need to add the checkbox to a ToolStripControlHost and then add the ToolStripControlHost to the ToolStripMenuItem object. The name of the last checkbox always get truncated by 3 characters. My solution is to toggle the AutoSize of the last item host as below:

Dim chkbox As System.Windows.Forms.CheckBox = New System.Windows.Forms.CheckBox()
chkbox.Text = "xxxxx"
Dim tshost As ToolStripControlHost = New ToolStripControlHost(chkbox)
tsmiAssemblySpecificTools.DropDownItems.Add(tshost)
                    tshost.AutoSize = False
                    tshost.AutoSize = True

The AutoSize toggle has to be done after the host item added to the list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top