Tag Archives: textbox

Advanced Textbox Manipulation in Visual Basic and VB.NET | Part 2

This is Part Two of the Advanced Textbox Control Manipulation series. If you haven’t already you should check out Part 1 of this series. That article showed how to use the SendMessage API call to make a Textbox control Page Left, Page Right, Line Left, Line Right, Left Edge, and go to the Right Edge of the contents. This Part 2 post will show how to add the functionality that is outlined below…

Page UP

Page Down

Line UP

Line Down

Top Edge

Bottom Edge

Note: These codes are basically taken from an example I for VB.NET that shows lots and lots of various textbox based manipulating and functionality. Just go to http://www.vbcodesource.com under the Visual Basic.NET – Examples page.

Continue reading

Advanced Textbox Manipulation in Visual Basic and VB.NET | Part 1 of 2

This is the first post in a two part series on how to do add some extra textbox features not exposed in your normal VB or VB.NET textbox. For instance, Page Left/Right and Line Up/Down.

Note: These codes are basically taken from an example I made at my vbcodesource.com site for VB.NET that shows how to do lots and lots of various textbox based manipulating. Just go to http://www.vbcodesource.com/ under the Visual Basic.NET – Examples page.

So in Part #1 I will provide code to add six new abilities to a Textbox control…

  • Page Left
  • Page Right
  • Line Left
  • Line Right
  • Left Edge
  • Right Edge

To accomplish these features I will use the tried and true Windows API. Specifically the SendMessage Function.


First is the API function

Visual Basic 6.0


    'Used to Send Messages to the control.
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal winHandle As Long, _
        ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Visual Basic.NET, VB 2008 and Visual Basic 2010

    'Used to Send Messages to the control.
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal winHandle As Int32, _
        ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32

Now for some Constants…

Visual Basic 6.0


    Private Const EM_SCROLL = &HB5

    Private Const SB_PAGEDOWN = 3

    Private Const SB_PAGEUP = 2

    Private Const SB_LINEUP = 0

    Private Const SB_LINEDOWN = 1

    Private Const SB_BOTTOM = 7

    Private Const SB_TOP = 6

Visual Basic.NET through VB 2008, VB 2010, and newer


    Private Const EM_SCROLL As Int32 = &HB5

    Private Const SB_PAGEDOWN As Int32 = 3

    Private Const SB_PAGEUP As Int32 = 2

    Private Const SB_LINEUP As Int32 = 0

    Private Const SB_LINEDOWN As Int32 = 1

    Private Const SB_BOTTOM As Int32 = 7

    Private Const SB_TOP As Int32 = 6


All that’s really needed is the Textbox you want to manipulate and call Sendmessage with the right combination of constants to perform the intended function.

NOTE: If your using VB 6.0 then change one thing in the rest of the code below…

  • Instead of txtControl.Handle.ToInt32, make it txtControl.hWnd

That’s basically the only change needed.


Page Left / Page Right

        'Move the position pageleft.
        SendMessage txtControl.Handle.ToInt32, WM_HSCROLL, SB_PAGELEFT, 0

        'Move the position pageright.
        SendMessage txtControl.Handle.ToInt32, WM_HSCROLL, SB_PAGERIGHT, 0

Line Left / Line Right

        'Move the position left.
        SendMessage txtControl.Handle.ToInt32, WM_HSCROLL, SB_LINELEFT, 0

        'Move the position right.
        SendMessage txtControl.Handle.ToInt32, WM_HSCROLL, SB_LINERIGHT, 0

Left Edge / Right Edge

        'Move the position to the left edge.
        SendMessage txtControl.Handle.ToInt32, WM_HSCROLL, SB_LEFT, 0

        'Move the position to the right edge.
        SendMessage txtControl.Handle.ToInt32, WM_HSCROLL, SB_RIGHT, 0

As you can see these non-native features were simple to add and configure.

Part #2 will go over:

  1. Page Up
  2. Page Down
  3. Line Up
  4. Line Down
  5. Top Edge
  6. Bottom Edge

Well that’s basically it for this article, Have fun!

              Jason

Revised: 2014

How to add Auto Complete to Textbox or Combobox Controls in Visual Basic 2005/2008, VB.NET 2010, and newer

