winform 文本框 输入限制 问题

itliyi 2009-05-05 04:06:20
如果输入的值大于40,则文本框中值不便,并且也不能出现别的字符,就像e.Handle=false 一样的效果
...全文
202 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
mdq001 2009-05-06
  • 打赏
  • 举报
回复

private void py_KeyPress(object sender, KeyPressEventArgs e)
{
string a = this.py.Text + e.KeyChar.ToString();
if (this.py.SelectedText.Length == 0)
{
if ((int)e.KeyChar != 8 && (int)e.KeyChar != 127)
{
try
{
int x = int.Parse(a);
if (x > 40)
{
e.Handled = true;
}
}
catch
{
MessageBox.Show("error");
e.Handled = true;
}
}
}
else if (this.py.SelectionStart == 1)
{
string b = ((int.Parse(this.py.Text) - int.Parse(this.py.SelectedText)) / 10).ToString() + e.KeyChar.ToString();
try
{
int x = int.Parse(b);
if (x > 40)
{
e.Handled = true;
}
}
catch
{
MessageBox.Show("error");
e.Handled = true;
}
}
else
{
string c = e.KeyChar.ToString() + ((int.Parse(this.py.Text) - int.Parse(this.py.SelectedText) * 10)).ToString();
try
{
int x = int.Parse(c);
if (x > 40)
{
e.Handled = true;
}
}
catch
{
MessageBox.Show("error");
e.Handled = true;
}
}

}


再试试,实现的复杂了点,应该可以更简化的
shui8iuhs 2009-05-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 mdq001 的回复:]
C# code
private void py_KeyPress(object sender, KeyPressEventArgs e)
{
string a = this.py.Text + e.KeyChar.ToString();
if ((int)e.KeyChar != 8 && (int)e.KeyChar != 127)
{
try
{
int x = int.Parse(a);
if (x > 40)
{
e.Handled = tr…
[/Quote]

正解
itliyi 2009-05-06
  • 打赏
  • 举报
回复
11楼 如果我输入34,光标覆盖到34上也就是我想覆盖34重新输入,还是不能输入
wenjie0728 2009-05-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 mdq001 的回复:]
C# code
private void py_KeyPress(object sender, KeyPressEventArgs e)
{
string a = this.py.Text + e.KeyChar.ToString();
if ((int)e.KeyChar != 8 && (int)e.KeyChar != 127)
{
try
{
int x = int.Parse(a);
if (x > 40)
{
e.Handled = tr…
[/Quote]
这个就行吧 没明白意思
ProjectDD 2009-05-06
  • 打赏
  • 举报
回复
UP
itliyi 2009-05-06
  • 打赏
  • 举报
回复
楼上的朋友们 理解错了 我是想如果输入的数字大于40 就不让输 e.Handle=true
textchange 事件 的话还可以输入
2楼的可以 但是如果我输入34再往后面输入大于40 就不能输入 这可以,但如果我把34覆盖再输入就不行
itliyi 2009-05-06
  • 打赏
  • 举报
回复
谢谢楼上朋友,青鸟?莫非你是青鸟的?
zhengli190602169 2009-05-06
  • 打赏
  • 举报
回复
来晚了
rockywu 2009-05-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 baiyunyinv 的回复:]
写文本框的TextChange事件
if(textbox1.Length>40)
{
textbox1.text="原来的40个字符";
}
[/Quote]
简单.......
accpyy 2009-05-05
  • 打赏
  • 举报
回复
楼上的答案就可以了!
baiyunyinv 2009-05-05
  • 打赏
  • 举报
回复
写文本框的TextChange事件
if(textbox1.Length>40)
{
textbox1.text="原来的40个字符";
}
bin_1987_huang 2009-05-05
  • 打赏
  • 举报
回复
很好!
CqCoder 2009-05-05
  • 打赏
  • 举报
回复
直接在KER_CHEANGE 事件里写个IF语句
IF(.){...}
mdq001 2009-05-05
  • 打赏
  • 举报
回复

private void py_KeyPress(object sender, KeyPressEventArgs e)
{
string a = this.py.Text + e.KeyChar.ToString();
if ((int)e.KeyChar != 8 && (int)e.KeyChar != 127)
{
try
{
int x = int.Parse(a);
if (x > 40)
{
e.Handled = true;
}
}
catch
{
MessageBox.Show("error");
e.Handled = true;
}
}
}
walkghost 2009-05-05
  • 打赏
  • 举报
回复
自己在change里面写。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