Horizontal Scroll bar for Listbox

I have gotten the API call to add a horizontal scroll bar to a list box, but don't know how to add it properly.  What if I wanted to add a horizontal scroll bar to all my listboxes, what would I have to code given the API function below?  Thanks.
--Vingamel

Public Const LB_SETHORIZONTALEXTENT = &H194

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Long) As Long


Public Sub AddHScrollToListBox(ByVal FormWithControl As Form, ByVal ListBox As Control, ByVal FullString As String)

  Dim ListHwnd As Long
  Static GreatestWidth As Long

  ' Get Focus of the ListBox to Make A Horizontal Scroll Bar For '
  ListBox.SetFocus
  ListHwnd = GetFocus()

  If FormWithControl.TextWidth(FullString) > GreatestWidth Then

    ' Add/Change Size of Horizontal Scroll Bar '
    Call SendMessage(ListHwnd, LB_SETHORIZONTALEXTENT, FormWithControl.TextWidth(FullString), NUL)
    GreatestWidth = FormWithControl.TextWidth(FullString)
    DoEvents

  End If

End Sub

LVL 1
VingamelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ca_hecklerCommented:
The actual code shown does not work properly.  Put the version bellow in a basic module in your app:

‘-------------------------------------------------------
‘begin module code

Option Explicit

Public Const LB_SETHORIZONTALEXTENT = &H194

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Long) As Long
Public Declare Function GetFocus Lib "user32" () As Long



Public Sub AddHScrollToListBox(ByVal FormWithControl As Form, ByVal ListBox As Control, ByVal FullString As String)

  Dim ListHwnd As Long
  Static GreatestWidth As Long

  ' Get Focus of the ListBox to Make A Horizontal Scroll Bar For '
  ListBox.SetFocus
  ListHwnd = GetFocus()

  If FormWithControl.TextWidth(FullString) > GreatestWidth Then

    ' Add/Change Size of Horizontal Scroll Bar '
    Call SendMessage(ListHwnd, LB_SETHORIZONTALEXTENT, FormWithControl.TextWidth(FullString), 0)
    GreatestWidth = FormWithControl.TextWidth(FullString)
    DoEvents

  End If

End Sub

‘end module code
 ‘-------------------------------------------------------


Then, wherever you want to add scrollbars to a list box, just call something like this:

   AddHScrollToListBox Form1, List1, "Bigger string in the list"

Claudio A. Heckler
www.flexint.com

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VingamelAuthor Commented:
ListBox.SetFocus gives a Run-Time Error 5 Invalid Procedure or Argument.  Here is my code.......

lstSoldiers.AddItem Soldiers(0, Whichsettlement, NumberOfSoldierTypes(Whichsettlement)) & " " & Soldiers(1, Whichsettlement, NumberOfSoldierTypes(Whichsettlement)) & " " & Soldiers(2, Whichsettlement, NumberOfSoldierTypes(Whichsettlement))
        AddHScrollToListBox frmUnits, lstSoldiers, Soldiers(0, Whichsettlement, NumberOfSoldierTypes(Whichsettlement)) & " " & Soldiers(1, Whichsettlement, NumberOfSoldierTypes(Whichsettlement)) & " " & Soldiers(2, Whichsettlement, NumberOfSoldierTypes(Whichsettlement))
0
ca_hecklerCommented:

What version of VB are you using ?

Anyway, you can try this:

‘-------------------------------------------------------
Option Explicit

Public Const LB_SETHORIZONTALEXTENT = &H194

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Long) As Long
Public Declare Function GetFocus Lib "user32" () As Long



Public Sub AddHScrollToListBox(ByVal FormWithControl As Form, ByVal ListBox As Control, ByVal FullString As String, ListHwnd As Long)

  'Dim ListHwnd As Long
  Static GreatestWidth As Long

  ' Get Focus of the ListBox to Make A Horizontal Scroll Bar For '
  'ListBox.SetFocus
  'ListHwnd = GetFocus()

  If FormWithControl.TextWidth(FullString) > GreatestWidth Then

    ' Add/Change Size of Horizontal Scroll Bar '
    Call SendMessage(ListHwnd, LB_SETHORIZONTALEXTENT, FormWithControl.TextWidth(FullString), 0)
    GreatestWidth = FormWithControl.TextWidth(FullString)
    DoEvents

  End If

End Sub
‘-------------------------------------------------------

Just add the extra parameter when calling:

   AddHScrollToListBox Form1, List1, "Bigger string in the list", List1.hwnd




0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

VingamelAuthor Commented:
Rock On!!!  Thanks a bunch.
--Vingamel
0
VingamelAuthor Commented:
Errr...one more thing: I am trying to use this code to add the horizontal scroll bar to 6 listboxes on the same form as soon as the form loads.  I am having troubles doing this as it appears only one listbox can have the scrollbar.  What am I doing wrong?
Thanks
--Vingamel
0
ca_hecklerCommented:
Post the piece of code where you try to add the scroll bars to the list boxes. You don't need to post the module.
--ca_heckler
0
VingamelAuthor Commented:
Yes, I tried that, as a matter of fact.  I just plopped the code in the Form_Load and modified each code for each listbox.  It does not work.  Now, what I was trying to do, and maybe this is where I was going wrong, was trying to set the width of the scrollbar equally for each listbox.  So I tried first to use a number parameter of 100 for the string, and that did not work.  I then created a string of 100 "x"'s and tried to use that as a parameter.  I don't get an error code, I just don't get the scroll bars.  Hey, do you want me to post another question with points, because really, you have already answered my fundamental question?
Thank you.
--Vingamel
0
ca_hecklerCommented:
Just show here where you are calling the AddHScrollToListBox, and with what parameters. Is in the form_load ? etc ?

