?
Solved

compile bug

Posted on 2006-05-26
2
Medium Priority
?
276 Views
Last Modified: 2010-04-07
Hi,

Please assist what could be missing, when we run (F5) the code below, the alignment of the data in the listbox is perfect, sadly when we run the compile exe application, the results were different. the second element was tab way to far.

i.e.

1                    2 3 4 5
5                    6 5 4 3


Thanks.

Option Explicit

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const LB_SETTABSTOPS = &H192
Private Sub Form_Load()
    'change the values below to change the tabstop positions
    Call ListInit(List1, 30, 60, 90, 120, 150)
End Sub
Private Sub Command1_Click()
    Call ListAdd(List1, 1, 12, 50, 1, 20)
    Call ListAdd(List1, -5, -4, -2, 50, -3)
    Call ListAdd(List1, 12, 12, 500, 111, 20)
End Sub
Private Sub ListInit(ByRef lst As ListBox, ParamArray tabs() As Variant)
    Call SendMessage(lst.hWnd, LB_SETTABSTOPS, (UBound(tabs) - 1), tabs(0))
End Sub
Private Sub ListAdd(ByRef lst As ListBox, ParamArray vals() As Variant)
    Dim temp As String
    Dim i As Integer
    For i = LBound(vals) To UBound(vals)
        If (i = LBound(vals)) Then
            temp = vals(i)
        Else
            temp = temp & vbTab & vals(i)
        End If
    Next
    Call lst.AddItem(temp)
End Sub
0
Comment
Question by:RyanBank
  • 2
2 Comments
 
LVL 5

Expert Comment

by:Willibob
ID: 16768024
Just tried your code and it does the same on my machine.

Why don't you ditch the ListBox control in favour of a ListView control?

That way you'll have full control over the alignment, column widths etc. without using API calls.

Let me know if you'd like some sample code for setting it up.

Cheers

Bill
0
 
LVL 5

Accepted Solution

by:
Willibob earned 2000 total points
ID: 16768118
If you want to stick with the ListBox then use the code below. (I've incorporated some of the code from: http://www.devx.com/vb2themax/Tip/18592)
I think the problem may have been in the way that you were passing values to ListInit i.e. passing as variant instead of long.

Regards

Bill


Option Explicit

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const LB_SETTABSTOPS = &H192
Private Sub Form_Load()
'change the values below to change the tabstop positions
   
Dim tabs(4) As Long
tabs(0) = 30
tabs(1) = 60
tabs(2) = 90
tabs(3) = 120
tabs(4) = 150

ListBoxSetTabStops List1, tabs()
End Sub
Private Sub Command1_Click()
    Call ListAdd(List1, 1, 12, 50, 1, 20)
    Call ListAdd(List1, -5, -4, -2, 50, -3)
    Call ListAdd(List1, 12, 12, 500, 111, 20)
End Sub
Private Sub ListBoxSetTabStops(lb As ListBox, tabStops() As Long)
    Dim numEls As Long
    numEls = UBound(tabStops) - LBound(tabStops) + 1
    SendMessage lb.hWnd, LB_SETTABSTOPS, numEls, tabStops(LBound(tabStops))
End Sub

Private Sub ListAdd(ByRef lst As ListBox, ParamArray vals() As Variant)
    Dim temp As String
    Dim i As Integer
    For i = LBound(vals) To UBound(vals)
        If (i = LBound(vals)) Then
            temp = vals(i)
        Else
            temp = temp & vbTab & vals(i)
        End If
    Next
    Call lst.AddItem(temp)
End Sub
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question