Note: This is an article which was taken from my vbcodesource.com website and is quite old. So take that into consideration… 🙂

As you will see, adding Auto-Complete support to textbox and combobox controls has NEVER been easier than it is now in Visual Basic.NET 2005. No longer do you HAVE to use the Windows API’s to get the Auto Complete functionality.

I will assume you already have a ‘Windows Application’ based project open. You can go ahead and add a textbox control or combobox control to the form. Select the textbox and goto the properties window. (I will assume you added a textbox, but the process is the same for a combobox also. I will just refer to textbox instead of combobox.)

There are 3x properties that relate to the ‘Auto Complete’ feature. You have “AutoCompleteCustomSource”, “AutoCompleteMode” and “AutoCompleteSource”. AutoCompleteCustomSource is exactly how it sounds. You can have your own text that you want the user to be able to use when they are typing in the control. But, I am not interested in going over that property right now though. There is really nothing more to it than what I already said. 🙂

OK, the “AutoCompleteMode” property is simply how the AutoComplete text responds. Click on the “AutoCompleteMode” property and then open the dropDown list associated with it. You will see 4x ‘modes’ available. “None, “Suggest”, “Append”, and “SuggestAppend”. This property MUST be selected either “Append”, “Suggest”, or “SuggestAppend” for the AutoComplete feature to work. If you select “Suggest” mode, then ‘AutoComplete’ will display a list that matches the text that has been typed thus far that you can scroll through and choose from. If you select “Append” mode, then as you are typing, it will automatically highlight the closest match to the text thus far in the textbox/combobox control, usually in alphabetical order. Plus, while you are typing, you CAN use the ‘Up/Down’ buttons on the keyboard to scroll through the list of text that is similar to the text that was typed in the textbox control. The “SuggestAppend” mode, is of course the combination of the other 2x modes. As you are typing the text, it will highlight the first occurance of matching text while also displaying a list of all the text that matches what has been typed in the control.

The “AutoCompleteSource” property is exactly how it sounds. It is the location on the computer system to get the ‘AutoComplete’ list from. Click on that property and then click on the box to dropdown the enumerator values that are available as the source. Available values, at least on my computer are: “FileSystem”, “HistoryList”, “RecentlyUsedList”, “AllUrl”, “AllSystemSources”, “FileSystemDirectories”, “CustomSource”. Just like the “AutoCompleteMode” property, you MUST select one of these values for you to get ‘AutoComplete’ functionality. Since all of the values are self-explanatory, I will not go over them.

Just to test out the ‘AutoComplete’ feature. With a textbox control, (Or Combobox), click on the “AutoCompleteMode” property and select the “Suggest” mode. Then click on the “AutoCompleteSource” property and then select “AllUrl” as the source. Run the project, start typing in the textbox ‘www’ and you should see a list of url’s you’ve visited that start with ‘www’ popup. You can then click on one of the urls and it will put that url text in the textbox.

And thats ALL there is to it! Enjoy!

Jason

 

Textbox Scroll to Bottom or Top – Part 2 using VB 6.0 and VB.NET


I have a previous post on how to scroll the textbox controls contents to the bottom and the top that many visitors didn’t appear to like. It has a 1.5 rating out of 10 so, it is not liked very much. I really don’t know why because the code seems to work fine. Maybe because the cursor position would be moved? This new code won’t move the actual cursor if thats the case. But anyways, I decided to post another way to scroll the textbox contents by using the Windows API. This method uses the SendMEssage function and passes a couple constants to perform the up and down scrolling process. Below are the codes for VB 6.0 and VB.NET.

_____________________________________

Visual Basic 6.0 Declarations…

    '
    'Used to Send Messages to the control and will be used with a request for the current line number in the textbox control.
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal winHandle As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    '
    'Constants used for the SendMessage API call function.
    '
    Private Const EM_SCROLL = &HB5
    Private Const SB_BOTTOM = 7
    Private Const SB_TOP = 6

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

   

   

Visual Basic .NET Declarations…

    '
    'Used to Send Messages to the control and will be used with a request for the current line number in the textbox control.
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal winHandle As _Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    '
    'Constants used for the SendMessage API call function.
    '
    Private Const EM_SCROLL = &HB5
    Private Const SB_BOTTOM = 7
    Private Const SB_TOP = 6

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

