本文介绍了验证字符串用户条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我试图让用户只能输入a、b、c 或 d".如果他们没有输入这四个字母之一,我宁愿让它出错,而不是用户只能输入这些字母.我只能找到对数字数据执行类似操作的资源(使用 try catch).任何网站或提示都会很棒.

My issue is that I am attempting to make the user only able to enter "a, b, c or d". I would rather have it error out if they did not enter one of those four letters than the user only being able to input those letters. I have only been able to find resources that do something similar to this with numeric data (using try catch). Any sites or hints would be great.

                If String.Compare(TextBox2.Text, "a", True) = 0 AndAlso String.Compare(TextBox21.Text, "a", True) = 0 Then
                'MessageBox.Show("A")
                totCorrect = totCorrect + corAns
            ElseIf String.Compare(TextBox2.Text, "b", True) = 0 AndAlso String.Compare(TextBox21.Text, "b", True) = 0 Then
                'MessageBox.Show("B")
                totCorrect = totCorrect + corAns
            ElseIf String.Compare(TextBox2.Text, "c", True) = 0 AndAlso String.Compare(TextBox21.Text, "c", True) = 0 Then
                'MessageBox.Show("C")
                totCorrect = totCorrect + corAns
            ElseIf String.Compare(TextBox2.Text, "d", True) = 0 AndAlso String.Compare(TextBox21.Text, "d", True) = 0 Then
                'MessageBox.Show("D")
                totCorrect = totCorrect + corAns
            Else
                totWrong = totWrong + wrgAns
                Label13.Visible = True
            End If

推荐答案

这应该可以解决问题

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress

    Dim allowableChar As New List(Of Char) From {"a"c, "b"c, "c"c, "d"c}

    Call allowableChar.AddRange(allowableChar.Select(Function(c) Convert.ToChar(c.ToString().ToUpper())).ToList())

    If Not (allowableChar.Contains(e.KeyChar) OrElse e.KeyChar = Convert.ToChar(Keys.Delete) OrElse e.KeyChar = Convert.ToChar(Keys.Back)) Then
        e.Handled = True
    End If

End Sub

Call allowableChar.AddRange(...) 也将大写字符添加到列表中.就像现在一样,每次执行该方法时都会生成一个新列表,这有点浪费……如果您使用这段代码,我建议您将允许的字符列表设为类级变量并仅填充它一旦在表单的构造函数中.

The Call allowableChar.AddRange(...) adds the upper case characters to the list as well. As it is now, this generates a new list every time the method executes, which is a bit wasteful... if you use this piece of code I would suggest that you the list of allowable chars a class-level variable and fill it only once in the constructor of your form.

要让每种类型只允许一个字符,请使用:

To allow only once character of each type, use this:

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress

    Dim allowableChar As New List(Of Char) From {"a"c, "b"c, "c"c, "d"c}

    Call allowableChar.AddRange(allowableChar.Select(Function(c) Convert.ToChar(c.ToString().ToUpper())).ToList())

    If Not (allowableChar.Contains(e.KeyChar) OrElse e.KeyChar = Convert.ToChar(Keys.Delete) OrElse e.KeyChar = Convert.ToChar(Keys.Back)) Then
        e.Handled = True
    Else
        If Me.TextBox1.Text.Count(Function(c) c = e.KeyChar) >= 1 Then
            e.Handled = True
        End If
    End If

End Sub

这篇关于验证字符串用户条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:00