我正在使用 VB2008 Express。我一直在研究一个“弹出窗口”来选择一个日期范围。DateTimePicker 并不理想,因为其目的是选择一个日期范围,该范围始终为一整周,从星期日到星期六。控件工作得很好,我为此感到非常自豪。我的问题与为此使用 ToolstripControlHost 时添加的边框有关。我已经包含了屏幕截图和我的代码。
在下面的代码中,假设存在一个名为“btnTimePeriod”的按钮,我希望在该按钮下方显示一个面板,其中包含一些自定义项,面板的名称是“pnlDateRangePicker”。
它工作......但它看起来不正确。面板本身是 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