Category Archives: Strings/Text

A collection of articles and source code on manipulating or primarily working with strings and text.

How to Create/Make a String Buffer using VB.NET and VB 6.0

This is a simple tip on how to create a string buffer with a specific number of spaces. The code in this article will create a string with a 128 spaces or buffer size.


The traditional way a lot of the VB 6.0 programmers used to created a string variable with a specific size buffer as shown below, is not supported in .NET.

'
'This method works just fine with Classic VB (5.0/6.0), but Visual Basic.NET unfortunately
'does not support this somewhat traditional string buffer code.
Dim buf As String * 128

That code will create the string variable “buf” with a buffer of 128 spaces. IF you try running that code in Visual Basic.NET, then you should get a “End of statement expected.” Error.

There is another way, that I use even with VB 6.0 which will work with VB.NET as well. That is by using the Space (Space$) Function.

Public Function Space(ByVal Number As Integer) As String

The Space() Function will return a String with a buffer size that you specify. If your using Visual Basic 6.0 then use the Space$() Function which will return the value as an actual String. String$ is supported in .NET but the Space() returns a String variable no matter which format you use.

'
'Visual Basic.NET AND Visual Basic 6.0 supports this way of creating a String with
'a specific sized buffer.
Dim buf AsString

buf = Space(128)
'
'This should throw a message with the size of the buffered string variable.
MsgBox(Len(buf), MsgBoxStyle.OKOnly, " The size of the String")

Results of the codes above


That’s all you have to do in DotNET and 6.0 to make a String buffer. This code will work in all versions of VB.NET, Visual Basic 2008, VB 2010, and newer.

Anyways, hopefully this small little tip helped 🙂

Jason

Revised: 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 do Multi-Line Messageboxes and Inputboxes in VB 6.0 and in VB.NET

How to do Multiple Lined Messages (Input and Messagebox) is a very common question. Unfortunately the Message Functions do not have a native multi-lined option. But there is a simple workaround to create messages on different/separate lines. It is a little constant called: vbNewLine which is evidentially a brother to the vbCrLf constant.

Public Const vbNewLine As String = vbCrLf

Public Const vbCrLf As String = vbCrLf

These constants are basically the same as combining Chr(13) and Chr(10) characters which maps to the Carriage Return – Line Feed. Using the constants is very simple. Just add the constant outside the string that you want to start a new line at. Below is basic examples of using them…


        'Create a new string to display.
        Dim myString As String

        myString = "I want a Multi-Lined Message!"

Now that there is a string to use, simply add the vbNewline constant as so…

        myString = "I want a Multi-Lined Message!" & vbNewLine & vbNewLine & "This message is under the first!"
        MsgBox(myString)

The “myString” variable should now display as a multiline string.

The multilined messagebox.


That’s all you have to do! The vbNewline Constant (vbCrlf also) is all you need to do to make most multi-lined messages. This is a simply fix and should work just fine for most messagebox/inputbox messages and such.

Remember that these constants are available in ALL versions of Visual Basic. VB 6.0 through to VB.NET 2008, VB.NET 2010, and VB 2013. Have Fun!

Jason

FREE eBook – Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET

This is another resource that how been around for awhile but is still plenty useful and helpful. Especially if your a Visual Basic 5.0/6.0  developer making the move to Visual Basic.NET. It is pretty large and covers a wide range of areas taking up TwentyOne(21x) Chapters. It goes over The differences between VB Classic and .NET to Resolving Upgrade Issues and Preparing your Applications for the Upgrade. Below is a small excerpt…


Excerpt…

Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET is the complete technical guide to upgrading Visual Basic 6 applications to Visual Basic .NET, covering all upgrade topics from APIs to ZOrders. It shows how to fix upgrade issues with forms, language, data access, and COM+ Services, and how to upgrade applications with XML Web services, ADO.NET, and .NET remoting. It also provides big-picture architectural advice, a reference of function and object model changes, hundreds of before-and-after code samples, and a CD packed with useful examples.


If you are interested in this resource, then click this link to check it out. Try this link as well. Have fun 🙂

Jason

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