Allowing/Restricting Certain Characters/Letters/Numbers in a Textbox Control

 
There are times where you may want to limit the characters that a user can put in a textbox control in Visual Basic.NET and Visual Basic 2005 or VB 2008. For example, if the textbox is used for math based calculations, you may only want the user to have the ability to put in numbers or only numbers with a single decimal point. Fortunately it is very easy to do.

In case you might care, some of these codes are taken from my csTextboxX .NET Control located at my www.vbcodesource.com website. All of these codes should be added to the Textbox_Keypress event and work with ALL versions of Visual Basic.NET (Including 2005 and 2008).

Note:

This article goes over how to Allow Only Numbers in a Textbox control with a Single Decimal Point (.).  I might add other articles in the future with code snippets on how to restrict other chars(characters) and user input. I do have code snippets on this site on restricting/allowing specific characters as well if you simply/only want the source code and nothing else.

 

Remember to put these codes in the Textbox controls _KeyPress event.

 

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
    Handles TextBox1.KeyPress
'
'There are a couple scenerios this code is looking for. One is checking for the Decimal period "."
'and whether it exists in the textbox already. The other is seeing if the keypress was a Number and
'Control based key.
'
  If e.KeyChar = "." Then
'
'If a value higher than -1 is returned, it means there IS a existing decimal point’
    If TextBox1.Text.IndexOf(".") > -1 Then
'
'This says that I already dealt with the _KeyPress event so do not do anything else with this event.

        e.Handled = True

    End If
'
'Remember you want numbers to get through and the Control keys like “Backspace”
  ElseIf Char.IsNumber(e.KeyChar) = False AndAlso Char.IsControl(e.KeyChar) = False Then
'
'This says that I already dealt with the _KeyPress event so do not do anything else with this event.

    e.Handled = True

  End If

End Sub

Now if you run those codes, the textbox “should” only allow Number based characters with a Single decimal point char.

While the article was made to be very basic, hopefully it will give you a simple start on restricting textbox input to certain/specific characters. The basics from the article should help with that. Anyways, Have Fun!

Jason



How to Search/Find Text in a Textbox control using VB.NET and VB 2005 and Higher

 

Most anykind of word processing application from the basic Windows Notepad to the more advanced OpenOffice Writer includes a feature to search its text. There are many other reasons to implement a textbox search feature and hopefuly this article will show you how. The normal textbox control in .NET does not have any find/search capabilities that I could find. So I ended up having to create my own searching code for a control I made along time ago and decided it could be useful for someone if I posted it.

Visual Basic.NET 2002/2003/2005/2008/2010

Since DotNET doesn’t have built-in support for searching/finding text, you will need to make your own code. This is the snippet I originally made for a more advanced textbox control called: ‘csTextBoxX’ that I made for Visual Basic.NET 02/03. There are many other features available as well if you want to take a look at it here and with a example here.

———————————————-

Below is the Function I made…

'
'I’m sure this Function can be more refined if you wanted to do it.
'
Public Overloads Function SearchText(ByVal textToFind As String, Optional ByVal startPosition As Integer = 0, Optional ByVal endPosition As Integer = 0, Optional ByVal highlightText As Boolean = True, Optional ByVal matchCase As Boolean = False) As Integer
'
'Contains the return value of the search. IT it returns -1, then a match was not found.
Dim i As Integer

If endPosition < 1 Then

  If Not matchCase Then

    textToFind = textToFind.ToLower

  Dim temp As String = Textbox.Text.ToLower

    i = temp.IndexOf(textToFind, startPosition, Me.Text.Length)

  Else

    i = Textbox.Text.IndexOf(textToFind, startPosition, Me.Text.Length)

  End If

Else

  If matchCase = False Then

      textToFind = textToFind.ToLower

      Dim temp As String = Textbox.Text.ToLower

      i = temp.IndexOf(textToFind, startPosition, endPosition)

  Else

      i = Textbox.Text.IndexOf(textToFind, startPosition, endPosition)

  End If

End If

If i > -1 Then

  If highlightText Then

    Textbox.Focus()

    Textbox.SelectionStart = i

    Textbox.SelectionLength = textToFind.Length

  End If

End If
'
'Returns the position the text was found at, otherwise it will report -1, which means that the search string was not found.
  Return i

End Function

——————————————————————-

Unfortunately my Function may not appear straightforward or easy to understand. The page doesn’t seem to display the code very neatly either but you should be able to copy and paste with no problems. You may want to go through it and refine/optimize it where you can. But one things for sure, this function DOES work and will search the target Textbox for the specified text. IT supports Match Casing, No Match Casing ,and you can search at a specific start and/or ending position within the controls text. There is also a option to highlight the text that was found or no highlighting. Check out the example below to see it in use…

——————————————————————–

 

'
'Will contain the position where the text was found.  
  Dim textFound As Integer
'
'Find the first occurance of the text “box” at the beginning of the textbox through to the end of the textbox. Selected True to Highlight the text that was found and disabled searching text for the same casing.  
  textFound = SearchText(“box”, 0, Textbox.TextLength, True, False)
'
'Display a message with the starting position of the text if it was found. If a -1 is returned then it means it did Not find the specified text.   
  MsgBox(textFound.ToString)    

 

—————————————————

The example above starts finding/searching at the beginning of the text and will go to the end of the text to find a match. IT was set to highlight the text if found and not to worry about matching the text casing.

Thats all I guess. As a side note, I was almost sure that Visual Basic 2005 added a find/search feature in the textbox control but I couldn’t find it. So you can use this function for Visual Basic.NET 2002/2003 and Visual Basic 2005/2008. I only tested it with Visual Basic.NET 2003 but there should not being any major changes required for the newers VBs. Have fun :)

Jason



Allow only Numbers in a Textbox Control using VB.NET and Higher

 

This will only allow numbers to be entered into a textbox control. You can allow apply the codes to a combobox controls edit box. This works with VB.NET and VB 2005, VB 2008, and Visual Basic 2010.

        'Allow only numbers when someone types in a textbox control
        '
        'Add a textbox to the form and name is: txt . Then put the following 
        '
        'Code needs to be in the Textbox_KeyPress event of the textbox control.
        If e.KeyChar.IsNumber(e.KeyChar) = False Then

            e.Handled = True

        End If