【发布时间】:2015-03-29 09:51:18
【问题描述】:
有一个与此类似的问题,但它适用于 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"。
这段代码有什么问题?
【问题讨论】:
-
将
checkEmpty更改为返回 True 或 False 的函数并对其进行评估 -
您是否按照应有的方式在每个模块的开头声明了
Option Explicit?