Created
November 18, 2010 12:56
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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