Visual Basic List API
SetWindowLong: Right-Align List Box Data and/or the Scrollbar
     
Posted:   Saturday November 03, 2001
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   None
Author:   Harald Genauck, VBnet - Randy Birch
     

Related:  

VBnet CoolTabs
GetTextExtentPoint32: Right-Align List Box Data
SetWindowLong: Right-Align List Box Data and/or the Scrollbar
SetWindowLong: Right-Align List Contents in a Combo
SendMessage: Align Text Box Contents Using Tabstops

SendMessage: Align List Box Contents Using Tabstops
WM_LBUTTONDOWN: Substitute a Tabbed List for a Combo's Dropdown List
WM_LBUTTONDOWN: Substitute a ListView for a Combo's Dropdown List
       
 Prerequisites
None.

The newsgroup request for code to create a right-aligned column of numbers in a list box was one of those fun projects in our mvp VB newsgroup that ended up generating quite a few methods to accomplish this. This routine, based on the method provided by Harald M. Genauck, forces the listbox to display its contents right-aligned by toggling the listbox alignment extended style bit. It's certainly the simplest method we encountered.  I've added an additional method to completely render UI specs and conformance useless by moving the vertical scrollbar to the left. Either of these options can be set independently, providing complete flexibility.
 BAS Module Code
None.

 Form Code
To the form containing a list (List1) and two check boxes (Check1, Check2), add the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const GWL_EXSTYLE As Long = (-20)
Private Const WS_EX_RIGHT As Long = &H1000
Private Const WS_EX_LEFTSCROLLBAR As Long = &H4000

Private Declare Function GetWindowLong Lib "user32" _
   Alias "GetWindowLongA" _
  (ByVal hwnd As Long, _
   ByVal nIndex As Long) As Long

Private Declare Function SetWindowLong Lib "user32" _
   Alias "SetWindowLongA" _
  (ByVal hwnd As Long, _
   ByVal nIndex As Long, _
   ByVal dwNewLong As Long) As Long

Private Enum AlignConstants
   alignLeft = 0
   alignRight = 1
End Enum

   

Private Sub Form_Load()

   Dim x As Long
   
   For x = 0 To Screen.FontCount - 1
      List1.AddItem Screen.Fonts(x)
   Next

   Check1.Caption = "Align Scrollbar to Left"
   Check2.Caption = "Align Text to Right"
   
End Sub


Private Sub Check1_Click()
   
  'Since the values of the alignment Enum
  'correspond to the two values of a checkbox, 
  'the checkbox value property can be used 
  'directly as the alignment style parameter.
   ListScrollAlign List1, Check1.Value
   
End Sub

Private Sub Check2_Click()
  
   ListBoxAlign List1, Check2.Value

End Sub


Private Sub ListBoxAlign(lb As ListBox, _
                         Optional ByVal Align As AlignConstants = alignLeft)

   Dim nStyle As Long

   With lb
      
      nStyle = GetWindowLong(.hwnd, GWL_EXSTYLE)
        
      Select Case Align
         Case alignRight:
            nStyle = nStyle Or WS_EX_RIGHT
         Case alignLeft:
            nStyle = nStyle And Not WS_EX_RIGHT
      End Select
      
      SetWindowLong .hwnd, GWL_EXSTYLE, nStyle
    
   End With
    
End Sub


Private Sub ListScrollAlign(lb As ListBox, _
                            Optional ByVal Align As AlignConstants = alignRight)

   Dim nStyle As Long
   
   With lb
      
      nStyle = GetWindowLong(.hwnd, GWL_EXSTYLE)
        
      Select Case Align
         Case alignRight:
            nStyle = nStyle Or WS_EX_LEFTSCROLLBAR
         Case alignLeft:
            nStyle = nStyle And Not WS_EX_LEFTSCROLLBAR
      End Select
      
      SetWindowLong .hwnd, GWL_EXSTYLE, nStyle
    
   End With
   
End Sub
 Comments

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

 

Hit Counter