如果没有在文本框中输入任何内容,如何将默认值设置为零

时间:2015-03-10 13:27:05

标签: vb.net

如果没有在文本框中输入任何内容,如何将默认值设置为零?

If TextBox1.Text = "" Then
    TextBox1.Text = 0
End If
If TextBox2.Text = "" Then
    TextBox2.Text = 0
End If

2 个答案:

答案 0 :(得分:4)

您无法将.Text的{​​{1}}属性设置为值0,因为它是System.Windows.Forms.TextBox值类型。但是,您可以将其设置为String的字符串表示形式0

"0"

如果您要设置Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged Dim txtBox As TextBox = DirectCast(sender, TextBox) If String.IsNullOrEmpty(txtBox.Text) Then txtBox.Text = CStr(0) End Sub ,那么在尝试将Option Strict On属性设置为整数时会出错。

答案 1 :(得分:0)

不确定您的上下文或您尝试的内容,但您可以在Save / Capture / Next或任何按钮上尝试类似的内容。

If Trim(TextBox1.Text) = "" Then MyIntegerVariable = 0
相关问题