Now all thats left is to setup and call the SendMessage API function…

The first parameter of the SendMessage call is the handle to your textbox control that you want to use.

The second parameter is to let the api know we want to perform some type of scroll action on the control.

The third parameter is the type of scroll to perform on the control.

The fourth parameter is Not used in this example.

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

Visual Basic 5.0, 6.0 – Scroll the Textbox to the Bottom/Top…

        '
        'This will make the textbox scroll to the bottom without moving the cusor position.
        SendMessage TextBox1.hWnd, EM_SCROLL, SB_BOTTOM, 0
        '
        'The will make the textbox scroll to the top without moving the cursor.
        SendMessage TextBox1.hWnd, EM_SCROLL, SB_TOP, 0

Visual Basic.NET – Scroll the Textbox to the Bottom/Top…

 

        '
        'This will make the textbox scroll to the bottom without moving the cusor.
        SendMessage(TextBox1.Handle.ToInt32, EM_SCROLL, SB_BOTTOM, 0)
        '
        'The will make the textbox scroll to the top without moving the cursor.
        SendMessage(TextBox1.Handle.ToInt32, EM_SCROLL, SB_TOP, 0)

 

_____________________________________

Alright, those codes above should have moved your textbox control contents down to the bottom and up to the top. Also the cursors position will Not be moved like the other method would do. Hopefully this post will be liked alittle better. If not, then I don’t know what else you want and give up… 🙂

Jason

Textbox Scroll with Cursor to the Top or Bottom with VB and VB.NET

I originally made this post for VB 6.0, but I figured I would go ahead and add the .NET code here as well. The first codes is for VB 6.0 and the code after is for Visual Basic.NET , VB 2005/2008, VB 2010, and newer.


Visual Basic 6.0

This will scroll to the bottom of the textbox control.

'
Text1.SelStart = Len(Text1.Text)

 

The code below will put the cursor to the beginning of the textbox and scroll to its position.

'
'Goto the top/first character in the textbox control.
TextBox1.SelStart = 0

Visual Basic.NET 2002/2003, and VB.NET 2005, 2008, 2010

This will put the cursor position at the bottom and scroll the textbox to the end of its contents.

'
'Set the cursor to the end of the textbox.
Textbox1.SelectionStart = Textbox1.TextLength
'
'Scroll down to the cursor position.
Textbox1.ScrollToCaret()

 

The codes below will set the cursor and scroll the textbox to the top or beginning of its contents.

'
'Set the cursor to the first character in the textbox which will be at the top of the control.
TextBox1.SelectionStart = 0
'
'Make the textbox scroll to the actually caret postition.
TextBox1.ScrollToCaret()

Minor Revision: 2015

Clear all Textboxes on Form in VB 6.0

This code will clear all textbox controls on the form. The second code near the bottom of the post will clear all control but bypassing certain ones.

'Visual Basic 6.0

Public Sub ClearAllText(ByVal frm As Form, ByVal ctl As Control)

  For Each ctl In frm

    If TypeOf ctl Is TextBox Then

      ctl.Text = ""

    End If

  Next

End Sub

Updated! 02/07/2008: A request was made on how to clear or manipulate all controls on a form but to bypass/skip/passby certain controls named a certain name.

Clear All Textbox Controls except those with Specific Names

'

'Setup a Control object.

Dim ctl As Control

'

'Go through each control the form has.

For Each ctl In Me.Controls

'

'If the control type if a Textbox control, then…

  If TypeOf ctl Is TextBox And Not ctl.Name = "txt2" Then

    '

    'Change the text of the controls to "" (Nothing, Blank)

    ctl.Text = “”

  EndIf

Next

Revisited: 2015

Getting the Current Line Number in Textbox using VB.NET

There is unfortunately no so-called ‘built-in’ way to get the current line number in a Visual Basic.NET Textbox control. Microsoft added that support in Visual Basic 2005/2008/2010. But many still use the original .NET versions which do not have that feature in the textbox control.

What to do? Well, there is a somewhat easy way to add this feature by using the Windows API. The API to use is the SendMessage API call. Below is the declaration, constant value, and line# variable…


