我正在使用VB2008 Express。我一直在制作一个“弹出窗口”来选择日期范围。 DateTimePicker并不理想,因为目的是从星期日到星期六选择一个整周的日期范围。控制工作正常,我为此感到自豪。我的问题与使用ToolstripControlHost时添加的边框有关。我已经包含了截图和我的代码。
在下面的代码中,假设存在一个名为“btnTimePeriod”的按钮,在该按钮下方我希望显示一个面板,其中包含一些自定义项目,面板的名称为“pnlDateRangePicker”。
IT工作......但看起来并不合适。面板本身是147 x 326像素,但在附图中注意到它在面板周围添加了一个我不想要的边框。顶部,底部和左侧有一个边框......但由于某种原因,右边的边框特别大。虽然我的代码没有明确地设置它,但AutoSize = true所以我希望它在面板周围缩小。
根据需要,我的代码已经将ShowCheckMargin和ShowImageMargin设置为false。我没有包含DrawDateCalander Sub的代码,因为它不相关。我相信即使是一个空白面板也会产生相同的结果。我不知道这个边际来自哪里。有什么指导吗?
Private Sub btnTimePeriod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimePeriod.Click
Call DrawDateCalendar(DatePart(DateInterval.Month, FirstDisplayedSunday), DatePart(DateInterval.Year, FirstDisplayedSunday))
Call ShowControlBelow(btnTimePeriod, pnlDateRangePicker)
End Sub
Sub ShowControlBelow(ByVal Showbutton As Control, ByVal ShownControl As Control)
Dim PopupContainer As New ToolStripControlHost(ShownControl)
PopupContainer.Margin = New Padding(0)
Dim mnuDropDown As New ContextMenuStrip
mnuDropDown.Padding = New Padding(0)
mnuDropDown.ShowCheckMargin = False
mnuDropDown.ShowImageMargin = False
mnuDropDown.Items.Add(PopupContainer)
ShowMenuBelow(Showbutton, mnuDropDown)
End Sub
Sub ShowMenuBelow(ByVal Showbutton As Control, ByVal WhichMenu As ContextMenuStrip, Optional ByVal AlignRight As Boolean = False)
Dim x As Integer = 0
Dim y As Integer = 0
Dim itscontainer As Control = Showbutton.Parent
x = Showbutton.Location.X
y = Showbutton.Location.Y
If Not itscontainer Is Nothing Then
Do Until TypeOf itscontainer Is Form
x = x + itscontainer.Location.X
y = y + itscontainer.Location.Y
itscontainer = itscontainer.Parent
If itscontainer Is Nothing Then Exit Do
Loop
End If
y = y + Showbutton.Height
If AlignRight = True Then
x = x - WhichMenu.Width + Showbutton.Width
End If
Dim xy As New Point(x, y)
WhichMenu.Show(Showbutton.FindForm, xy)
End Sub
答案 0 :(得分:3)
我从未使用过ContextMenuStrip
,也许这就是问题所在。
您可以尝试使用ToolStripDropDown
代替:
Private Sub ShowControl(ByVal fromControl As Control, ByVal whichControl As Control)
'\\ whichControl needs MinimumSize set:
whichControl.MinimumSize = whichControl.Size
Dim toolDrop As New ToolStripDropDown()
Dim toolHost As New ToolStripControlHost(whichControl)
toolHost.Margin = New Padding(0)
toolDrop.Padding = New Padding(0)
toolDrop.Items.Add(toolHost)
toolDrop.Show(Me, New Point(fromControl.Left, fromControl.Bottom))
End Sub
Private Sub btnTimePeriod_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnTimePeriod.Click
Call DrawDateCalendar(DatePart(DateInterval.Month, FirstDisplayedSunday), DatePart(DateInterval.Year, FirstDisplayedSunday))
'\\Call ShowControlBelow(btnTimePeriod, pnlDateRangePicker)
Call ShowControl(btnTimePeriod, pnlDateRangePicker)
End Sub