Adding a horizontal scroll bar to a listbox

Option Explicit

Private Declare Function SendMessageA Lib "user32" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long


'Purpose : Adds items to a listbox and if neccessary sets the
' width of the horizontal scroll bar to the maximum width of the
' items in the listbox.
'Inputs : lbListbox The listbox to add the item to.
' sItemText The text to add to the listbox.
' [iIndex] The position within the object where the new item or row is placed.
'Outputs : Returns True on success
'Notes :
'Revisions :
'Assumptions :

Function ListboxAddItem(lbListbox As ListBox, sItemText As String, Optional iIndex As Integer = -1) as Boolean
Dim fTextWidth As Single, fExistScrollWidth As Single
Dim oParentFont As StdFont
Const LB_SETHORIZONTALEXTENT = &H194, LB_GETHORIZONTALEXTENT = &H193

On Error Resume Next

'Add item to listbox
If iIndex > -1 Then
lbListbox.AddItem sItemText, iIndex
Else
lbListbox.AddItem sItemText
End If

'Store the form's original font
Set oParentFont = lbListbox.Parent.Font
'Set the form's font to the listbox's font
Set lbListbox.Parent.Font = lbListbox.Font
'Get width of text on the form
fTextWidth = lbListbox.Parent.TextWidth(sItemText & " ") 'Extra space allows for vertical scroll bar
'Restore the form's font
Set lbListbox.Parent.Font = oParentFont

'Get the width of the existing scroll bar
fExistScrollWidth = SendMessageA(lbListbox.hwnd, LB_GETHORIZONTALEXTENT, 0, 0)

If lbListbox.Parent.ScaleMode = vbTwips Then
'Change twips to pixels
fTextWidth = fTextWidth / Screen.TwipsPerPixelX
End If

If fTextWidth > fExistScrollWidth Then
'Increase width of scroll bar
Call SendMessageA(lbListbox.hwnd, LB_SETHORIZONTALEXTENT, fTextWidth, 0)
End If
ListboxAddItem = (Err.Number = 0)
End Function


'Purpose : Modifies the text of an item in a listbox and if neccessary sets the
' width of the horizontal scroll bar to the maximum width of the
' items in the listbox.
'Inputs : lbListbox The listbox to update the item in.
' sNewItemText The new text for the item in the listbox.
' [iIndex] The index of the item to update within the listbox.
'Outputs : Returns True on Success
'Notes :
'Revisions :
'Assumptions :

Function ListboxUpdateItem(lbListbox As ListBox, sNewItemText As String, iIndex As Integer) As Boolean
Dim fTextWidth As Single, fExistScrollWidth As Single
Dim oParentFont As StdFont
Const LB_SETHORIZONTALEXTENT = &H194, LB_GETHORIZONTALEXTENT = &H193

'Add item to listbox
On Error GoTo ErrFailed
If lbListbox.List(iIndex) <> sNewItemText Then
lbListbox.List(iIndex) = sNewItemText
'Get width of text
Set oParentFont = lbListbox.Parent.Font
Set lbListbox.Parent.Font = lbListbox.Font
fTextWidth = lbListbox.Parent.TextWidth(sNewItemText & " ") 'Extra space allows for vertical scroll bar
Set lbListbox.Parent.Font = oParentFont
fExistScrollWidth = SendMessageA(lbListbox.hwnd, LB_GETHORIZONTALEXTENT, 0, 0)

If lbListbox.Parent.ScaleMode = vbTwips Then
'Change twips to pixels
fTextWidth = fTextWidth / Screen.TwipsPerPixelX
End If

If fTextWidth > fExistScrollWidth Then
'Increase width of scroll bar
Call SendMessageA(lbListbox.hwnd, LB_SETHORIZONTALEXTENT, fTextWidth, 0)
End If
End If
ListboxUpdateItem = True

Exit Function

ErrFailed:
Debug.Print "Error in ListboxAddItem: " & lbListbox.Name & " Description: " & Err.Description
ListboxUpdateItem = False
End Function


'Purpose : Adds a horizontal scroll bar to a listbox
'Inputs : lbListbox The listbox to add the scrollbar to.
'Outputs : Returns True on success
'Notes :
'Revisions :
'Assumptions :

Function ListboxAddHorizontalScollBar(lbListbox As ListBox) As Boolean
On Error GoTo ErrFailed
Dim fTextWidth As Single
Dim oParentFont As StdFont, fExistScrollWidth As Single
Dim lThisListItem As Long, fMaxScollWidth As Single, lMaxTextLen As Long
Const LB_SETHORIZONTALEXTENT = &H194, LB_GETHORIZONTALEXTENT = &H193