Getting the Current Line Number code

    '
    'Unfortunately, .NET before 2005 doesn’t have a built-in feature to get the current
    'line. So I am using the SendMessage API to do it. You CAN do various extractions or
    'whatever to do it, but this is easier and more elegant in my opinion.
    '
    'Used to Send Messages to the control and will be used with a request for the
    'current line number in the textbox control.
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal winHandle As Int32, _
        ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    '
    'Constant found in the VB 6.0 API Viewer App IT will be passed to the SendMessage
    'API to tell the control to give the current Line # from the textbox character position.
    Public Const EM_LINEFROMCHAR = &HC9
    '
    'Will be used to contain the current line #.
    Dim lineNum As Int32

You can put the codes below in the textbox_Click, textbox_Keydown, textbox_TextChange event you want to take place or a button control  to get the line# results or whatever. In other words, put the code for where/when you want the current line results.

I put these codes in the Textbox_TextChanges event and used a label control to display the current line number. Its also worth nothing that Windows reports line # starting at “0”. So if the API returns a “0”, you can Add (+) one to the value which will then show the line# as 1. Another example is if the Windows API returns a 1, but in reality the current line is the second, then as before simple add (+) a One(1) to the value which will then be line #2.

        '
        'Call the API to get the Line # from the current char. Txt is your textbox control.
        lineNum = SendMessage(txt.Handle.ToInt32, EM_LINEFROMCHAR, -1, 0)
        '
        'Windows sees the first line in a edit box as line 0. So just add a value of 1 to the
        'result if you want the first line to be line #1.
        lblCurrent.Text = "Current Line: #" & (lineNum + 1).ToString
        '
        'Check for line count first to keep a possible error from happening if the textbox has
        'none.
        If txt.Lines.Length > 0 Then
            '
            'Get the text at the current line position.
            txtLineText.Text = txt.Lines.GetValue(lineNum)

        End If


Thats all there is to it! When you run your project and type your characters or make a new line or whatever in the textbox, the label control should show the number that the caret or cursor is currently at. If you want the Line # code to be updated when the mouse clicks on a different line or position, simply put the code above in the textbox_Click event. Have Fun!

Jason

Revisited: 2015

Auto Complete Support in Textbox and Combobox Controls using VB 6.0 and VB.NET

Autocomplete is a very useful feature that Microsoft added with the release of Internet Explorer 5.0 and therefore Internet Explorer 5.0 or higher needs to be installed on the target computer. Adding autocomplete to your applications can be a real benefit to the user and is a easy feature to implement.

Note: Visual Basic 2005 and Visual Basic 2008 already has Autocomplete capabilities in the Textbox and Combobox controls by default.


There are a few different locations you can get the autocomplete data from…

  • File System
  • History List
  • URLs Recently Used
  • ALL URLs
  • ALL System Sources

There are likewise a few different options available to set the way you want AutoComplete to react.

  • Append
  • Suggest
  • Append & Suggest

Visual Basic.NET

    '
    'The API call that enables the autoComplete feature to your combo or textbox controls.
    Private Declare Sub SHAutoComplete Lib "shlwapi.dll" (ByVal controlHandle As Int32, ByVal _
        completeFlags As Int32)

Visual Basic 6.0

    '
    'The API call that enables the autoComplete feature to your combo or textbox controls.
    Private Declare Sub SHAutoComplete Lib "shlwapi.dll" (ByVal controlHandle As Long, ByVal _
        completeFlags As Long)

The SHAutoComplete API Sub is easy to use. The first parameter is the Handle to the Edit (Combobox/Textbox) control you want to enable Auto Complete too. The second Parameter is how you want your Autocomplete to react and display.


 

    '
    'Various Flags you can set to be the source.
    '
    Private Const Defaults = &H0

    Private Const Filesystem = &H1

    Private Const HistoryList = &H2

    Private Const URLRecentlyUsedList = &H4

    Private Const AllURLs = (HistoryList Or URLRecentlyUsedList)

    Private Const AllSystemsSources = AllURLs Or FileSystem

The flags below are used to specify how you want your autocomplete feature to react.

    '
    'Flags you can use to specify how your want autocomplete to react.
    '
    Private Const append = &H40000000 Or &H20000000

    Private Const suggest = &H10000000 Or &H80000000

    Private Const suggestAppend = &H40000000 Or &H10000000

