【问题标题】:in vb.net is it possible to use a validator to limit the words allowed to be entered在 vb.net 中是否可以使用验证器来限制允许输入的单词
【发布时间】:2015-10-24 11:30:29
【问题描述】:

在 vb.net 中是否可以使用验证器来限制允许输入的单词。对于称为 housetype 的文本框,只能输入“house”“flat”“semi”“duplex”的单词

【问题讨论】:

  • 那你为什么需要文本框?然后去下拉菜单。

标签: asp.net vb.net validation


【解决方案1】:

你有很多可能性。一种选择是创建一个单词列表以允许并使用List.Any extension method,如下所示:

dim words as new List(of string) From { "house", "flat", "semi", "duplex" }
dim word = housetype.Text
Console.WriteLine(words.Any (function(w) w = word))

这样做的好处是,如果出现新词(或者如果从配置文件中读取列表等),它很容易扩展。

功能可以打包在扩展方法中,以使代码更具可读性,例如if (texBoxControl.ContainsOnlyValidWords()) then ...

【讨论】:

    【解决方案2】:

    可能比 SkorrloreGaming-Productions 的答案更好...

     Public Function CheckForAllowedWords() As String
            Dim ok_word As String
            Dim AllowedWords As String() = "house flat semi duplex".Split(" ")
            For Each item As String In AllowedWords
                If housetype.Text = item Then
                    ok_word = item
                    Exit For
                Else
                    Continue For
                End If
            Next
            Return ok_word
        End Function
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            CheckForAllowedWords()
            If CheckForAllowedWords() <> Nothing Then
                MsgBox("Accepted")
            Else
                MsgBox("Not Accepted")
            End If
        End Sub
    

    【讨论】:

    • .contains - 那么你就不需要处理数组了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 2013-01-09
    • 2011-08-31
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    相关资源
    最近更新 更多