'Add item to listbox
On Error GoTo ErrFailed
'Set the parent font
Set oParentFont = lbListbox.Parent.font
Set lbListbox.Parent.font = lbListbox.font
Set lbListbox.Parent.font = oParentFont

'Determine max. length of text
For lThisListItem = 0 To lbListbox.ListCount - 1
If Len(lbListbox.list(lThisListItem)) > lMaxTextLen Then
lMaxTextLen = Len(lbListbox.list(lThisListItem))
End If
Next

'Get the text length
fTextWidth = lbListbox.Parent.TextWidth(String(lMaxTextLen + 1, "W")) 'Extra space allows for vertical scroll bar
'Restore the form's font
Set lbListbox.Parent.font = oParentFont

'Get the width of the existing scroll bar
fExistScrollWidth = SendMessage(lbListbox.hwnd, LB_GETHORIZONTALEXTENT, 0, 0)

If lbListbox.Parent.ScaleMode = vbTwips Then
'Change twips to pixels
fTextWidth = fTextWidth / Screen.TwipsPerPixelX
End If

If fTextWidth > fExistScrollWidth Then
'Increase width of scroll bar
Call SendMessage(lbListbox.hwnd, LB_SETHORIZONTALEXTENT, fTextWidth, 0)
End If
ListboxAddHorizontalScollBar = (err.number = 0)

Exit Function

ErrFailed:
Debug.Print err.description
Debug.Assert False
ListboxAddHorizontalScollBar = (err.number = 0)
End Function

No comments:

Visual Basic-6 has emerged as one of the standard Windows Programming Language and it has become a must for all Software people for developing Applications in Visual Environment. So it is, one must learn Visual Basic-6.

What is our Objective in this Courseware?


The Overall Objective in this Courseware is to give a Hands-on Approach to develop different projects in Visual Basic-6.0 using intrinsic, professional and user–created ActiveX controls and also develop projects using databases, DAO’s, ADO’s, DLL’s, Documents, Crystal Reports etc. covering almost all the essential features of VB-6 Professional Edition. After reading one lesson any interested reader will be able to get complete hands-on experience with the VB project and get a sense of fulfilment and achievement. Learning by doing is the motto with which this courseware is written. After giving a short introduction about VB-6 we will explain how to create and execute a project in VB using some intrinsic ActiveX controls. Creating and executing projects will be the central theme of all the lessons which we will be giving in this courseware.

What is Visual Basic-6?


Visual Basic-6 has its origin in Basic which was developed round about the year 1960, when high level languages were just being introduced to the computer community. Microsoft has made it extremely powerful by gearing all its good features to the Windows environment. Starting with the version 3 and then with 4, and then with 6, Visual Basic is now at version 6. Basic is a Procedure Oriented Language intended to implement single tasks in text based environment whereas Visual Basic is an Event Driven Language intended to implement Projects or Applications containing multiple tasks in Windows Environment.

What can Visual Basic do for you?

Visual Basic can serve as an ideal front end tool for the clients to interact. It has got connectivity mechanisms for all types of databases situated far and wide in a network and so it can cater to the needs of a large body of clients. Using the latest ActiveX technologies, it can integrate the functionalities provided by other applications like Word Excel and other Windows. Its internet capabilities provide easy access to documents and applications across the internet. Above all it embodies the Object Oriented Technology, which is the cutting edge technology for all the present day developments in the Software World. The final application is a true EXE file and so can be freely distributed.


Structure of VB-6 Projects:


We said earlier that VB-6 implements projects or applications. A project is developed using one or more Forms. A Form is simply a window containing one or more Controls. Controls in VB consist of labels, text boxes, list boxes, combo boxes, scroll bars etc. which are the constituents of windows environment. It is only the controls that give VB, its immense power and so there is a lot of interest in creating more and more powerful controls. ActiveX controls mark a significant development in controls technology. In fact all controls in VB-6 are ActiveX controls, which have the extension .ocx. These controls have properties whose values can be initialized at design time and also varied during run time. The properties are something like variables. The controls are activated by codes written in a high level language. By associating our problem variables with the properties of the controls, our problem variables can be manipulated to give the problem solution. In summary we can say that a VB project is made of forms, controls and their properties and codes.

Integrated Development Environment:

The working environment in VB is often referred to as the Integrated Development Environment or IDE, because it integrates many different functions such as design, editing, compiling and debugging within a common environment. Since all our projects are developed only in the IDE, let us now have a brief look at its features. You will be able to understand their uses at the time of building projects. The VB IDE looks as shown in the figure.