我是初学英语和vb.net 但是我想问一下,当我点击按钮进行数学计算时,如果文本框为空,我试图发出警告 这是我的代码
Private Sub hitung_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles hitung.Click
Dim z, iterasi, n1, n2, n3 As Single
Dim fx0, fx1, fx2, x2, x0, x1 As Decimal
If a.Text = Nothing Or
b.Text = Nothing Or
g.Text = Nothing Or
h.Text = Nothing Or
i.Text = Nothing Or
j.Text = Nothing Then
MessageBox.Show("TextBox is empty", "No entry",
MessageBoxButtons.OK, MessageBoxIcon.Error)
With a
.Focus()
.SelectAll()
End With
End If
x0 = (a.Text)
x1 = (b.Text)
n1 = (g.Text)
n2 = (h.Text)
n3 = (i.Text)
iterasi = (j.Text)
但仍然有一个错误,上面写着“从字符串转换”“到'Decimal'类型无效。”如果我将文本框留空并按下按钮
所以我将x0更改为此
x0 = Decimal.Parse(a.Text)
但错误显示“输入字符串的格式不正确。”
如何解决错误?
答案 0 :(得分:0)
Nothing
和一个空字符串是不同的东西;您可以使用string.IsNullOrEmpty(a.Text)
(等)代替a.Text = Nothing
以获得更具弹性的方法
答案 1 :(得分:0)
以下是代码:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If a.Text = "" Then
MessageBox.Show("Empty TextBox", "No entry")
End If
End Sub
End Class