| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3584 人关注过本帖, 1 人收藏
标题:[原创]关于用TextBox来验证数据的有效性(只允许输入带两位小数的数字)
只看楼主 加入收藏
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
收藏(1)
 问题点数:0 回复次数:3 
[原创]关于用TextBox来验证数据的有效性(只允许输入带两位小数的数字)

*/ --------------------------------------------------------------------------------------
*/ 出自: 编程中国 http://www.bc-cn.net
*/ 作者: C_B_Lu QQ:184118549
*/ 时间: 2007-9-4 编程论坛首发
*/ 声明: 尊重作者劳动,转载请保留本段文字
*/ --------------------------------------------------------------------------------------

在维护界面中,常常需要对在TextBox中输入的数据进行验证,以保证其有效性,
以前自己也有在论坛中发表过,这次做了些小的改进,不过感觉还是有些繁锁,然与大家一起讨论类似问题.
如下例,是一个保确只输入带两位小数的数字,如果配合设置其TextBox控件的MaxLength属性,可以控制该数值的大小.
注: tbPartsCost为一个TextBox控件.tbPartsCost_KeyPress和tbPartsCost_Validating分别是该控件的KeyPress事件和Validating事件.

private void tbPartsCost_KeyPress(object sender, KeyPressEventArgs e)
{
TextBox tb = (TextBox)sender;
int curPos = tb.SelectionStart; // 當前光標位置
int pointPos = tb.Text.LastIndexOf('.'); // 小數點的位置
if ((pointPos > 0) && (curPos > (pointPos + 2))) // 輸入超過兩位小數﹐
{
e.Handled = true;
return;
}
string strInt = tb.Text.Trim().Substring(0, curPos);
string strDec= "";
if(pointPos <0)
{
strDec = ".00";
}
else
{
strDec = tb.Text.Trim().Substring(pointPos);
if (strDec.Length > 3)
{
strDec = strDec.Substring(0, 3);
}
else
{
strDec = strDec.PadRight(3, '0');
}
}

if ((e.KeyChar < '0' || e.KeyChar > '9') && (e.KeyChar != '.') && (e.KeyChar != '\b'))
{ // 如果用戶輸入的不是字符~9﹐也不是刪除鍵和小數點﹐則不顯示﹐
e.Handled = true; // 不允許輸入
}
else if (e.KeyChar == '.')
{
e.Handled = true;
if (strInt.Length == 0)
{
strInt = "0";
}
tb.Text = strInt + strDec;
tb.SelectionStart = ++curPos;
}
else
{
e.Handled = false;
if (curPos > pointPos)
{
tb.SelectionLength = 1;
}
else if (curPos == pointPos)
{
tb.SelectionStart = ++curPos;
SendKeys.Send(e.KeyChar.ToString());
}
if (pointPos < 0)
{
tb.Text = strInt + strDec;
pointPos = tb.Text.LastIndexOf('.'); // 小數點的位置
}
if ((curPos < pointPos) && (tb.Text.Trim().Length == tb.MaxLength))
{
tb.SelectionLength = 1;
}

tb.SelectionStart = curPos;
}
}

private void tbPartsCost_Validating(object sender, CancelEventArgs e)
{
TextBox tb = (TextBox)sender;
string[] strs = tb.Text.Split(new char[] { '.' });
if (strs[0].Trim() == "")
{
strs[0] = "0";
}
strs[0] =int.Parse( strs[0]).ToString().Trim();
if (strs[0].Length > tb.MaxLength - 3)
{
strs[0] = strs[0].Substring(0, tb.MaxLength - 3);
}
strs[1] = strs[1].Trim();
if (strs[1].Length < 2)
{
strs[1] = strs[1].PadRight(2, '0');
}
tb.Text = strs[0] + "." + strs[1];
}

搜索更多相关主题的帖子: TextBox 小数 有效性 数字 数据 
2007-09-04 23:13
卡卡艾
Rank: 6Rank: 6
等 级:贵宾
威 望:22
帖 子:672
专家分:0
注 册:2007-4-3
收藏
得分:0 
支持原创,不过给人感觉确实有点繁琐

革命尚未成功,同志仍需努力-----+++
2007-09-05 00:10
师妃暄
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:805
专家分:107
注 册:2006-3-1
收藏
得分:0 
很有自己的想法

不过有一个BUG.当我首先输入0,会显示0.00,这时候我后退,把第一个0删除.那么输入就不受任何限制了

还可以到网上搜集些资料综合一下

[此贴子已经被作者于2007-9-5 10:57:39编辑过]


有实力才会有魅力 实力来自坚持不懈的努力
2007-09-05 09:35
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
收藏
得分:0 

新版,用于验证TextBox中只能输入带两位小数的数字

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == '\b') || (e.KeyChar == '.'))
{
e.Handled = false; // 允许输入
if ((e.KeyChar == '.') && (textBox1.Text.IndexOf('.') > -1))
{
e.Handled = true;
int pos = textBox1.SelectionStart;
if ((pos < textBox1.Text.Length - 1) && (textBox1.Text.Substring(pos, 1) == "."))
{
textBox1.SelectionStart = ++pos;
}
}
}
else
{
e.Handled = true; // 不允许输入
}
}

private void textBox1_Validating(object sender, CancelEventArgs e)
{
errorProvider.SetError(textBox1, "");
double dValue;
if (!double.TryParse(textBox1.Text,out dValue))
{
errorProvider.SetError(textBox1, string.Format("请输入带{0}位小数的有效数字", DecimalLength.ToString()));
e.Cancel = true;
}
}

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
int pos = textBox1.SelectionStart;
string strValue = textBox1.Text.Trim();
int pointPos = strValue.IndexOf('.');
if (pointPos < 0)
{
strValue += ".00";
}
if (pointPos == 0)
{
strValue = "0" + strValue;
textBox1.SelectionStart = ++pos;
}
// 自动使其保留两位小数
string[] strs = strValue.Split(new char[] { '.' });
strs[0] = strs[0].Length > 0 ? strs[0] : "0";

while (strs[0].Substring(0, 1) == "0")
{
if (strs[0].Length > 1)
{
strs[0] = strs[0].Substring(1, strs[0].Length - 1);
if (pos > 0)
{
--pos;
}
}
else
{
break;
}
}

if (strs[1].Length > 2)
{
strs[1] = strs[1].Substring(0, 2);
}
else if (strs[1].Length < 2)
{
strs[1] = strs[1].PadRight(2, '0');
}
textBox1.Text = strs[0] + "." + strs[1];
textBox1.SelectionStart = pos;
}

[此贴子已经被作者于2007-9-20 11:43:20编辑过]


帮助那些真正需要帮助的人,是对帮助你的人最好的回报!
2007-09-20 09:59
快速回复:[原创]关于用TextBox来验证数据的有效性(只允许输入带两位小数的数字)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018973 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved