使用vb.net检查空字段 [英] Check empty fields using vb.net

查看:69
本文介绍了使用vb.net检查空字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有问题。当我单击按钮执行一行代码时,mssgbox会继续重复。用户忘记输入或选择值(combBox,txtBox..etc)的组框内的项目总数也是mssgbox弹出的次数。请帮忙! (新手)



Theres something wrong with my codes. When i click the button to perform a lines of code, mssgbox keeps on repeating. Total items inside a groupbox that a user forgot to enter or select a value (combBox,txtBox..etc) is also the number times the mssgbox will pops out. Please help! (newbie)

For Each item As Control In GroupBox1.Controls

            If item.Text = Nothing Then
                ''inform the user that all fields should have a value
                MsgBox("Please fill up all fields")
            Else
                If MsgBox("Do you want to save this record?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
                    ''save the record
                End If

            End If

        Next

推荐答案

亲爱的开发者,



请尝试以下代码...



Dear Developer,

Try the following code...

nCount = 0
For Each item As Control In GroupBox1.Controls

    If item.Text = Nothing Then
      nCount = nCount + 1
    End If

Next

If nCount<>0 Then
    ''' inform the user that all fields should have a value
    MsgBox("Please fill up all fields")
Else
    If MsgBox("Do you want to save this record?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
        '''save the record
    End If

End If


我会建议你这样:



I would suggest you to go this way:

Public Sub SaveRecord()
    For Each item As Control In GroupBox1.Controls
        If String.IsNullOrEmpty(item.Text) Then
            ''inform the user that all fields should have a value
            MsgBox("Please fill up all fields............." & item.Name)
            item.Focus()
            Exit Sub
        End If
    Next
    If MsgBox("Do you want to save this record?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
        ''save the record
    End If
End Sub





使用此解决方案的优点是:



1)您可以指出用户的文本框缺少或未填充。

2)这样您就不必迭代到所有文本框控件并在最后做出决定。

3)无需额外的变量。



希望它对你有用,因为你是新手。



The advantage of using this solution is:

1) You can point out the textbox that the user is missing or left unfilled.
2) This way you don't have to iterate to all textbox controls and make decision at the end.
3) No need of additional variable.

Hope it is useful for you since you are newbie.


我推荐这个解决方案:





I recommend this solution :


For Each item As Control In GroupBox1.Controls

           If item.Text = Nothing Then
               ''inform the user that all fields should have a value
               MsgBox("Please fill up all fields")
               Exit Sub ' or return if it's a function
           End If
      
       Next

If MsgBox("Do you want to save this record?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
                   ''save the record
End If





因为,为什么要寻找其他如果第一个不正确则控制?



Because, why looking to other controls if the 1st is not correct ?


这篇关于使用vb.net检查空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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