Now that the API based codes are out of the way, you can setup the edit based controls (Like Textbox and Combobox) to have Autocomplete capabilities. Below is a simple example of adding autocomplete to a textbox control that I named txt.

        'Set autocomplete to the textbox using the system defaults.
        SHAutoComplete(txt.hWnd, Defaults)

        'You can also specify how it will react and what it will display.
        SHAutoComplete(txt.hWnd, suggest Or AllURLs)

If your using .NET, just change txt.hWnd to txt.Handle.ToInt32.

Unfortunately adding this feature to a Combobox isn’t as straight forward. Thats because when you use the Handle/Hwnd Property for the Combobox it returns the Main Window Portion of the control and NOT the Edit Window portion. There are a couple ways to get around this though. One way it to use the GetComboBoxInfo API or to use the FindWindowEx API call. I chose to go ahead and use the FindWindowEx API.


Visual Basic.NET

    '
    'API Function used to get the Edit window of a combobox control.
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As  _
        Int32, ByVal hwndChildAfter As Int32, ByVal lpszClass As String, ByVal lpszWindow As String) As Int32

Visual Basic 6.0

    '
    'API Function used to get the Edit window of a combobox control.
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal handle As _
        Long, ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long

Now you just need to use the FindWindowEx Function using the 1st and 3rd parameter. And once you call the FindWindow function, simply pass its return value to the SHAutoComplete Method.

        '
        'Simply change this variable type to a Integer if your using VB.NET.
        Dim cmbHandle As Long

        'Retrieve the handle to the ‘Edit’ Window of the combobox. I named my Combobox control cmb.
        cmbHandle = FindWindowEx(cmb.hWnd, 0, "Edit", vbNullString)

        'Activate Autocomplete to the combobox edit window.
        SHAutoComplete(cmb.hWnd, Defaults)

Tip: Here is how you can specify exactly how to react and display….

        '
        'You can also specify how it will react and what it will display.
        SHAutoComplete(cmb.hWnd, suggest Or AllURLs)

Again, change cmb.hWnd to cmb.Handle.ToInt32 if your using Visual Basic.NET.

Thats all there is to it! Depending on your application, you or your apps user will really appreciate you putting that feature in your program. Well, I guess there is nothing more to add. Have fun!

Jason

How to Search/Find Text in a Textbox control in VB.NET, VB 2008, 2010, and Higher

Most any kind of word processing application from the basic Windows Notepad to the more advanced OpenOffice Writer includes a feature to search its text. There are 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 thought it could be useful for someone if I posted it.

Visual Basic.NET, VB 2008/2010/2013

Since DotNET doesn’t have built-in support for searching/finding text, you will need to make your own code. This is a snippet I originally made for a more advanced textbox control called: ‘csTextBoxX’. There are many other features available as well if you want to take a look at it here and with an 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 be easy to understand but It does work. You can also go through it and refine/optimize it where you can. I’m sure there are areas that need some attention.

The function supports Match Casing, No Match Casing, and Search at a specific Starting and/or Ending position within the text. There is also an option whether to Highlight the text that was found or not. 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 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.


That’s all I guess. As a side note, I was almost sure that Visual Basic 2005 (.NET 2.0 and Newer) added a find/search feature in the textbox control but I couldn’t find it. So you can use this function for VB 2005, Visual Basic 2008, VB 2010, and newer. Have fun 🙂

Jason

Revised: 2014

Allow only certain characters in a textbox using VB 6.0

Shows how to use Visual Basic 6.0 (VB 5.0 as well) to only allow certain, specific characters in a textbox control.

        '
        '1 textbox ‘put in keypress procedure of textbox
        '
        Const Numbers$ = "0123456789."

        If KeyAscii <> 8 Then

            If InStr(Numbers, Chr(KeyAscii)) = 0 Then

                MsgBox("error")

                KeyAscii = 0

                Exit Sub

            End If

        End If

Limit text input in a Textbox by Visual Basic 6.0

The code snippet below will limit the kind of text your user can put in a textbox control using VB.

'vb 6.0

Function LimitTextInput(ByVal source as Long) As String
'
'put the next line in the Textbox_KeyPress event
'
'KeyAscii = LimitTextInput(KeyAscii)
'
'change Numbers with any other character
Const Numbers$ = "0123456789."'backspace =8

