1+ Imports System.ComponentModel
2+ Imports System.Windows.Forms.Design
3+
4+ <ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip), DebuggerStepThrough()>
5+ Public Class ToolStripSpringTextBox
6+ Inherits ToolStripTextBox
7+
8+ Private _MaxWidth As Single
9+
10+ <Category( "Layout" )>
11+ Public Property MaxWidth As Single
12+ Get
13+ Return _MaxWidth
14+ End Get
15+ Set (value As Single )
16+ _MaxWidth = value
17+ End Set
18+ End Property
19+
20+ Public Overrides Function GetPreferredSize(constrainingSize As Size) As Size
21+ ' Use the default size if the text box is on the overflow menu
22+ ' or is on a vertical ToolStrip.
23+ If IsOnOverflow Or Owner.Orientation = Orientation.Vertical Then
24+ Return DefaultSize
25+ End If
26+
27+ ' Declare a variable to store the total available width as
28+ ' it is calculated, starting with the display width of the
29+ ' owning ToolStrip.
30+ Dim width As Int32 = Owner.DisplayRectangle.Width
31+
32+ ' Subtract the width of the overflow button if it is displayed.
33+ If Owner.OverflowButton.Visible Then
34+ width = width - Owner.OverflowButton.Width -
35+ Owner.OverflowButton.Margin.Horizontal()
36+ End If
37+
38+ ' Declare a variable to maintain a count of ToolStripSpringTextBox
39+ ' items currently displayed in the owning ToolStrip.
40+ Dim springBoxCount As Int32 = 0
41+
42+ For Each item As ToolStripItem In Owner.Items
43+
44+ ' Ignore items on the overflow menu.
45+ If item.IsOnOverflow Or Not item.Visible Then Continue For
46+
47+ If TypeOf item Is ToolStripSpringTextBox Then
48+ ' For ToolStripSpringTextBox items, increment the count and
49+ ' subtract the margin width from the total available width.
50+ springBoxCount += 1
51+ width -= item.Margin.Horizontal
52+ Else
53+ ' For all other items, subtract the full width from the total
54+ ' available width.
55+ width = width - item.Width - item.Margin.Horizontal
56+ End If
57+ Next
58+
59+ ' If there are multiple ToolStripSpringTextBox items in the owning
60+ ' ToolStrip, divide the total available width between them.
61+ If springBoxCount > 1 Then width = CInt (width / springBoxCount)
62+
63+ ' If the available width is less than the default width, use the
64+ ' default width, forcing one or more items onto the overflow menu.
65+ If width < DefaultSize.Width Then width = DefaultSize.Width
66+
67+ ' Retrieve the preferred size from the base class, but change the
68+ ' width to the calculated width.
69+ Dim preferredSize As Size = MyBase .GetPreferredSize(constrainingSize)
70+ If _MaxWidth > 0 And width > MaxWidth Then
71+ width = MaxWidth
72+ End If
73+ preferredSize.Width = width
74+
75+ Return preferredSize
76+ End Function
77+ End Class
0 commit comments