日期:2014-05-18  浏览次数:20707 次

如何保证我输入一个字符 这个字符必须是0~9 或者ABCDEF (字母必须是大写)
在Textbox1控件里输入一个字符时触发 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)

如何保证我输入一个字符 这个字符必须是0~9 或者ABCDEF (字母必须是大写).


再一个

  private void Form1_Load(object sender, EventArgs e)
  {
  this.textBox1.Focus();

  }

为什么Form首次运行后,textbox没有得到焦点啊




麻烦大家一下,在线等!


------解决方案--------------------
C# code
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar >= 'A' && e.KeyChar <= 'F') || e.KeyChar == '\b'))
    {
        e.Handled = true;
    }
}

------解决方案--------------------
把下面的代码放在窗体类中,来设置焦点
C# code
protected override void OnShown(EventArgs e)
{
    base.OnShown(e);
    this.textBox1.Focus();
}