If source <> 8 Then

  If InStr(Numbers, Chr(source)) = 0 Then

    LimitTextInput = 0

    Exit Function

  EndIf

EndIf

LimitTextInput = source

End Function

How to Autocomplete ComboBoxes like Textboxes using VB and VB.NET

I’ve seen this question before and the answer is pretty simple when you get an understanding of the combo control. When you use the handle associated with the Combobox, it is the main portion of the Control that the handle refers to and NOT the edit box which is the important part.

Note: IF your using Classic Visual Basic (5.0 or 6.0) then simply change the int32 and Integer variable types to Long types since the codes here are setup for VB.NET.


To get the handle to this area of the Combobox will require a windows api call. The API Call is: FindWindowEx. This call will get the Handle of the specified control according to the specified Class Name.

    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As  _
        Int32, ByVal hwndChildAfter As Integer, ByVal lpszClass As String, ByVal lpszWindow As String) As Int32

The parameters we are interested in is hWnd and lpszClass. You can set the others to Null. The Handle would be the handle to the main portion of a control. Which is what the ComboBox.Handle.ToInt32 method returns. The class name is the class type you want to get the handle too. Which happens to be “Edit”

        'Will hold the “Edit” handle given by the FindWindowEx API call from the ComboBox.
        Dim cmbHandle As Int32 = FindWindowEx(ComboBox.Handle.ToInt32, 0, "Edit", vbNullString)

And thats all you need to make the combobox Autocomplete capable. Simply specify the cmbHandle integer in the SHAutoComplete ‘handle’ parameter and it will give your combobox the Autocomplete feature!

More Info: If you want the complete source code to autocomplete combobox controls and textbox controls with the actual SHAutoComplete source codes as well, then simply click on this link to view the post.

               Jason

Revised: 2015

Only allow Numbers in a TextBox Control in VB.NET

Code snippet to allow only numbers in a textbox.

Put the snippet below in the Textbox_Keypress Event.

        '
        'This will simply check the Keychar that was pressed and will only allow numbers
        'to be typed‘in the TextBox control.
        'Put in the ‘TextBox_KeyPress’ event.
        If Not Char.IsNumber(e.KeyChar) Then

            e.Handled = True

        End If

Read a Text File and Add text to a Textbox Control With Stream Reader Class – VB.NET

This will open a textfile and then add the text contents to a textbox control using VB.NET, VB 2008, VB 2010, and Visual Basic 2013.

        '
        'Read from a text file to a textbox with the Stream Reader Class
        'Put a textbox on the form and name txt and set to multi-line
        '
        'This will make the Reader read the entire contents of the textfile and write to the
        'TextBox
        'Create a new stream reader specifying its source to be from the newTextFile.txt file.
        Dim sReader As IO.StreamReader = New IO.StreamReader("c:\newTextFile.txt")
        '
        'Make the textbox keep the current data while adding the new data from the stream.
        txt.AppendText(sReader.ReadToEnd)

Revisted: 2015

Why it Beeps Pressing Enter in a Textbox Control and How to Fix it in .NET

I’ve seen many questions on how to disable the beep that occurs when you press the ‘Enter’ key in a single line Textbox control or a Combobox control. From my understanding the reason is because in Single Line mode, the textbox control doesn’t know how to handle the Enter/Return key, and is basically reporting an error when you get the beep. But it is pretty simple to keep that from happening.

Visual Basic 2005, VB 2008, and VB 2010

        ' This code will disable the 'Beeping' you get when you press the 'Enter' key in a Textbox
        'control. This code needs to go into the 'Textbox_KeyDown' event.
        If e.KeyCode = Keys.Enter Then

            e.SuppressKeyPress = True

        End If

Visual Basic.NET 2002/2003

The code below will also work with VB 05, 2008, and Visual Basic 2010. But the above snippet will NOT work on Visual Basic.NET 2002 or .NET 2003.

        '
        'This is for VB.NET 2002, .NET 2003, and Higher
        'Put in your textbox controls Textbox_KeyPress
        '
        If e.KeyChar = Chr(13) Then

            e.Handled = True

        End If
        '
        'Or you can use the code below. Which will be the same result as above.
        '
        If e.KeyChar = vbCr Then

            e.Handled = True

        End If

That should take care of the beeping problem. Take care.