我使用的是VB2008快递。我一直在做一个“弹出窗口”来选择一个日期范围。DateTimePicker并不理想,因为它的目的是选择一个日期范围,该范围始终是从周日到周六的整整一周。这个控件运行得很好,我对此感到非常自豪。我的问题与使用ToolstripControlHost时添加的边框有关。我包含了一个屏幕截图和我的代码。

在下面的代码中,假设有一个名为"btnTimePeriod“的按钮,我希望在它下面显示一个面板,其中包含一些自定义项,面板的名称是"pnlDateRangePicker”。
它起作用了..。但看起来不太对劲。面板本身是147x326像素,但请注意,在附加的图形中,它在面板周围添加了一个我不想要的边框。在顶部、底部和左侧有一个边框。但由于某些原因,右边的边框特别大。虽然我的代码没有明确地设置它,但是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发布于 2011-11-12 05:03:07
我从来没有使用过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 Subhttps://stackoverflow.com/questions/8099230
复制相似问题