GPAL - Generally Positive Automation Library v1.0
GPAL The Fluent Automation LIbrary
Loading...
Searching...
No Matches
PlaceholderTextbox.cs
1// =============================================================================
2// GPAL - Generally Positive Automation Library
3// Copyright © 2026 Software Decisions, Inc. All rights reserved.
4//
5// This file is part of GPAL.
6// Licensed under the Business Source License 1.1
7//
8// Primary development, architecture, and vision by Michael B. Vederman,
9// CEO of Software Decisions, Inc., Texas.
10//
11// Internal development maintained privately.
12// Public releases appear on GitHub: https://github.com/SoftwareDecisionsInc/GPAL.
13//
14// See LICENSE for full terms, including Additional Use Grant.
15// =============================================================================
16
17using System;
18using System.Collections.Generic;
19using System.Drawing;
20using System.Linq;
21using System.Text;
22using System.Threading.Tasks;
23using System.Windows.Forms;
24
26{
30 public class PlaceholderTextBox : TextBox
31 {
32 private bool isPlaceholder = false;
33 private string placeholderText;
34
35 public string PlaceholderText
36 {
37 get { return placeholderText; }
38 set
39 {
40 placeholderText = value;
41 SetPlaceholder();
42 }
43 }
44
45 private bool _suppressPlaceholder = false;
46
47 public PlaceholderTextBox()
48 {
49 TextChanged += OnTextChanged;
50 }
51
52 protected override void OnGotFocus(EventArgs e)
53 {
54 base.OnGotFocus(e);
55 if (isPlaceholder)
56 {
57 _suppressPlaceholder = true;
58 isPlaceholder = false;
59 this.ForeColor = System.Drawing.SystemColors.WindowText;
60 this.Text = "";
61 _suppressPlaceholder = false;
62 }
63 }
64
65 protected override void OnLostFocus(EventArgs e)
66 {
67 base.OnLostFocus(e);
68 if (!isPlaceholder && string.IsNullOrEmpty(this.Text))
69 SetPlaceholder();
70 }
71
72 private void SetPlaceholder()
73 {
74 if (!isPlaceholder)
75 {
76 this.Text = placeholderText;
77 this.ForeColor = Color.Gray;
78 isPlaceholder = true;
79 }
80 }
81
82 private void RemovePlaceHolder()
83 {
84 if (isPlaceholder)
85 {
86 this.ForeColor = System.Drawing.SystemColors.WindowText;
87 isPlaceholder = false;
88 }
89 }
90
91 private void OnTextChanged(object sender, EventArgs e)
92 {
93 if (_suppressPlaceholder || string.IsNullOrEmpty(placeholderText))
94 return;
95
96 if (this.Text.Length == 0 && !this.Focused)
97 SetPlaceholder();
98 else
99 RemovePlaceHolder();
100 }
101 }
102}
103