遍历空文本框,直到文本框有数据
Loop through empty textbox until the textbox has data
我确定对此有一个简单的解决方案,但它逃脱了我。我有一个表格,上面有三个文本框。在主要代码 运行 之前,我想确保每个文本框中都有数据。我已经将 "hasData" 初始化为决定代码是否可以继续运行的变量。我在 Do While 循环中评估 hasData,但代码发现存在没有数据的文本框,并将 hasData 变量设置为 "False"。但后来我陷入了一个连续循环,消息框永远不会消失,让您可以在空文本框中输入文本。
感谢您的帮助。
Dim hasData As String
hasData = "False"
Do While hasData = "False"
If txtTechManName.Text.Trim = "" Or txtDirectory.Text.Trim = "" Or txtBxUnique.Text.Trim = "" Then
btnExecute.Enabled = False
hasData = "False"
MsgBox(" Please fill all text boxes on form ")
' this resulted in an endless loop of the msgBox. It didn't let me add text to the empty fields
Else
btnExecute.Enabled = True
hasData = "True"
End If
Loop
If (hasData = "True") Then
searchDir = txtDirectory.Text
Prefix = txtBxUnique.Text
Dim manualName = txtTechManName.Text
我将我的评估条件移至单击事件时的执行按钮。我在那里 运行 一个 if 语句,当 hasData 为真时 运行 函数和其余代码。
Private Sub btnExecute_Click(sender As Object, e As EventArgs) Handles btnExecute.Click
Dim hasData As Boolean = False
If txtTechManName.Text.Trim = "" Or txtDirectory.Text.Trim = "" Or txtBxUnique.Text.Trim = "" Then
hasData = False
MsgBox(" Please fill all text boxes on form ")
If txtTechManName.Text.Trim = "" Then
txtTechManName.Focus()
ElseIf txtDirectory.Text.Trim = "" Then
txtDirectory.Focus()
ElseIf txtBxUnique.Text.Trim = "" Then
txtBxUnique.Focus()
End If
Else
hasData = True
runProgram()
End If
End Sub
您是否看到必须将 TextBox 列出两次?两种情况下的控件列表是相同的,为什么要重复工作?
If txtTechManName.Text.Trim = "" Or txtDirectory.Text.Trim = "" Or txtBxUnique.Text.Trim = "" Then
' ...
If txtTechManName.Text.Trim = "" Then
txtTechManName.Focus()
' ...
如果您想将文本框添加到逻辑中,则必须记住在两个地方更新代码。你甚至可能有两个以上的地方......
- 在某种 IEnumerable 集合中只列出一次 TextBox。
- 使用 LINQ 帮助您处理一些逻辑。
- 用方法封装逻辑,让你的程序流程更流畅,更容易理解。
' store the TextBoxes in a collection
Private ReadOnly textBoxes As New List(Of TextBox)()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' add the TextBoxes which should be filled to the collection
textBoxes.AddRange({txtTechManName, txtDirectory, txtBxUnique})
End Sub
Private Function hasData() As Boolean
' ![any TextBox is invalid] == all TextBoxes are valid
Return Not textBoxes.Any(AddressOf textBoxIsInvalid)
End Function
Private Function lastInvalidTextBox() As TextBox
' last TextBox with invalid entry, or default (null)
Return textBoxes.LastOrDefault(AddressOf textBoxIsInvalid)
End Function
' returns true when passed TextBox is invalid
Private Function textBoxIsInvalid(tb As TextBox) As Boolean
' define invalid here, in once place only
Return String.IsNullOrWhiteSpace(tb.Text)
End Function
Private Sub btnExecute_Click(sender As Object, e As EventArgs) Handles btnExecute.Click
' simply checks the hasData function which hides the logic
If hasData() Then
runProgram()
Else
MessageBox.Show("Please fill all text boxes on form")
' focus the last invalid TextBox, effectively the same as your original logic
' if it is null (no invalid TextBox, see lastInvalidTextBox()), isn't called
lastInvalidTextBox()?.Focus()
End If
End Sub
我确定对此有一个简单的解决方案,但它逃脱了我。我有一个表格,上面有三个文本框。在主要代码 运行 之前,我想确保每个文本框中都有数据。我已经将 "hasData" 初始化为决定代码是否可以继续运行的变量。我在 Do While 循环中评估 hasData,但代码发现存在没有数据的文本框,并将 hasData 变量设置为 "False"。但后来我陷入了一个连续循环,消息框永远不会消失,让您可以在空文本框中输入文本。
感谢您的帮助。
Dim hasData As String
hasData = "False"
Do While hasData = "False"
If txtTechManName.Text.Trim = "" Or txtDirectory.Text.Trim = "" Or txtBxUnique.Text.Trim = "" Then
btnExecute.Enabled = False
hasData = "False"
MsgBox(" Please fill all text boxes on form ")
' this resulted in an endless loop of the msgBox. It didn't let me add text to the empty fields
Else
btnExecute.Enabled = True
hasData = "True"
End If
Loop
If (hasData = "True") Then
searchDir = txtDirectory.Text
Prefix = txtBxUnique.Text
Dim manualName = txtTechManName.Text
我将我的评估条件移至单击事件时的执行按钮。我在那里 运行 一个 if 语句,当 hasData 为真时 运行 函数和其余代码。
Private Sub btnExecute_Click(sender As Object, e As EventArgs) Handles btnExecute.Click
Dim hasData As Boolean = False
If txtTechManName.Text.Trim = "" Or txtDirectory.Text.Trim = "" Or txtBxUnique.Text.Trim = "" Then
hasData = False
MsgBox(" Please fill all text boxes on form ")
If txtTechManName.Text.Trim = "" Then
txtTechManName.Focus()
ElseIf txtDirectory.Text.Trim = "" Then
txtDirectory.Focus()
ElseIf txtBxUnique.Text.Trim = "" Then
txtBxUnique.Focus()
End If
Else
hasData = True
runProgram()
End If
End Sub
您是否看到必须将 TextBox 列出两次?两种情况下的控件列表是相同的,为什么要重复工作?
If txtTechManName.Text.Trim = "" Or txtDirectory.Text.Trim = "" Or txtBxUnique.Text.Trim = "" Then
' ...
If txtTechManName.Text.Trim = "" Then
txtTechManName.Focus()
' ...
如果您想将文本框添加到逻辑中,则必须记住在两个地方更新代码。你甚至可能有两个以上的地方......
- 在某种 IEnumerable 集合中只列出一次 TextBox。
- 使用 LINQ 帮助您处理一些逻辑。
- 用方法封装逻辑,让你的程序流程更流畅,更容易理解。
' store the TextBoxes in a collection
Private ReadOnly textBoxes As New List(Of TextBox)()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' add the TextBoxes which should be filled to the collection
textBoxes.AddRange({txtTechManName, txtDirectory, txtBxUnique})
End Sub
Private Function hasData() As Boolean
' ![any TextBox is invalid] == all TextBoxes are valid
Return Not textBoxes.Any(AddressOf textBoxIsInvalid)
End Function
Private Function lastInvalidTextBox() As TextBox
' last TextBox with invalid entry, or default (null)
Return textBoxes.LastOrDefault(AddressOf textBoxIsInvalid)
End Function
' returns true when passed TextBox is invalid
Private Function textBoxIsInvalid(tb As TextBox) As Boolean
' define invalid here, in once place only
Return String.IsNullOrWhiteSpace(tb.Text)
End Function
Private Sub btnExecute_Click(sender As Object, e As EventArgs) Handles btnExecute.Click
' simply checks the hasData function which hides the logic
If hasData() Then
runProgram()
Else
MessageBox.Show("Please fill all text boxes on form")
' focus the last invalid TextBox, effectively the same as your original logic
' if it is null (no invalid TextBox, see lastInvalidTextBox()), isn't called
lastInvalidTextBox()?.Focus()
End If
End Sub