-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathPlaceholderTextBox.cs
More file actions
75 lines (63 loc) · 1.58 KB
/
PlaceholderTextBox.cs
File metadata and controls
75 lines (63 loc) · 1.58 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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace ReClassNET.Controls
{
public class PlaceholderTextBox : TextBox
{
private Font fontBackup;
private Color foreColorBackup;
private Color backColorBackup;
/// <summary>
/// The color of the placeholder text.
/// </summary>
[DefaultValue(typeof(Color), "ControlDarkDark")]
public Color PlaceholderColor { get; set; } = SystemColors.ControlDarkDark;
/// <summary>
/// The placeholder text.
/// </summary>
[DefaultValue("")]
public string PlaceholderText { get; set; }
public PlaceholderTextBox()
{
fontBackup = Font;
foreColorBackup = ForeColor;
backColorBackup = BackColor;
SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
if (string.IsNullOrEmpty(Text))
{
if (!GetStyle(ControlStyles.UserPaint))
{
fontBackup = Font;
foreColorBackup = ForeColor;
backColorBackup = BackColor;
SetStyle(ControlStyles.UserPaint, true);
}
}
else
{
if (GetStyle(ControlStyles.UserPaint))
{
SetStyle(ControlStyles.UserPaint, false);
Font = fontBackup;
ForeColor = foreColorBackup;
BackColor = backColorBackup;
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (string.IsNullOrEmpty(Text) && Focused == false)
{
using var brush = new SolidBrush(PlaceholderColor);
e.Graphics.DrawString(PlaceholderText ?? string.Empty, Font, brush, new PointF(-1.0f, 1.0f));
}
}
}
}