Can you please take a look at following code and let me know how I can restrict the "txtColor.Text" in a way that if user insert a wrong hex sting(value) like "KKKKKK" the exception handler handle the issue and pops a warning message to correct the input?
Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SimpleColorPicker
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void txtColor_TextChanged(object sender, EventArgs e)
{
if (txtColor.Text == "")
{
lblColor.ForeColor = System.Drawing.Color.MediumPurple;
}
else
{
string cc = String.Format("#{0}", txtColor.Text);
Color col = System.Drawing.ColorTranslator.FromHtml(cc);
lblColor.ForeColor = col;
lblColor.Refresh();
}
}
}
}
Thanks,
Thanks,
Javeed M ShaikhPosted Oct 22, 2011, 9:11 PM
use the following regex to validate your texbox:
^[0-9A-Fa-f]+$
Please do not forget to mark "Accepted Answer".
Prabhu RajaPosted Oct 23, 2011, 12:44 AM
You can use RegularExpressionValidator to check your Textbox having Valid Hexadecimal Color Codes or not before submitting the page.