循环遍历空文本框,直到文本框有数据 [英] Loop through empty textbox until the textbox has data

查看:42
本文介绍了循环遍历空文本框,直到文本框有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信对此有一个简单的解决方案,但它让我无法理解.我有一个带有三个文本框的表单.在运行主代码之前,我想确保每个文本框中都有数据.我已经将hasData"初始化为决定代码是否可以继续运行的变量.我在 Do While 循环中评估 hasData 但代码发现有没有数据的文本框并将 hasData 变量设置为False".但后来我处于一个连续循环中,消息框永远不会消失,允许您在空文本框中输入文本.

I'm sure there's a simple solution on this, but it escapes me. I have a form with three text boxes on it. Before the main code is run I want to make sure each text box has data in it. I've initialized "hasData" to be the variable that will decide if the code can move on or not. I evaluate hasData in a Do While loop but the code discovers there are text boxes without data and set hasData variable to "False". But then I'm in a continuous loop, the message box never goes away to allow you to enter text into the empty text boxes.

感谢您的帮助.

    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 为真时,运行该函数和其余代码.

I moved my evaluating condition to the execute buttons on click event. There I run an if statement and when hasData is true run the function with the rest of the code.

    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

这篇关于循环遍历空文本框,直到文本框有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