A set of .NET Framework managed libraries for developing graphical user interfaces.
I haven't gotten all my questions answered yet, but I've fixed the parts that I think can be fixed. AttachKeyEvent has been renamed to AttachEvents.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
public partial class CustomTextBox : TextBox
{
private int maxCharacters;
public int MaxCharacters {
get { return maxCharacters; }
set {
maxCharacters = value;
Text = Text.Length > maxCharacters ? Text.Substring(0, maxCharacters) : Text;
}
}
private bool useUpper = false;
public bool UseUpper {
get { return useUpper; }
set { useUpper = value; }
}
public CustomTextBox() {
CustomTextBoxWork.AttachEvents(this);
}
protected override void Dispose(bool disposing) {
base.Dispose(disposing);
CustomTextBoxWork.DetachEvents(this);
}
}
class CustomTextBoxWork
{
[ThreadStatic]
[Obsolete]
private static Dictionary<Control, Color> originalBackColors;
// not use originalBackColors
#pragma warning disable 0612
public static Dictionary<Control, Color> OriginalBackColors {
get {
if (originalBackColors == null) {
originalBackColors = new Dictionary<Control, Color>();
}
return originalBackColors;
}
}
#pragma warning restore 0612
public static void AttachEvents(TextBox txb) {
txb.KeyDown += Txb_KeyDown;
txb.LostFocus += Txb_LostFocus;
txb.ParentChanged += Txb_ParentChanged;
txb.TextChanged += Txb_TextChanged;
}
public static void DetachEvents(TextBox txb) {
txb.KeyDown -= Txb_KeyDown;
txb.LostFocus -= Txb_LostFocus;
txb.ParentChanged -= Txb_ParentChanged;
txb.TextChanged -= Txb_TextChanged;
if (OriginalBackColors.ContainsKey(txb)) {
OriginalBackColors.Remove(txb);
}
}
private static void Txb_ParentChanged(object sender, EventArgs e) {
var txb = (TextBox)sender;
if (txb.Parent != null) {
OriginalBackColors[txb] = txb.Parent.BackColor;
txb.BackColor = OriginalBackColors[txb];
}
}
private static void Txb_KeyDown(object sender, KeyEventArgs e) {
var txb = (TextBox)sender;
if (e.KeyCode == Keys.Escape) {
txb.Text = "";
}
if (e.KeyCode == Keys.Enter) {
txb.BackColor = Color.Yellow; // Change the back color to a different color when Enter is pressed
Control ctl = txb;
while (ctl.Parent != null) {
var next = ctl.Parent.GetNextControl(ctl, true);
if (next != null) {
next.Focus();
e.SuppressKeyPress = true;
e.Handled = true;
return;
}
ctl = ctl.Parent;
}
}
}
private static void Txb_TextChanged(object sender, EventArgs e) {
var txb = (CustomTextBox)sender;
if (txb.UseUpper && txb.TextLength > 0) {
string prevText = txb.Text;
string nextText = ToUseUpper(prevText);
if (nextText != prevText) {
int selectionStart = txb.SelectionStart;
int selectionLength = txb.SelectionLength;
txb.Text = nextText;
txb.Select(selectionStart, selectionLength);
}
}
}
private static string ToUseUpper(string text) {
const string space = " ";
var args = text.Split(space.ToCharArray());
for (int i = 0; i < args.Length; i++) {
if (!string.IsNullOrWhiteSpace(args[i])) {
args[i] = char.ToUpper(args[i][0]) + args[i].Substring(1);
}
}
return string.Join(space, args);
}
private static void Txb_LostFocus(object sender, EventArgs e) {
var txb = (TextBox)sender;
if (OriginalBackColors.TryGetValue(txb, out Color originalBackColor)) {
txb.BackColor = originalBackColor;
}
}
}