Sunday, February 10, 2013

Some VB6 Tricks

Though Visual Basic 6 has started getting lost in antiquity since the advent of .NET, we do know there are still zillions of VB6 programs in existence, with medium to high complexity. They are not migrated to C# or VB.NET and are maintained in their current state.

This may be due to several reasons, the primary being the high cost involved in migrating to .NET platform. Given below are a few tips and tricks to better the user experience that I’ve come across during my programming endeavors while dealing with VB6:
[1] Adding horizontal scrollbar to a list box: The standard listbox control provided by VB6 pretty much serves our purpose when we want to show a list of possible values to a user from which she can select one. However, there is one slight glitch: There is no horizontal scrollbar in it. If the number of list items increase beyond the visible area of the listbox control, then yes, we automatically get a vertical scrollbar to scroll. But when the text width is too much, the user cannot read the entire text, you get something like this:






In the above screen there is a vertical, but no horizontal bar. To overcome this limitation, we need to use a Windows API function called SendMessage(). Windows API is a powerful method to get access to functionality that VB6 does not provide out of the box. In order to use the SendMessage() function, all you have to do is declare this function at the top of your VB Form code:

Option Explicit
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, ByVal _
wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const LB_SETHORIZONTALEXTENT = &H194


Do remember that you only need to declare this once in your form, though this function may be called at several places. If you are going to use the function at several places, then declare it in a global module instead and make it public. The second declaration beginning with “Const” declares a constant that comes handy while using this function. Now, let us use this function by calling it. Typically, this will be inside the Form_Load event of your form:
Dim x As Long
Dim s As String
x = TextWidth(List1.List(0) & " ")
If ScaleMode = vbTwips Then _
x = x / Screen.TwipsPerPixelX ' if twips change to pixels
SendMessage List1.hwnd, LB_SETHORIZONTALEXTENT, x, 0

Here, we first determine the width our horizontal scrollbar will have. This is determined by the variable x. We assign it the text-width of the first item in your listbox i.e. List1.List(0). In a real world application however, you have to make it equal to the widest string in your listbox. Once you do this, you will have a horizontal scrollbar in your listbox as shown below:







[2] Adding soft-search facility to a listbox: Soft-search is a facility provided by many applications to instantly show filtered results in a dropdown as you type. Popular example of this is the google search box that shows matching search terms in a dropdown as you start typing your search text. Now, suppose, you want to provide a similar facility in your VB6 

Form by providing a textbox control to type and a listbox to filter the soft-search results. How difficult do you think this would be? Pretty trivial, I would say, by using the SendMessage() API function. You already get what the user is typing in the textbox in the Change() event. All you have to do is call this API function, and pass the search string and list-box handle as the parameters.

The API will automatically find and return the index of the searched string if found in the Listbox!! Now all you have to do is set the topindex property of the listbox to that of the value returned by SendMessage() function:


Private Sub Text1_Change()
Dim i As Integer
i = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal Text1.Text)
If i <> -1 Then
List1.TopIndex = i
'Else
'List1.Selected(
End If
End Sub



No comments:

Post a Comment