控制空文本框 [英] Control empty textboxes

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

问题描述

我想在panel6中控制一组文本框。如果文本框为空,请选择它,如果文本框包含五个字符并且也可见则选择下一个空文本框。



我尝试过:



I would like to control a collection of textboxes in panel6. If textbox is empty select it, if textbox contain five characters and also is visible select next empty textbox.

What I have tried:

Dim EmptyTextBoxFound As Boolean = False 'Boolean flag for empty textbox 
        Dim EmptyTextBoxName As String = ""
        For Each ctl As Control In Panel6.Controls

            If TypeOf ctl Is TextBox AndAlso ctl.Visible AndAlso ctl.Text.Length = 0 Then
                EmptyTextBoxName = ctl.Name
                EmptyTextBoxFound = True
                Exit For

                If EmptyTextBoxFound = True Then
                    ctl.Select()

                    '.. do whatever you have do
                End If
            End If
            If ctl.Text.Length > 5 Then
                ctl.Select()

            End If
        Next

推荐答案

您的代码不起作用的原因是因为您已经包含了在循环中执行某些操作的操作。如果找到一个空文本框,则发出退出,这样就不会出现
The reason your code does not work is because you have included the actions to do something within the loop. You issue an Exit For when you find an empty text box so you will never hit the line
If EmptyTextBoxFound = True Then

如果你调试了你的代码 - 如果你对调试没有信心那么这就是你的文章掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

尝试将代码更改为更像这样:

This would have been obvious if you had debugged your code - if you are not confident with debugging then this is the article for you Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Try changing your code to look more like this:

Dim EmptyTextBoxFound As Boolean = False 'Boolean flag for empty textbox 
Dim EmptyTextBoxName As String = ""
Dim ctl As Control
For Each ctl In Panel6.Controls

    If TypeOf ctl Is TextBox AndAlso ctl.Visible AndAlso ctl.Text.Length = 0 Then
        EmptyTextBoxName = ctl.Name
        EmptyTextBoxFound = True
        Exit For

    End If

Next

If EmptyTextBoxFound = True Then
    ctl.Select()

    '.. do whatever you have do
End If
        
If ctl.Text.Length > 5 Then
    ctl.Select()
End If

请注意我已搬家了将ctl声明到循环外部,以便在退出循环时仍然可以引用它

Note that I've moved the declaration of ctl to outside the loop so that it can still be referenced when you exit the loop


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

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