整数文本框不接受空值 [英] integer textbox not accept null value

查看:84
本文介绍了整数文本框不接受空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (txt_CheqNo.Text == "")
                {
                    objdal.Cheq_No =Convert.ToInt16("");
                }
                else
                {
                    objdal.Cheq_No = Convert.ToInt16(txt_CheqNo.Text);
                }



i有文本框存储数值。

但是当文本框留空时,会产生错误。

错误:输入字符串的格式不正确。



i不得不把requierfi​​eld填入textbox.i有很多情况下要把它留空。我必须保持空白。不要'0' '或其他任何进入它的东西。



如何解决这个问题?


i have textbox wich store numeric value.
but when textbox i leave empty,it creates error.
error:Input string was not in a correct format.

i have to not put requierfield to fill textbox.i have the situation in many case to leave it empty.i have to keep it complatly blank.Not ''0'' or anything else to enter in it.

how to solve this?


推荐答案

简单:不要尝试进行转换:

Simple: don''t try to do a convert:
if (txt_CheqNo.Text == "")
    {
    objdal.Cheq_No = 0;
    }
else
    {
    objdal.Cheq_No = Convert.ToInt16(txt_CheqNo.Text);
    }

但更好的方法是:

But a better way would be:

objdal.Cheq_No = 0;
if (!string.IsNullOrWhiteSpace(txt_CheqNo.Text))
    {
    objdal.Cheq_No = Convert.ToInt16(txt_CheqNo.Text);
    }


访问此链接可能对您有所帮助。

这也会捕获其他非数字输入



http://social.msdn.microsoft.com/forums/en-US/winforms/thread/84990ad2-5046-472b-b103-f862bfcd5dbc/ [ ^ ]
visit this link it might help you.
this also traps other non numeric inputs

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/84990ad2-5046-472b-b103-f862bfcd5dbc/[^]


最好的方法是在编写的代码下面,它不会给出解析器错误。



best way is below written code which will not give parser error.

Int16 _cheq_No;
Int16.TryParse(txt_CheqNo.Text, out _cheq_No);
objdal.Cheq_No = _cheq_No;


这篇关于整数文本框不接受空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