private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((Convert.ToInt32(e.KeyChar) >= 48) && Convert.ToInt32(e.KeyChar) < 58)
textBox1.Text = textBox1.Text + e.KeyChar;
else
{ MessageBox.Show("请确保输入数字");}
e.Handled = true;
}
//这样就可以了 但是这个实际操作还有很多问题的
//你如果覆盖输入好象不能覆盖的
//还有就是关标一直到第一个位置//这样比较好哦 什么小数点的你自己考虑
if ((Convert.ToInt32(e.KeyChar) >= 48) && Convert.ToInt32(e.KeyChar) < 58)
{}
else
{
MessageBox.Show("请确保输入数字");
e.Handled = true;
}
{
if ((Convert.ToInt32(e.KeyChar) >= 48) && Convert.ToInt32(e.KeyChar) < 58)
textBox1.Text = textBox1.Text + e.KeyChar;
else
{ MessageBox.Show("请确保输入数字");}
e.Handled = true;
}
//这样就可以了 但是这个实际操作还有很多问题的
//你如果覆盖输入好象不能覆盖的
//还有就是关标一直到第一个位置//这样比较好哦 什么小数点的你自己考虑
if ((Convert.ToInt32(e.KeyChar) >= 48) && Convert.ToInt32(e.KeyChar) < 58)
{}
else
{
MessageBox.Show("请确保输入数字");
e.Handled = true;
}
小数点我也不会弄,我写成这样不行呀:
if ((Convert.ToInt32(e.KeyChar) >= 48) && (Convert.ToInt32(e.KeyChar) < 58) && (Convert.ToInt32(e.KeyChar) == 46))
{ }
else
{
MessageBox.Show("请确保输入数字");
e.Handled = true;
}
/// <summary>
/// TextBox自定义
/// </summary>
class zdyTextbox : System.Windows.Forms.TextBox
{
//上次输入的内容
protected string strText = string.Empty; //当前关标的位置
protected int Selectiondigit = 0; //确定当前是键盘输入 还是复制
protected Boolean blZhuanTai = false; //输入的类型
protected Type type; //小数点的位数
protected int digit = 0; #region ========== TextBox自定义限定 ========= /// <summary>
/// 位数(文字类型无效,正整数数数值位数,其他小数点后的位数)
/// </summary>
public int Digit
{
set
{
digit = value;
}
get
{
return digit;
}
} /// <summary>
/// TextBox写入内容的格式
/// </summary>
public Type Type
{
set
{
if (value == Type.文字)
{
this.digit = 0;
}
type = value;
}
get
{
return type;
}
} /// <summary>
/// 触发键盘事件
/// </summary>
/// <param name="e"></param>
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
this.blZhuanTai = true;
switch (this.type)
{
case Type.文字:
if (e.KeyChar != '\'')
{
//输入都有效
base.OnKeyPress(e);
Selectiondigit = this.SelectionStart + 1;
break;
}
e.Handled = true;
break;
case Type.正数:
if (char.IsDigit(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == (char)8)
{//只有输入为数值,小数点,退格符 //小数点
if (e.KeyChar == '.')
{
e.Handled = this.InDigit();
} if (!e.Handled)
Selectiondigit = this.SelectionStart+1;
base.OnKeyPress(e);
break;
}
e.Handled = true;
break;
case Type.正整数:
if (e.KeyChar == '/' && this.Text.Length == 0)
{
base.OnKeyPress(e);
break;
}
else
{
if (!(this.Text.Length > 0 && this.Text.Substring(0, 1).Equals("/")) || e.KeyChar == (char)8)
{
if ((char.IsDigit(e.KeyChar) || e.KeyChar == (char)8))
{//只有输入为数值,退格符有效 if (this.Text.Length > this.digit && this.digit != 0)
{ e.Handled = true; } if (!e.Handled)
Selectiondigit = this.SelectionStart + 1;
base.OnKeyPress(e);
break;
}
}
}
e.Handled = true;
break;
}
} /// <summary>
/// TextBox的内容修改触发事件
/// </summary>
/// <param name="e"></param>
protected override void OnTextChanged(EventArgs e)
{
this.Text = CheckDigit(this.Text.ToString().TrimEnd());
this.SelectionStart = this.Selectiondigit;
this.blZhuanTai = true;
base.OnTextChanged(e);
} /// <summary>
/// 控件失去焦点时触发事件
/// </summary>
/// <param name="e"></param>
protected override void OnLeave(EventArgs e)
{
this.Text = this.Text.ToString().TrimEnd();
if (this.Text != "")
{
if (this.Text.LastIndexOf('.') + 1 == this.Text.Length)
{
this.Text = this.Text.Substring(0, this.Text.Length - 1);
}
}
base.OnLeave(e);
} /// <summary>
/// 判断小数的位数(键盘输入)
/// </summary>
/// <param name="KeyChar"></param>
/// <param name="Digit"></param>
/// <returns></returns>
protected string CheckDigit(string strValues)
{
if (this.digit != 0)
{
try
{
if (strValues.TrimEnd() != "")
{
System.Double.Parse(strValues.TrimEnd());
}
}
catch (Exception ex)
{
// MessageBox.Show("控件 " + ex.Message , "提示", MessageBoxButtons.OK);
this.Text = "";
}
int LastIndex = strValues.LastIndexOf('.');
if (LastIndex != -1)
{
if ((strValues.Length - LastIndex) >= this.digit + 1)
{
strValues = strValues.Substring(0, LastIndex + 1 + this.digit);
}
else
{
if (!this.blZhuanTai)
{
strValues = strValues.PadRight(LastIndex + 1 + this.digit, '0');
}
}
}
else
{
if (!this.blZhuanTai)
{
strValues += ".";
strValues = strValues.PadRight(LastIndex + 1 + this.digit, '0');
}
}
if (this.type == Type.正整数)
{
if (strValues.Length >= this.digit)
{
strValues = strValues.Substring(0, this.digit);
}
}
}
return strValues;
} /// <summary>
/// 小数的输入
/// </summary>
/// <returns></returns>
private Boolean InDigit()
{
if (this.SelectionStart == 0)
{//小数点在第一位置无效
return true;
}
if (this.Text.LastIndexOf('.') != -1)
{//小数点存在
if (this.SelectedText.LastIndexOf('.') != -1)
{//要覆盖的有小数点有效
return false;
}
else
{//反之无效
return true;
}
}
else
{//小数点不存在有效
return false;
}
} #endregion } public enum Type { 文字 = 0,正数, 正整数 }
static text1;
try
{
decimal d=Convert.ToDecimal(TextBox1.Text);
}
catch
{
textBox1.Text=text1;
}
text1=textBox1.Text;
static string text1;
try
{
decimal d=Convert.ToDecimal(TextBox1.Text);
}
catch
{
textBox1.Text=text1;
}
text1=textBox1.Text;
{
if ((Convert.ToInt32(e.KeyChar) >= 48) && Convert.ToInt32(e.KeyChar) < 58)
{
//textBox1.Text = textBox1.Text + e.KeyChar;
}
else
{
MessageBox.Show("请确保输入数字");
e.Handled = true;
} }
把往文本加数字的注释就好了,本你就想文本输入数据,textBox1_KeyPress只不是是键盘按下去的事件