Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
hi,
i have a textbox on form and i want to make the textbox to accept only hexadecimal values,which means values from 0 to 9 and a to f.
how to do it.please help me.

What I have tried:

i have tried using [a-zA-Z] regularexpressions.it was unable
Posted
Updated 10-Aug-16 4:54am

There is a much simpler way to do this:
C#
public const string HexLetters = "0123456789abcdefABCDEF\b"; // \b is the BackSpace character

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (! HexLetters.Contains(e.KeyChar)) e.Handled = true;
}
The KeyPress EventHandler is fired after the KeyDown Event, and gives us an easy way to get the character of the key that was pressed.

If the current character is not in the set of characters we want to allow, then setting e.Handled = true; in the KeyPress EventHandler will stop the current character from being "passed on" to other possible subscribers.
 
Share this answer
 
C#
private void TextBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
	if (IsHexadecimal(Strings.Chr(e.KeyValue)) == false && e.KeyCode != Keys.Back) {
		e.Handled = true;
		e.SuppressKeyPress = true;
	}
}

public bool IsHexadecimal(string strInput)
{

	System.Text.RegularExpressions.Regex myRegex = new System.Text.RegularExpressions.Regex("^[a-fA-F0-9]+$");
	//boolean variable to hold the status
	bool isValid = false;
	if (string.IsNullOrEmpty(strInput)) {
		isValid = false;
	} else {
		isValid = myRegex.IsMatch(strInput);
	}
	//return the results
	return isValid;
}

private void TextBox1_TextChanged(object sender, System.EventArgs e)
{
	if (IsHexadecimal(TextBox1.Text) == false && string.IsNullOrWhiteSpace(TextBox1.Text) == false) {
		TextBox1.Clear();
		MessageBox.Show("Invalid Content");
	}
}
 
Share this answer
 
Comments
charmyvora 10-Aug-16 6:30am    
System.Text.RegularExpressions.Regex myRegex = new System.Text.RegularExpressions.Regex("^[a-fA-F0-9]+$");
string text = textBox1.Text;
bool isHex = myRegex.IsMatch(text);
if (!isHex)
{
textBox1.Text = text.Substring(0, text.Length - 1);
textBox1.SelectionStart = textBox1.Text.Length;
}
Member 12659926 10-Aug-16 6:39am    
if I delete the text in textbox it was giving exception saying that length cannot be zero,wat to do for this
charmyvora 10-Aug-16 6:55am    
This is simple logic, just have if on text change and skip whole logic when blank.
Member 12659926 10-Aug-16 6:59am    
if I do that textbox will accept all alphabets.
Member 12659926 10-Aug-16 6:59am    
I have place the code in textbox1_textchanged() is that right
C#
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            System.Text.RegularExpressions.Regex myRegex = new System.Text.RegularExpressions.Regex("^[a-fA-F0-9]+$");
            string text = textBox1.Text;
            bool isHex = myRegex.IsMatch(text);
            if (!isHex && text.Length > 0)
            {
                textBox1.Text = text.Substring(0, text.Length - 1);
                textBox1.SelectionStart = textBox1.Text.Length;
            }

}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900