如何使文本框仅接受十六进制值 [英] How to make textbox to accept only hexadecimal values

查看:71
本文介绍了如何使文本框仅接受十六进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i在表单上有一个文本框,我想让文本框只接受十六进制值,这意味着值从0到9和a到f。

怎么做。请帮助我。



我尝试过的事情:



i尝试使用[a-zA-Z] regularexpressions.it无法

解决方案

有一种更简单的方法可以做到这一点:

  public   const   string  HexLetters =   0123456789abcdefABCDEF \ b​​;  //   \ b是BackSpace字符 

private void textBox1_KeyPress( object sender,KeyPressEventArgs e)
{
if (!HexLetters.Contains(e.KeyChar))e.Handled = true ;
}

KeyPress EventHandler在KeyDown事件之后被触发,并为我们提供了一种简单的方法来获取按下的键的字符。



如果当前字符不在我们想要允许的字符集中,则设置e.Handled = true;在KeyPress中,EventHandler将阻止当前字符传递给其他可能的订阅者。


  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] +

);
// 保存状态的布尔变量
bool isValid = false ;
if string .IsNullOrEmpty(strInput)){
isValid = ;
} else {
isValid = myRegex.IsMatch(strInput);
}
// 返回结果
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( 无效内容);
}
}


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

解决方案

There is a much simpler way to do this:

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.


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"); } }


这篇关于如何使文本框仅接受十六进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