如果没有在文本框中输入任何内容,如何将默认值设置为零? [英] how can I set a default value to zero if nothing is entered into the textbox?
本文介绍了如果没有在文本框中输入任何内容,如何将默认值设置为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果没有在文本框中输入任何内容,如何将默认值设置为零?
how can I set a default value to zero if nothing is entered into the textbox?
推荐答案
请参阅:Int32.TryParse [ ^ ]方法。
示例:
See: Int32.TryParse[^] method.
Example:
int result = 0;
if (!Int32.TryParse(TextBox1.Text, result))
//conversion failed, default value of result is equal to 0 (zero)
else
//conversion OK, result is eqaul to the value entered into TextBox ;)
根据帖子的评论,OP编写了代码
As per the comments to the post, OP had written code
If TextBox1.Text = "" Then
TextBox1.Text = 0
End If
If TextBox2.Text = "" Then
TextBox2.Text = 0
End If
即他们将数字0分配给字符串。
将代码更改为
I.e. they were assigning a numeric 0 to a string.
Changing the code to
If TextBox1.Text = "" Then
TextBox1.Text = "0"
End If
If TextBox2.Text = "" Then
TextBox2.Text = "0"
End If
解决了问题
这篇关于如果没有在文本框中输入任何内容,如何将默认值设置为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文