Skip to content

Instantly share code, notes, and snippets.

@OrigamiTech
Created November 18, 2010 12:56
Show Gist options
  • Save OrigamiTech/704931 to your computer and use it in GitHub Desktop.
Save OrigamiTech/704931 to your computer and use it in GitHub Desktop.
A standard C# System.Windows.Forms.TextBox, which changes color based on the specified Regex pattern.
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace WKCore
{
public partial class RegexTextBox : TextBox
{
private string
_RegexPattern = "";
private Color
_RegexColorMatch = Color.FromArgb(200, 255, 200),
_RegexColorUnmatch = Color.FromArgb(255, 200, 200),
_RegexColorFatal = Color.FromArgb(255, 0, 0);
public string RegexPattern
{
get { return _RegexPattern; }
set { _RegexPattern = value; Recheck(); }
}
public Color RegexColorMatch
{
get { return _RegexColorMatch; }
set { _RegexColorMatch = value; Recheck(); }
}
public Color RegexColorUnmatch
{
get { return _RegexColorUnmatch; }
set { _RegexColorUnmatch = value; Recheck(); }
}
public Color RegexColorFatal
{
get { return _RegexColorFatal; }
set { _RegexColorFatal = value; Recheck(); }
}
public RegexTextBox()
{ this.TextChanged += new EventHandler(RegexTextBox_TextChanged); }
void RegexTextBox_TextChanged(object sender, EventArgs e)
{ Recheck(); }
private void Recheck()
{
try
{
this.BackColor =
Regex.Match(this.Text, this._RegexPattern).Success
? _RegexColorMatch
: _RegexColorUnmatch;
}
catch
{ this.BackColor = _RegexColorFatal; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment