How to Restrict Textbox Input in Hexadecimal Range

Oct 22 2011 6:12 PM
Hi,
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,





Answers (2)