-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathToolStripSpringTextBox.cs
More file actions
94 lines (79 loc) · 3.67 KB
/
ToolStripSpringTextBox.cs
File metadata and controls
94 lines (79 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
RMLibUI: Visual support classes/components used by multiple R&M Software programs
Copyright (C) 2008-2014 Rick Parrish, R&M Software
This file is part of RMLibUI.
RMLibUI is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
RMLibUI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RMLibUI. If not, see <http://www.gnu.org/licenses/>.
This code file was downloaded from the following MSDN article:
How to: Stretch a ToolStripTextBox to Fill the Remaining Width of a ToolStrip (Windows Forms)
http://msdn.microsoft.com/en-us/library/ms404304%28v=vs.80%29.aspx
It was modified to move it into the RMLibUI namespace
*/
using System;
using System.Drawing;
using System.Windows.Forms;
namespace RandM.RMLibUI
{
public class ToolStripSpringTextBox : ToolStripTextBox
{
public override Size GetPreferredSize(Size constrainingSize)
{
// Use the default size if the text box is on the overflow menu
// or is on a vertical ToolStrip.
if (IsOnOverflow || Owner.Orientation == Orientation.Vertical)
{
return DefaultSize;
}
// Declare a variable to store the total available width as
// it is calculated, starting with the display width of the
// owning ToolStrip.
Int32 width = Owner.DisplayRectangle.Width;
// Subtract the width of the overflow button if it is displayed.
if (Owner.OverflowButton.Visible)
{
width = width - Owner.OverflowButton.Width -
Owner.OverflowButton.Margin.Horizontal;
}
// Declare a variable to maintain a count of ToolStripSpringTextBox
// items currently displayed in the owning ToolStrip.
Int32 springBoxCount = 0;
foreach (ToolStripItem item in Owner.Items)
{
// Ignore items on the overflow menu.
if (item.IsOnOverflow) continue;
if (item is ToolStripSpringTextBox)
{
// For ToolStripSpringTextBox items, increment the count and
// subtract the margin width from the total available width.
springBoxCount++;
width -= item.Margin.Horizontal;
}
else
{
// For all other items, subtract the full width from the total
// available width.
width = width - item.Width - item.Margin.Horizontal;
}
}
// If there are multiple ToolStripSpringTextBox items in the owning
// ToolStrip, divide the total available width between them.
if (springBoxCount > 1) width /= springBoxCount;
// If the available width is less than the default width, use the
// default width, forcing one or more items onto the overflow menu.
if (width < DefaultSize.Width) width = DefaultSize.Width;
// Retrieve the preferred size from the base class, but change the
// width to the calculated width.
Size size = base.GetPreferredSize(constrainingSize);
size.Width = width;
return size;
}
}
}