gpt4 book ai didi

vb.net - 填充/大小/边距,当使用 ToolstripControlHost 作为弹出控件时

转载 作者:行者123 更新时间:2023-12-01 16:02:15 24 4
gpt4 key购买 nike

我正在使用 VB2008 Express。我一直在研究一个“弹出窗口”来选择一个日期范围。 DateTimePicker 并不理想,因为其目的是选择一个日期范围,该范围始终是从周日到周六的整整一周。控件工作正常,我为此感到非常自豪。我的问题与为此使用 ToolstripControlHost 时添加的边框有关。我附上了屏幕截图和我的代码。

enter image description here

在下面的代码中,假设存在一个名为“btnTimePeriod”的按钮,我希望在其下方显示一个面板,其中包含一些自定义项,面板的名称为“pnlDateRangePicker”。

它可以工作...但它看起来不正确。面板本身是 147 x 326 像素,但请注意所附图形中它在面板周围添加了我不想要的边框。上、下、左各有一个边框……不知为什么,右边的边框特别大。尽管我的代码没有明确设置它,但 AutoSize = true 所以我预计它会在面板周围缩小。

根据需要,我的代码已经将 ShowCheckMargin 和 ShowImageMargin 设置为 false。我没有包含 DrawDateCalander Sub 的代码,因为它不相关。我相信即使是空白面板也会产生相同的结果。我不知道这个 margin 是从哪里来的。有什么指导吗?

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

最佳答案

我从来没有为此使用过 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

关于vb.net - 填充/大小/边距,当使用 ToolstripControlHost 作为弹出控件时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8099230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com