Results 1 to 12 of 12

Thread: [RESOLVED] Listbox Column Width ?

  1. #1

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,663

    Resolved [RESOLVED] Listbox Column Width ?

    Does anyone if there are any API's or a workaround to set the Column Width of a standard ListBox with the Column property >0 ?
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Listbox Column Width ?

    Change the columns property. The columns will be list.width/number of columns.
    So lets say you have a list box that is 300 pixels wide and you want your columns to be 150 pixels wide. Set the columns=2 and you will have two visible columns of 150 pixels additional columns will also be 150 pixels but only 2 will be visible at any given time and you will get a scrol bar if more than two need to be drawn.

    Set it to 1 to get the column the full width of the list and only 1 visible at a time

    If you want the columns to be a different size from one another then you should be using something other than a list box such as a listview or one of the grid controls which give you more options

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Listbox Column Width ?

    standard ListBox with the Column property >0 ?
    Since when has the standard listbox had more than one column?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Listbox Column Width ?

    I actually weaned myself off the listbox a few years ago and started using listview, but here's my standard listbox column setter:

    Code:
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Public Sub SetTabStopsInListbox(lst As ListBox, ColWidths() As Long)
        Const LB_SETTABSTOPS = &H192
        ' A character is approximately 4 "width" units.
        ' ColWidths() is in character widths of some standard character.
        ' ColWidths() can be either zero or one based.
        ' SADLY: This does not work for listboxes with the style set to checkbox.
        Dim NumCols As Long
        '
        NumCols = UBound(ColWidths) - LBound(ColWidths) + 1 ' Calculate the number of columns.
        SendMessage lst.hWnd, LB_SETTABSTOPS, 0&, ByVal 0& ' Clear any existing tabs.
        SendMessage lst.hWnd, LB_SETTABSTOPS, NumCols, ColWidths(LBound(ColWidths)) ' Set new tabs.
    End Sub
    Just throw it in some all purpose module and play with it.

    Enjoy.

    p.s. I just thought I'd add that you add strings to the listbox with embedded vbTab characters to jump to different columns after calling this procedure.
    Last edited by Elroy; Oct 17th, 2014 at 01:58 PM.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Listbox Column Width ?

    Quote Originally Posted by techgnome View Post
    Since when has the standard listbox had more than one column?

    -tg
    Whenever you set the .Columns property to a non 0 value

  6. #6

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,663

    Re: Listbox Column Width ?

    Quote Originally Posted by DataMiser View Post
    Change the columns property. The columns will be list.width/number of columns.
    So lets say you have a list box that is 300 pixels wide and you want your columns to be 150 pixels wide. Set the columns=2 and you will have two visible columns of 150 pixels additional columns will also be 150 pixels but only 2 will be visible at any given time and you will get a scrol bar if more than two need to be drawn.
    Sorry but this doesn't seem to be correct. [list.width/number of columns] Widening the Width of the listbox has No effect on its appearance!



    Quote Originally Posted by Elroy View Post
    Code:
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Public Sub SetTabStopsInListbox(lst As ListBox, ColWidths() As Long)
        Const LB_SETTABSTOPS = &H192
        ' A character is approximately 4 "width" units.
        ' ColWidths() is in character widths of some standard character.
        ' ColWidths() can be either zero or one based.
        ' SADLY: This does not work for listboxes with the style set to checkbox.
        Dim NumCols As Long
        '
        NumCols = UBound(ColWidths) - LBound(ColWidths) + 1 ' Calculate the number of columns.
        SendMessage lst.hWnd, LB_SETTABSTOPS, 0&, ByVal 0& ' Clear any existing tabs.
        SendMessage lst.hWnd, LB_SETTABSTOPS, NumCols, ColWidths(LBound(ColWidths)) ' Set new tabs.
    End Sub
    This doesn't seem to be doing anything either?
    I believe the input of the listbox.additem must be in the form of listbox.additem "item" & vbTab & "item2" to get this working?
    But this is no good to me...
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Listbox Column Width ?

    Quote Originally Posted by some1uk03 View Post
    Sorry but this doesn't seem to be correct. [list.width/number of columns] Widening the Width of the listbox has No effect on its appearance!
    Yes it does.

    Try this, create a new project, add a listbox set the columns to 2 and a command button then place the following code behind the form
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        List1.Width = 5000
    End Sub
    
    Private Sub Form_Load()
        List1.Height = 3000
        List1.Width = 3000
        Dim x As Integer
        For x = 1 To 40
            List1.AddItem "List Item " & CStr(x)
        Next
    End Sub
    When you run the code the form loads and you will see a populated listbox with 2 columns visible. More columns will exist to the right due to the height of the list vs the number of items in the list

    When you click the button the list box expands in width. You will still have 2 columns visible and more columns to the right. Those two visible columns will now be wider than they were before as they use the size of the list box/number of columns to determine their width.

    Name:  Image1.jpg
Views: 2452
Size:  36.4 KB

    Notice how the columns are 1/2 the width of the listbox in both cases
    If you did not see this behavior then show us what you did to get a different behavior
    Last edited by DataMiser; Oct 17th, 2014 at 06:19 PM.

  8. #8

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,663

    Re: Listbox Column Width ?

    AAhaaa...

    Just realised that mine didn't work cos I was setting the width of the listbox after it populated the items :/
    It seems you gotta set the width before you add the items and it all works perfect

    Thanks
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Listbox Column Width ?

    Quote Originally Posted by some1uk03 View Post
    Just realised that mine didn't work cos I was setting the width of the listbox after it populated the items :/
    It seems you gotta set the width before you add the items and it all works perfect
    Hmm, no such issue here.

    Perhaps you mean Columns and not Width?

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [RESOLVED] Listbox Column Width ?

    Ahhh yes, sorry. You do have to set the column widths BEFORE you start populating it. I usually make those calls at the top of the Form_Load procedure. Again though, it's a bit of a PITA to learn, but the listview is WAY more powerful than the listbox. Once you get into it, ask me for the API calls to tell which row and column of a listview were clicked. It's really cool that you can identify the exact cell that was clicked in a listview. Just as an FYI, the listview is part of the standard VB6 package, but it is in one of the additional OCX libraries that you have to reference.

    Elroy

  11. #11
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Listbox Column Width ?

    Ahhh, and I see we're talking about two different things too. I'm thinking of columns in terms of different "Fields" of the same record. Others are thinking of columns in terms of a single field being listed, but wrapped into two columns. Two somewhat different concepts.

  12. #12
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Listbox Column Width ?

    Quote Originally Posted by Elroy View Post
    Ahhh, and I see we're talking about two different things too. I'm thinking of columns in terms of different "Fields" of the same record. Others are thinking of columns in terms of a single field being listed, but wrapped into two columns. Two somewhat different concepts.
    Yeah, I was thinking the same... once I saw the reply and examples from DM, it became clear.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width