Put some code to take a look ...

Heckler
0
VingamelAuthor Commented:
Private Sub Form_Load()

Call AddScrollBars

End Sub

Private Sub AddScrollBars()

Dim ListBoxFiller As String
ListBoxFiller = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

AddHScrollToListBox frmUnits, lstInactiveUnits, ListBoxFiller, lstInactiveUnits.hwnd
AddHScrollToListBox frmUnits, lstTransports, ListBoxFiller, lstTransports.hwnd
AddHScrollToListBox frmUnits, lstArmor, ListBoxFiller, lstArmor.hwnd
AddHScrollToListBox frmUnits, lstWeapons, ListBoxFiller, lstWeapons.hwnd
AddHScrollToListBox frmUnits, lstActiveUnits, ListBoxFiller, lstActiveUnits.hwnd
AddHScrollToListBox frmUnits, lstSoldiers, ListBoxFiller, lstSoldiers.hwnd
End Sub
0
ca_hecklerCommented:
Just to make sure this is not the problem, try to make the form visible BEFORE you add the scroll bars:

fdmUnits.Show

If it is not visible, then is possible that the list boxes does not have a handle assigned ...

Heckler
0
VingamelAuthor Commented:
I have frmUnits.Show 1 in another form where frmUnits opens from.
0
VingamelAuthor Commented:
..so, frmUnits.Show 1 happens before the form even displays...
0
ca_hecklerCommented:
try to put the add scroll code in the load event of the frmUnits form itself.

Other, try with a new project (it works with a simple one), and then add the forms structure of the actual project, to see when it stop working ...

Heckler
0
VingamelAuthor Commented:
Already tried the first option.
Will try your second option now.
0
VingamelAuthor Commented:
And just to verify I have the right API stuff, here is what I have in the called module...

' Graphics functions and constants used in the example.
Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Public Const SRCAND = &H8800C6
Public Const SRCCOPY = &HCC0020

' The following function and constants aren't
' used in this example.
Declare Function StretchBlt Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Public Const SRCERASE = &H440328
Public Const SRCINVERT = &H660046
Public Const SRCPAINT = &HEE0086

Public Const LB_SETHORIZONTALEXTENT = &H194

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Long) As Long
Public Declare Function GetFocus Lib "user32" () As Long

Option Explicit

Public Sub AddHScrollToListBox(ByVal FormWithControl As Form, ByVal ListBox As Control, ByVal FullString As String, ListHwnd As Long)

  'Dim ListHwnd As Long
  Static GreatestWidth As Long

  ' Get Focus of the ListBox to Make A Horizontal Scroll Bar For '
  'ListBox.SetFocus
  'ListHwnd = GetFocus()

  If FormWithControl.TextWidth(FullString) > GreatestWidth Then

    ' Add/Change Size of Horizontal Scroll Bar '
    Call SendMessage(ListHwnd, LB_SETHORIZONTALEXTENT, FormWithControl.TextWidth(FullString), 0)
    GreatestWidth = FormWithControl.TextWidth(FullString)
    DoEvents

  End If

End Sub

0
VingamelAuthor Commented:
When I created a new project with 6 listboxes like you suggested it adds a scrollbar to just the first one (which is what it is doing in my main project).
0
ca_hecklerCommented:
ok, now you got something interesting. My suggestion: start a new question, to see if anyone has a idea.  I'll take a look at it latter.  Btw, here also I got scroll only for the first list box.

Heckler
0
VingamelAuthor Commented:
Might be a focus problem....okidoki, thanks again for your help.
--Vingamel
0
VingamelAuthor Commented:
Here was the answer:
You're setting GreatestWidth in the first call and therefore you won't get into the If statement in every subsequent one.

 
Comment  
From: caraf_g
 Date: Friday, February 11 2000 - 07:13AM PST  
PS - normally, any width or height value passed into an API would be in pixels. Provided you've left your form's scalemode as twips you may have to use FormWithControl.TextWidth(FullString)/Screen.TwipsPerPixelX instead in your SendMessage

 
Thanks again for your help, Sir
--Vingamel
0
ca_hecklerCommented:
argh !  You are right !  The variable was declared as static.  It looks like someone was thinking about many calls to this code, for a simple listbox, and pick the greatest one.

Just remove the greatest and work with the size of the string (converted, as suggested).

Heckler
0
VingamelAuthor Commented:
Yeah, I just set the GreatestWidth to 100 and it works most beautifully.  Now my players will be glad I am back on the roll writing this game.  Thanks again.  (You are awfully friendly for being a heckler...lol)
--Vingamel
0
ca_hecklerCommented:
btw, what "heckler" means ?  This is my last name, and english is not my native language, so ...
0
VingamelAuthor Commented:
Heckler, in English, means to give somebody a hard time.  Say, at a soccer game, a heckler would yell bad things at the opposing team.

Heckler, auf englisch, bedeutet, jemand eine harte Zeit zu geben. Sagen Sie, an einem Fußballspiel, einen Heckler würde kreischen falsche Sachen am entgegensetzenden Team.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.