使用VB.NET进行验证(存在检查)问题 [英] Issue with validation (presence check) using VB.NET

查看:87
本文介绍了使用VB.NET进行验证(存在检查)问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一共使用了7个文本框,其中6个被命名为txtBox1,txtBox2,txtBox3,txtBox4,txtBox5,txtBox6,第七个被命名为txtBoxSearch,我尝试使用下面的代码进行存在检查,问题是我只想对前6个文本框进行存在检查,下面的代码也包括txtBoxSearch。我不知道如何处理这个问题,有没有人有任何想法?谢谢。



我尝试了什么:



 Dim PresenceCheck As Boolean 

Dim emptyTextBoxes =
来自txt In Me.Controls.OfType(Of TextBox) ()
其中txt.Text.Length = 0
选择txt.Name()
如果为emptyTextBoxes.Any那么

结束如果
PresenceCheck = False
'这段代码查找表单中的所有文本框,如果其中任何文本框为空白,则其名称将包含在消息框中,提醒用户将其填入'


MessageBox.Show(String.Format(请填写以下文本框:{0},
String.Join(,,emptyTextBoxes)))
MessageBox.Show(客户没有已成功添加)

解决方案

如果您想获取TextBox的列表,其名称是 txtBoxN (其中 N 是一个数字)并且TextBox不为空,然后试试这个:



< pre lang =VB.NET> Dim 模式作为 字符串 = ^ txtBox \d



Dim r As Regex = 正则表达式(模式)

Dim notEmptyTextBoxes = .Controls .OfType( Of TextBox)_
.Where( Function (x)x.Text。 Trim.Length> 0 r.Match(x.Name).Success)_
.ToList()

对于 每个 tb As TextBox In notEmptyTextBoxes
MsgBox( String .Format( {0} {1},tb.Name,tb.Text))
下一步





如果您想通知用户要填写的文本框,试试这个:

  Dim  msg =  String .Join( ;.Controls.OfType( of  TextBox)_ 
.Where(功能(x)x.Text.Trim.Length = 0 r .Match(x.Name).Success)_
选择功能(x) x.Name)_
.ToList())

MsgBox( 请,填写以下文本框:& vbCr& _
msg,MsgBoxStyle.Information, 信息


I am using a total of 7 text boxes, six of them are named txtBox1,txtBox2,txtBox3,txtBox4,txtBox5,txtBox6, the seventh is named txtBoxSearch,I have tried using the code below to carry out a presence check, the problem is that I only want to carry out the presence check on the first 6 text boxes, the code below includes the txtBoxSearch as well. I don't know how to deal with this, does anyone have any ideas?Thanks.

What I have tried:

      Dim PresenceCheck As Boolean

 Dim emptyTextBoxes =
From txt In Me.Controls.OfType(Of TextBox)()
             Where txt.Text.Length = 0
         Select txt.Name()
        If emptyTextBoxes.Any Then

        End If
        PresenceCheck = False
        'this piece of code looks for all text boxes in the form, if any of them are blank their name would be included in the messagebox which reminds the user to fill them in'


        MessageBox.Show(String.Format("Please fill following textboxes: {0}",
                        String.Join(",", emptyTextBoxes)))
        MessageBox.Show("Customer has not been successfully added")

解决方案

If you would like to get a list of TextBoxes, which name is txtBoxN (where N is a number) and TextBox is not empty, then try this:

Dim pattern As String = "^txtBox\d


" Dim r As Regex = New Regex(pattern) Dim notEmptyTextBoxes = Me.Controls.OfType(Of TextBox) _ .Where(Function(x) x.Text.Trim.Length > 0 And r.Match(x.Name).Success) _ .ToList() For Each tb As TextBox In notEmptyTextBoxes MsgBox(String.Format("{0} {1}", tb.Name, tb.Text)) Next



If you would like to inform a user about textboxes to fill, try this:

Dim msg = String.Join("; ", Me.Controls.OfType(Of TextBox) _
                            .Where(Function(x) x.Text.Trim.Length = 0 And r.Match(x.Name).Success) _
                            .Select(Function(x) x.Name) _
                            .ToList())

MsgBox("Please, fill the following textboxes: " & vbCr & _
        msg, MsgBoxStyle.Information, "Information")


这篇关于使用VB.NET进行验证(存在检查)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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