I’m making a program that will be like a search engine, similar to a map program. There will be a text box for the user to search for a location. The thing I don’t know is that the user has 4 different combinations that they may enter. For example: N229 OR n229 OR N 229 OR n 229. I want the program to respond the same to all 4 possible combinations to show the same thing. Is it possible in any way to make this real?

You can simplify the process by:

Convert the text to all lower case:

Dim sConversionString as String = TextBox1.Text.ToLower()

Then do something like the following:

If sConversionString.Contains("n229") Or sConversionString.Contains("n 229") Then
    'Do Work
End If

Edited 4 Years Ago by Begginnerdev

Personally, I prefer to prevent the user from having such entry options. It makes the subsequent coding much easier. It is easy enough to write custom entry controls that inherit from those supplied in .Net.

I have assumed that you want to be able to enter N=North, S=South, E=East, W=West for the first character position and that you only want to allow up to 3 digits to follow that.

To accoplish this, I start off with a MaskedTextBox with the proper mask. I override the ONKeyPress to limit the first letter entered. The ONKeyDown override prevents the user from entering spaces. WndProc handles the user pasting invalid characters.

Just copy this code to your project and rebuild it. If your have Visual Studio set up to automatically add UserControls to the toolbox, it should show up in the toolbox.

Public Class CoordinateTB
   Inherits MaskedTextBox
   Public Sub New()
      Me.PromptChar = " "c 'I hate that default underscore
      Me.Mask = ">L0##" ' 1st char is letter converted to uppercase
                        ' followed by 3 digits, last 2 are optional
   End Sub

   Private Const AcceptableChars As String = "NEWSnews0123456789"
   ' This is to further filter the allowed characters
   ' I assumed that you want to be able to enter N=North, S=South, E=East, W=West
   ' for the 1st character.  AcceptableChars has both the Upper and Lower case 
   ' representations of the letters.  The Mask will convert it to Uppercase.
   Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
      If Acceptablechars.Contains(e.KeyChar) Then
         MyBase.OnKeyPress(e)
      Else
         e.Handled = True
      End If
   End Sub

   ' I do not like way the MaskedTextBox handles clicking in it.
   ' This routine will position the cursor at the entered character that
   ' matches the Mouseposition or at the end of the entered text
   Protected Overrides Sub OnMouseClick(ByVal e As System.Windows.Forms.MouseEventArgs)
      MyBase.OnMouseClick(e)
      Dim pos As Int32 = GetCharIndexFromPosition(PointToClient(MousePosition))
      Do While pos <> 0
         If pos > Text.Length Then
            pos -= 1
         Else
            Exit Do
         End If
      Loop
      SelectionStart = pos
   End Sub

   ' This prevents the user from using the Right Arrow to skip a character entry
   Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
      If e.KeyCode = Keys.Right Then
         If Text.Length <= SelectionStart Then
            e.Handled = True : Exit Sub
         End If
      End If
      MyBase.OnKeyDown(e)
   End Sub

   Private Const WM_PASTE As Int32 = &H302
   Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

      ' Make sure only valid characters are pasted.
      ' The mask will do further filtering
      If m.Msg = WM_PASTE AndAlso Clipboard.ContainsText Then
         Dim pasted As String = Clipboard.GetText()
         Dim stripped As New System.Text.StringBuilder(pasted.Length)
         For Each c As Char In pasted.ToCharArray
            If AcceptableChars.Contains(c) Then
               stripped.Append(c)
            End If
         Next c
         If stripped.Length = 0 Then Exit Sub 'No acceptable chars

         If stripped.ToString <> pasted Then 'only modify clibboard if different
            Clipboard.SetText(stripped.ToString)
         End If
      End If

      MyBase.WndProc(m)
   End Sub
End Class
This article has been dead for over six months. Start a new discussion instead.