How to remove right blank space in ToolStripMenuItem

ss

In the screenshot, I marked this empty space with a green rectangle, I want the left and right spaces to be equal in size in ToolStripMenuItem, but there is a large empty area on the right side that I cannot delete.

Codes:

private void UpdateWorkflowsMenu() { ((ToolStripDropDownMenu)tsddbWorkflows.DropDown).ShowImageMargin = false; tsddbWorkflows.DropDownItems.Clear(); Program.HotkeyManager.Hotkeys.ForEach<HotkeySettings>(x => { if (x.TaskSettings.Job != HotkeyType.None && (!Program.Settings.WorkflowsOnlyShowEdited || !x.TaskSettings.IsUsingDefaultSettings)) { ToolStripMenuItem tsmi = new ToolStripMenuItem(x.TaskSettings.Description); if (x.HotkeyInfo.IsValidHotkey) tsmi.ShortcutKeyDisplayString = " " + x.HotkeyInfo.ToString(); tsmi.Click += (sender, e) => HandleTask(x.TaskSettings); tsddbWorkflows.DropDownItems.Add(tsmi); } }); tsddbWorkflows.Visible = tsddbWorkflows.DropDownItems.Count > 0; } 
+6
source share
3 answers

VB version (actually there are 18 pixels for the arrow: 10 for the size and 8 for the field, leave 2 pixels for the field)

 Parent.DropDown.GetType.GetField("ArrowPadding", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static).SetValue(Nothing, New Padding(0, 0, -16, 0)) 
+1
source

As mentioned above, this space is reserved for the "open submenu" of the arrow, and in general I do not recommend touching it, but, of course, you can delete this space. And in fact, there are several ways to do this, but they all require some encoding. Here's a snippet of the easiest way to do this, you should know the expected width (it can be calculated using ToolstripItem.GetPreferredSize):

 private void RecentButton_DropDownOpening(object sender, EventArgs e) { ToolStripDropDownItem RecentButton = (ToolStripDropDownItem)sender; RecentButton.DropDown.SuspendLayout(); try { RecentButton.DropDownItems.Clear(); // Populate items RecentButton.DropDown.MinimumSize = new Size(RecentButton.Bounds.Right - DisplayRectangle.Left, 0); RecentButton.DropDown.MaximumSize = RecentButton.DropDown.MinimumSize; } finally { RecentButton.DropDown.ResumeLayout(); } } 

The ToolStip tool as a whole is very flexible, and with it you can implement very interesting things when you know its insides.

0
source

Converted Ark response to C #:

 public static void HideArrowMargin(this ToolStripDropDownItem tsddi) { tsddi.DropDown.GetType().GetField("ArrowPadding", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, new Padding(0, 0, -14, 0)); } public static void HideImageMargin(this ToolStripDropDownItem tsddi) { ((ToolStripDropDownMenu)tsddi.DropDown).ShowImageMargin = false; } 

Using the extension for it, I can use it in several places:

 tsddbWorkflows.HideImageMargin(); tsddbWorkflows.HideArrowMargin(); 

Edit:

Now I noticed that this is a static field, it removes the addition of an arrow from all controls. So this is also not a worthy solution.

0
source

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


All Articles