-
-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathBorderedTextBox.cs
More file actions
141 lines (106 loc) · 3.69 KB
/
BorderedTextBox.cs
File metadata and controls
141 lines (106 loc) · 3.69 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
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
using SharpBrowser.Config;
using SharpBrowser.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Reflection.Metadata;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SharpBrowser.Controls {
public static class BorderedTextBoxUtils {
/// <summary>
/// Wraps an exisiting textbox with a panel which has roundd borders and a custom backcolor.
/// Returns the panel which wraps the textbox.
/// </summary>
public static BorderedTextBox ToBordered(this TextBox tbx) {
//backup previous TBX status
var tbxOriginalParent = tbx.Parent;
var loc = tbx.Location;
var size = tbx.Size;
var anchor = tbx.Anchor;
var dock = tbx.Dock;
//restore them to Panel
var panel = new BorderedTextBox(tbx);
panel.Location = loc;
panel.Size = size;
//bordered_tbx.Height= size.Height+5;
panel.Anchor = anchor;
panel.Dock = dock;
tbxOriginalParent.Controls.Add(panel);
panel.BackColor = Color.Transparent;
//remove tbx min size. it causes tbx getting clipped.
if (tbx.MinimumSize.Height != 0 && tbx.MinimumSize.Height >= panel.MinimumSize.Height) {
tbx.MinimumSize = new Size();
}
return panel;
}
}
public class BorderedTextBox : Panel {
private TextBox textBox;
private bool focusedAlways = false;
private Color normalBorderColor = Color.LightGray;
private Color focusedBorderColor = Color.FromArgb(153, 187, 239); //edge light blue
public int borderThickness = 2;
public TextBox TextBox {
get { return textBox; }
//set { textBox = value; }
}
public bool FocusedAlways {
get { return focusedAlways; }
set { focusedAlways = value; }
}
public BorderedTextBox(TextBox tbx = null) {
this.DoubleBuffered = true;
this.Padding = new Padding(1 + borderThickness * 2);
this.Height += borderThickness * 2;
if (tbx == null)
textBox = new TextBox();
else
textBox = tbx;
this.TextBox.AutoSize = false;
this.TextBox.BorderStyle = BorderStyle.None;
this.TextBox.Dock = DockStyle.Fill;
this.TextBox.Enter += TextBox_Refresh;
this.TextBox.Leave += TextBox_Refresh;
this.TextBox.Resize += TextBox_Refresh;
this.Controls.Add(this.TextBox);
}
private void TextBox_Refresh(object sender, EventArgs e) => this.Invalidate();
protected override void OnPaint(PaintEventArgs e) {
this.Padding = new Padding(
left: 15 + borderThickness,
right: 15 + borderThickness,
top: 1 + borderThickness + 5,
bottom: borderThickness + 5
);
this.AutoSize = false;
var txtHeight = MeasureHeight(textBox);
this.Height = txtHeight
+ Padding.Top + Padding.Bottom;
var color = this.TextBox.Focused || focusedAlways ? focusedBorderColor : normalBorderColor;
using (Pen borderPen = new Pen(color, borderThickness)) {
var brushbg = new SolidBrush(textBox.BackColor);
var w = this.ClientSize.Width - borderThickness * 2;
var h = this.ClientSize.Height - borderThickness * 2;
var radius = h / 2;
e.Graphics.FillRoundRectangle(brushbg,
new Rectangle(0 + borderThickness, 0 + borderThickness,w,h),radius);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawRoundRectangle(borderPen,
new Rectangle(0 + borderThickness, 0 + borderThickness,w,h),radius);
}
base.OnPaint(e);
}
static int MeasureHeight(TextBox textbox) {
Size size = TextRenderer.MeasureText("AĞÜüğGgpPa", textbox.Font, Size.Empty, TextFormatFlags.TextBoxControl);
//textbox.MinimumSize = new Size(0, s.Height + 1 + 3);
return size.Height;
}
}
}