Reputation: 8431
I am trying to remove the white space around a toolstripcontrolhost control which hosts a calendar control in a contextmenu. See the attached image and code.
VB:
Dim menuItem As ToolStripMenuItem = New ToolStripMenuItem("MyMenu")
Dim calControl As New MonthCalendar
Dim controlHost As ToolStripControlHost = New ToolStripControlHost(calControl)
controlHost.Margin = Padding.Empty
controlHost.Padding = Padding.Empty
ContextMenuStrip1.Items.Add(menuItem)
menuItem.DropDown.Items.Add(controlHost)
C#:
ToolStripMenuItem menuItem = new ToolStripMenuItem("MyMenu");
MonthCalendar calControl = new MonthCalendar();
ToolStripControlHost controlHost = new ToolStripControlHost(calControl);
controlHost.Margin = Padding.Empty;
controlHost.Padding = Padding.Empty;
ContextMenuStrip1.Items.Add(menuItem);
menuItem.DropDown.Items.Add(controlHost);
Upvotes: 3
Views: 3286
Reputation: 437
Most of the answers to this question I've found redirect developers to use a ToolStripDropDown
INSTEAD of a context menu strip. However, for my project, I wanted a drop-down off an already complex context menu. Earlier in my project, for a different popup needed, I had gotten a simple popup wrapper from here: http://www.codeproject.com/Articles/17502/Simple-Popup-Control. This simple popup was inherited from ToolStripDropDown
. Here's the core VB code:
Public Class PopupBox
Inherits ToolStripDropDown
Private mHost As ToolStripControlHost = Nothing
Public ReadOnly Property Host As ToolStripControlHost
Get
Return mHost
End Get
End Property
Public Sub New(content As Control)
MyBase.New()
Me.ResizeRedraw = True
Me.Margin = Padding.Empty
Me.Padding = Padding.Empty
Me.AutoSize = True
Me.mHost = New ToolStripControlHost(content)
Me.mHost.Margin = Padding.Empty
Me.mHost.Padding = Padding.Empty
Me.mHost.AutoSize = True
Me.Items.Add(Me.mHost)
End Sub
End Class
It turns out that this popup is perfect for use as-is to apply a drop-down (with no padding) to an existing context menu. Here are the steps:
ToolStripMenuItem
to your context menu as you normally
would. In this example, the item is named "ToolStripMenuItem1". ToolStripMenuItem1.DropDown = New PopupBox(<your control here>)
Apparently without the inherited object accessing the protected ResizeRedraw
property AND applying the stated values to the Margin
, Padding
, and AutoSize
properties in the constructor of the PopupBox
, the ContextMenuStrip
still applies padding to the left and right of the dropdown. So there's no shortcut.
NOTE: The CodeProject article indicates a GNU License, and also that there were some caveats to what could be hosted by PopupBox, so read the article for more details.
Related/Source links:
Upvotes: 0
Reputation: 36
Try this
ToolStripMenuItem menuItem = new ToolStripMenuItem("MyMenu");
MonthCalendar calControl = new MonthCalendar();
ToolStripControlHost controlHost = new ToolStripControlHost(calControl);
controlHost.Margin = Padding.Empty;
controlHost.Padding = Padding.Empty;
ContextMenuStrip1.Items.Add(menuItem);
ToolStripDropDown dropDown = new ToolStripDropDown();
dropDown.Items.Add(controlHost);
menuItem.DropDown = dropDown;
Upvotes: 0
Reputation: 81610
A ToolStripDropDown
will probably work better in this scenario:
MonthCalendar calControl = new MonthCalendar();
ToolStripControlHost controlHost = new ToolStripControlHost(calControl);
controlHost.Margin = Padding.Empty;
controlHost.Padding = Padding.Empty;
ToolStripDropDown toolDrop = new ToolStripDropDown();
toolDrop.Padding = Padding.Emtpy;
toolDrop.Margin = Padding.Empty;
toolDrop.Items.Add(controlHost);
toolDrop.Show(this, location);
Upvotes: 3