ToolStripButton text is disabled in ContextMenuStrip

I create my own ToolStripButtonand add it to ContextMenuStrip. It appears, but the text is disabled:

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 truncated as a property AutoSize true?

+3
source share
6 answers

Curious; I can reproduce this ... the oddity for me (for me) is that the setup .Widthfixes it ... but sets it to something (it seems to completely ignore the value):

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

See if this works. This is for me, even if it makes no sense.

Evenly:

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

leaves enough space but

layoutMenus.Width = layoutMenus.Width;

(-, , ).

+3

, . @PeteBaughman, , ContextMenuStrip.Width - ( ContextMenuStrip.MaximumSize : (0,0)).

, , . : ContextMenuStrip.PerformLayout() .

( SuspendLayout ResumeLayout .)


: , ToolStripButton ContextMenuStrip, ToolStripMenuItem :

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

PerformLayout() .

+3

2 . MaxSize, Width . ContextMenuStrip - , " ". "Not Supp".

ContextMenuStrip False. ContextMenuStrip , , .

+2

. , , MaxSize , (200, 0). Reset (0,0) .

+1

: , . , , .

ContextMenuStrip.PerformLayout .

0

I have a similar problem. I want to add a check box to the list of menu items. To do this, I need to add a checkbox in ToolStripControlHost, and then add ToolStripControlHost to the ToolStripMenuItem object. The name of the last flag is always truncated by 3 characters. My solution is to toggle the autosize of the last element node, as shown 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

AutoSize switching should be done after adding the host element to the list.

0
source

Source: https://habr.com/ru/post/1719804/


All Articles