论坛首页 编程语言技术论坛

NET判断输入是否为double类型

浏览 2901 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-04-03   最后修改:2008-12-25
第一种:
很常见一个字符去判断是否為數字。
第二種:
用正則表邊式
using System.Text.RegularExpressions;
Regex reg=new Regex(@"^\d+(\.\d+)?$",RegexOptions.None);
// Match the double data
if(!reg.IsMatch(this.txtRefWeight.Text) || !reg.IsMatch(this.txtWeiRange.Text))
{
labelStatus.Text="重量格式錯誤!";
labelStatus.ForeColor=Color.Red;
this.btnupload.Enabled=false;
return;
}
第三種:
是最笨也最慢的方法就是用铺抓異常
try{
this.Weight=Convert.ToDouble(this.txtRefWeight.Text);//Set the Weight
this.Weight_range=Convert.ToDouble(this.txtWeiRange.Text);
}catch
{
labelStatus.Text="重量格式錯誤!";
labelStatus.ForeColor=Color.Red;
this.btnupload.Enabled=false;
return;
}
   发表时间:2008-07-28  
受教了!谢谢楼主
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics