-
Oct 17th, 2014, 11:11 AM
#1
Thread Starter
Frenzied Member
[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.
-
Oct 17th, 2014, 12:52 PM
#2
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
-
Oct 17th, 2014, 01:11 PM
#3
Re: Listbox Column Width ?
standard ListBox with the Column property >0 ?
Since when has the standard listbox had more than one column?
-tg
-
Oct 17th, 2014, 01:32 PM
#4
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.
-
Oct 17th, 2014, 02:06 PM
#5
Re: Listbox Column Width ?
Originally Posted by techgnome
Since when has the standard listbox had more than one column?
-tg
Whenever you set the .Columns property to a non 0 value
-
Oct 17th, 2014, 05:52 PM
#6
Thread Starter
Frenzied Member
Re: Listbox Column Width ?
Originally Posted by DataMiser
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!
Originally Posted by Elroy
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.
-
Oct 17th, 2014, 06:10 PM
#7
Re: Listbox Column Width ?
Originally Posted by some1uk03
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.
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.
-
Oct 17th, 2014, 06:54 PM
#8
Thread Starter
Frenzied Member
-
Oct 17th, 2014, 07:12 PM
#9
Re: Listbox Column Width ?
Originally Posted by some1uk03
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?
-
Oct 18th, 2014, 12:32 PM
#10
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
-
Oct 18th, 2014, 12:34 PM
#11
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.
-
Oct 18th, 2014, 08:28 PM
#12
Re: Listbox Column Width ?
Originally Posted by Elroy
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|