首页 > 解决方案 > visual basic:用户在文本后输入一个 % 符号

问题描述

Private Sub btnBillSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBillSearch.Click

        If Me.txbBILL_NR.Text = "" Or Me.txbBILL_NR.TextLength >= 4 Then
            Try
                Search()

                If Me.bsBillGrid.Current IsNot Nothing Then
                    Me.dgvBill.Focus()
                End If
            Catch ex As Exception
                Utility.ExceptionManager.HandleException(ex)
            End Try
        Else
            MessageBox.Show("enter at least 4 numbers")
        End If
    End Sub

我的应用程序中有这种搜索按钮的方法。所以,我已经写了如果用户输入的最小字符数是 4 来选择条目的条件。

我需要对 % 符号再进行一次验证。我想通了,这有点 startWidth(),但我不明白我怎么能得到这些情况:

我考虑使用 startWith/endWith 或正则表达式。谁能给我一些建议在哪里看。

  1. 用户在 txbBILL_NR 后添加“%”符号 - 系统不会在后台添加额外的“%”,在列表中找到并反映具有相同部件号的 txbBILL_NR。

  2. 用户没有在 txbBILL_NR 之后添加“%”符号 - 系统在后台添加了一个额外的“%”,txbBILL_NR 具有相同的编号。找到并列出零件。“%”字符不需要用户表示;

3.用户在txbBILL_NR的开头或中间添加了“%”符号——系统不会在后台添加额外的“%”,找到并反映在列表中(无需更改);

  1. 只有当用户在 txbBILL_NR 字段中输入了至少 4 个字符(假设发票编号不能短于 4 个字符)时,才需要标记“%”系统。

  2. 如果用户输入了至少 4 个字符(无论是起始字符还是中间字符)并且在这些符号的开头或中间添加了“%”,则系统不会在发票的后台添加额外的“%” (s) 具有相同部分(符号)的部分被找到并反映在列表中。

标签: stringvb.netchar

解决方案


'Get raw user entry.
Dim searchText = txbBILL_NR.Text

'Get user entry without wildcards.
Dim cleanSearchText = searchText.Replace("%", String.Empty)

If cleanSearchText.Length = 0 OrElse cleanSearchText.Length >= 4 Then
    If searchText = cleanSearchText Then
        'There are no wildcards so append one.
        searchText &= "%"
    End If

    'Use searchText here.
End If

推荐阅读