vb6检查文本框是否为空

时间:2015-03-28 14:37:17

标签: textbox vb6

有一个类似的问题,但它适用于C#,Check if TextBox is empty and return MessageBox?

还有另一种解决方案可以检查文本框是否为空https://www.daniweb.com/software-development/visual-basic-4-5-6/threads/414651/checking-if-textbox-is-empty,但如果您要检查表单中的所有文本框,则此方法有效。如果它们是空的,我想检查表格中的一些文本框。

我已编写此代码以检查文本框是否为空

Private sub checkEmpty()
If text1.text = "" Or text2.text="" Then
blank = true
End If
End Sub

然后将此代码添加到我的命令按钮

Private Sub command1_Click()
checkEmpty
If blank = true Then
Msgbox "a text box is empty"
Else
Msgbox "Text box has text"
End If
End Sub

当我启动程序时,即使文本框中没有文本,它也会输出"Text box has text"

此代码有什么问题?

1 个答案:

答案 0 :(得分:4)

您需要将程序更改为返回值的函数(我同时更改名称以使其更清楚它的作用)。

Private Function AnyTextBoxEmpty() As Boolean
  AnyTextBoxEmpty = text1.Text = "" or text2.Text = ""
End Function

Private Sub command1_Click()
  If AnyTextBoxEmpty Then
    Msgbox "a text box is empty"
  Else
    Msgbox "Text box has text"
  End If
End Sub
相关问题