-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoundedTextBox.cs
188 lines (161 loc) · 5.67 KB
/
RoundedTextBox.cs
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class RoundedTextBox : UserControl
{
private TextBox textBox;
private const int paddingLeft = 15; // Padding between the text box and the rounded rectangle
private const int paddingRight = 15; // Padding between the text box and the rounded rectangle
private const int paddingTop = 15; // Padding between the text box and the rounded rectangle
private const int paddingBottom = 20; // Padding between the text box and the rounded rectangle
public RoundedTextBox()
{
textBox = new TextBox
{
Multiline = false,
BorderStyle = BorderStyle.FixedSingle,
TextAlign = HorizontalAlignment.Center
};
Controls.Add(textBox);
// Attach event handlers
textBox.KeyPress += TextBox_KeyPress;
textBox.KeyUp += TextBox_KeyUp;
textBox.KeyDown += TextBox_KeyDown;
textBox.GotFocus += TextBox_GotFocus;
textBox.LostFocus += TextBox_LostFocus;
}
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
//keyPressInRoundedTextBox?.Invoke(this, e);
base.OnKeyPress(e);
}
//add other event handlers
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
//keyUpInRoundedTextBox?.Invoke(this, e);
base.OnKeyUp(e);
}
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
//keyDownInRoundedTextBox?.Invoke(this, e);
base.OnKeyDown(e);
}
private void TextBox_GotFocus(object sender, EventArgs e)
{
//gotFocusInRoundedTextBox?.Invoke(this, e);
base.OnGotFocus(e);
}
private void TextBox_LostFocus(object sender, EventArgs e)
{
//lostFocusInRoundedTextBox?.Invoke(this, e);
base.OnLostFocus(e);
}
public new bool Focus()
{
return base.Focus() && textBox.Focus();
}
public new string Text
{
get { return textBox.Text; }
set { textBox.Text = value; }
}
public new Font Font
{
get { return textBox.Font; }
set { textBox.Font = value; }
}
public new int Width
{
get { return base.Width; }
set
{
textBox.Width = value - paddingLeft - paddingRight;
Size = new Size(value, textBox.Height + paddingTop + paddingBottom);
}
}
public new Point Location
{
get { return base.Location; }
set
{
base.Location = value;
textBox.Location = new Point(paddingLeft, paddingTop);
}
}
public int MaxLength
{
get { return textBox.MaxLength; }
set { textBox.MaxLength = value; }
}
public new BorderStyle BorderStyle
{
get { return textBox.BorderStyle; }
set { textBox.BorderStyle = value; }
}
public new Color BackColor
{
get { return textBox.BackColor; }
set { textBox.BackColor = value; }
}
public new Color ForeColor
{
get { return textBox.ForeColor; }
set { textBox.ForeColor = value; }
}
public new object Tag
{
get { return textBox.Tag; }
set { textBox.Tag = value; }
}
public HorizontalAlignment TextAlign
{
get { return textBox.TextAlign; }
set { textBox.TextAlign = value; }
}
public new Padding Margin
{
get { return textBox.Margin; }
set { textBox.Margin = value; }
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (var path = new GraphicsPath())
using (var pen = new Pen(Color.White, 2))
{
RectangleF rect = new RectangleF(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width - 1, ClientRectangle.Height - 1);
int radius = 10;
// Create a rounded rectangle path
path.AddArc(rect.X, rect.Y, 2 * radius, 2 * radius, 180, 90); // Top-left corner
path.AddArc(rect.X + rect.Width - 2 * radius, rect.Y, 2 * radius, 2 * radius, 270, 90); // Top-right corner
path.AddArc(rect.X + rect.Width - 2 * radius, rect.Y + rect.Height - 2 * radius, 2 * radius, 2 * radius, 0, 90); // Bottom-right corner
path.AddArc(rect.X, rect.Y + rect.Height - 2 * radius, 2 * radius, 2 * radius, 90, 90); // Bottom-left corner
path.CloseFigure();
// Draw the rounded rectangle
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.FillPath(Brushes.White, path);
e.Graphics.DrawPath(pen, path);
}
}
protected void OnPaintV1(PaintEventArgs e)
{
base.OnPaint(e);
using (var path = new GraphicsPath())
using (var pen = new Pen(Color.White, 2))
{
RectangleF rect = new RectangleF(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width - 1, ClientRectangle.Height - 1);
int radius = 10;
// Create a rounded rectangle path
path.AddArc(rect.X, rect.Y, radius, radius, 180, 90); // Top-left corner
path.AddArc(rect.X + rect.Width - radius, rect.Y, radius, radius, 270, 90); // Top-right corner
path.AddArc(rect.X + rect.Width - radius, rect.Y + rect.Height - radius, radius, radius, 0, 90); // Bottom-right corner
path.AddArc(rect.X, rect.Y + rect.Height - radius, radius, radius, 90, 90); // Bottom-left corner
path.CloseFigure();
// Draw the rounded rectangle
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.FillPath(Brushes.White, path);
e.Graphics.DrawPath(pen, path);
}
}
}