Home All Groups Group Topic Archive Search About

Autosizing ListView columns

Author
22 Sep 2005 4:45 PM
Bryan Dickerson
I have an old piece of code that I use to automagically resize the columns
in a listview box that evidently is either outdated or has some quirk that
if I call the routine before the actual display happens, it's as if I didn't
call the routine at all.

The code is pretty simple:
------------------------------------------------------------
Private Declare Function SendMessageLong Lib "user32.dll" Alias
"SendMessageA" _
                        (ByVal hwnd As Long, ByVal wMsg As Long, _
                         ByVal wParam As Long, ByVal lParam As Long) As Long

Public Enum LvCW_Styles
   LvCW_AUTOSIZE = -1
   LvCW_AUTOSIZE_USEHEADER = -2
End Enum

Private Const LVM_FIRST As Long = &H1000
Private Const LVM_SETCOLUMNWIDTH As Long = LVM_FIRST + 30

Private Sub SetColWidth(ByVal ColumnIndex As Long, ByVal Style As
LvCW_Styles)
   With ListView
      If .View = lvwReport Then
         If ColumnIndex >= 1 And ColumnIndex <= .ColumnHeaders.Count Then _
            Call SendMessageLong(.hwnd, LVM_SETCOLUMNWIDTH, ColumnIndex - 1,
ByVal Style)
      End If
   End With
End Sub

Public Sub SetAllColWidths(ByVal Style As LvCW_Styles)
   Dim iCol As Integer
   Dim iCnt As Integer

   iCnt = ListView.ColumnHeaders.Count
   With ListView
      For iCol = 1 To iCnt
         SetColWidth iCol, Style
      Next iCol
   End With
End Sub

------------------------------------------------------------

The last routine "SetAllColWidths" is what gets called in the main program.
Is there any quirk about running before the display actually renders that I
should know about?  Or any other way of doing this that would be more
reliable??

TIA!

--
TFWBWY...A

Author
22 Sep 2005 4:56 PM
Matt Williamson
Bryan-

I use some very similar code to do the same thing. I don't see anything
obviously wrong with your code, so it has to be when you're calling it. Has
the listview been populated and had all of the column headers created prior
to your calling your code? I don't think it has anything to do with the
display. I'm fairly certain I call the autosize code prior to making the LV
visible in my apps.

HTH

Matt

Show quoteHide quote
"Bryan Dickerson" <txprp***@netscape.net> wrote in message
news:egEPTU5vFHA.2064@TK2MSFTNGP09.phx.gbl...
>I have an old piece of code that I use to automagically resize the columns
>in a listview box that evidently is either outdated or has some quirk that
>if I call the routine before the actual display happens, it's as if I
>didn't call the routine at all.
>
> The code is pretty simple:
> ------------------------------------------------------------
> Private Declare Function SendMessageLong Lib "user32.dll" Alias
> "SendMessageA" _
>                        (ByVal hwnd As Long, ByVal wMsg As Long, _
>                         ByVal wParam As Long, ByVal lParam As Long) As
> Long
>
> Public Enum LvCW_Styles
>   LvCW_AUTOSIZE = -1
>   LvCW_AUTOSIZE_USEHEADER = -2
> End Enum
>
> Private Const LVM_FIRST As Long = &H1000
> Private Const LVM_SETCOLUMNWIDTH As Long = LVM_FIRST + 30
>
> Private Sub SetColWidth(ByVal ColumnIndex As Long, ByVal Style As
> LvCW_Styles)
>   With ListView
>      If .View = lvwReport Then
>         If ColumnIndex >= 1 And ColumnIndex <= .ColumnHeaders.Count Then _
>            Call SendMessageLong(.hwnd, LVM_SETCOLUMNWIDTH, ColumnIndex -
> 1, ByVal Style)
>      End If
>   End With
> End Sub
>
> Public Sub SetAllColWidths(ByVal Style As LvCW_Styles)
>   Dim iCol As Integer
>   Dim iCnt As Integer
>
>   iCnt = ListView.ColumnHeaders.Count
>   With ListView
>      For iCol = 1 To iCnt
>         SetColWidth iCol, Style
>      Next iCol
>   End With
> End Sub
>
> ------------------------------------------------------------
>
> The last routine "SetAllColWidths" is what gets called in the main
> program. Is there any quirk about running before the display actually
> renders that I should know about?  Or any other way of doing this that
> would be more reliable??
>
> TIA!
>
> --
> TFWBWY...A
>
Author
22 Sep 2005 5:04 PM
Bryan Dickerson
My columnheaders are hardcoded--they don't change and the call to the
autosize code is done in the GotFocus Event (this is in a UserControl).
BTW, it's done whenever any change is made to the items in the listview, so
once the UC is displayed and in use, it works fine, it just doesn't seem to
work in the micro-seconds before.

Show quoteHide quote
"Matt Williamson" <ih8spam@spamsux.org> wrote in message
news:uUedZa5vFHA.3720@TK2MSFTNGP14.phx.gbl...
> Bryan-
>
> I use some very similar code to do the same thing. I don't see anything
> obviously wrong with your code, so it has to be when you're calling it.
> Has the listview been populated and had all of the column headers created
> prior to your calling your code? I don't think it has anything to do with
> the display. I'm fairly certain I call the autosize code prior to making
> the LV visible in my apps.
>
> HTH
>
> Matt
>
> "Bryan Dickerson" <txprp***@netscape.net> wrote in message
> news:egEPTU5vFHA.2064@TK2MSFTNGP09.phx.gbl...
>>I have an old piece of code that I use to automagically resize the columns
>>in a listview box that evidently is either outdated or has some quirk that
>>if I call the routine before the actual display happens, it's as if I
>>didn't call the routine at all.
>>
>> The code is pretty simple:
>> ------------------------------------------------------------
>> Private Declare Function SendMessageLong Lib "user32.dll" Alias
>> "SendMessageA" _
>>                        (ByVal hwnd As Long, ByVal wMsg As Long, _
>>                         ByVal wParam As Long, ByVal lParam As Long) As
>> Long
>>
>> Public Enum LvCW_Styles
>>   LvCW_AUTOSIZE = -1
>>   LvCW_AUTOSIZE_USEHEADER = -2
>> End Enum
>>
>> Private Const LVM_FIRST As Long = &H1000
>> Private Const LVM_SETCOLUMNWIDTH As Long = LVM_FIRST + 30
>>
>> Private Sub SetColWidth(ByVal ColumnIndex As Long, ByVal Style As
>> LvCW_Styles)
>>   With ListView
>>      If .View = lvwReport Then
>>         If ColumnIndex >= 1 And ColumnIndex <= .ColumnHeaders.Count Then
>> _
>>            Call SendMessageLong(.hwnd, LVM_SETCOLUMNWIDTH, ColumnIndex -
>> 1, ByVal Style)
>>      End If
>>   End With
>> End Sub
>>
>> Public Sub SetAllColWidths(ByVal Style As LvCW_Styles)
>>   Dim iCol As Integer
>>   Dim iCnt As Integer
>>
>>   iCnt = ListView.ColumnHeaders.Count
>>   With ListView
>>      For iCol = 1 To iCnt
>>         SetColWidth iCol, Style
>>      Next iCol
>>   End With
>> End Sub
>>
>> ------------------------------------------------------------
>>
>> The last routine "SetAllColWidths" is what gets called in the main
>> program. Is there any quirk about running before the display actually
>> renders that I should know about?  Or any other way of doing this that
>> would be more reliable??
>>
>> TIA!
>>
>> --
>> TFWBWY...A
>>
>
>
Author
22 Sep 2005 5:48 PM
Matt Williamson
I've never seen that behavior before. I think I'd create a new app using
that usercontrol and the minimal amount of code necessary to populate it and
do some testing. Throw a sleep or doevents loop in with the gotfocus call to
see if it's just a timing issue. What else does this usercontrol do? Does it
have any other controls in it? Is there a form and if so is autoredraw set
to true? Way to many variables without more info..

HTH

Matt

Show quoteHide quote
"Bryan Dickerson" <txprp***@netscape.net> wrote in message
news:uko12e5vFHA.2556@TK2MSFTNGP15.phx.gbl...
> My columnheaders are hardcoded--they don't change and the call to the
> autosize code is done in the GotFocus Event (this is in a UserControl).
> BTW, it's done whenever any change is made to the items in the listview,
> so once the UC is displayed and in use, it works fine, it just doesn't
> seem to work in the micro-seconds before.
>
> "Matt Williamson" <ih8spam@spamsux.org> wrote in message
> news:uUedZa5vFHA.3720@TK2MSFTNGP14.phx.gbl...
>> Bryan-
>>
>> I use some very similar code to do the same thing. I don't see anything
>> obviously wrong with your code, so it has to be when you're calling it.
>> Has the listview been populated and had all of the column headers created
>> prior to your calling your code? I don't think it has anything to do with
>> the display. I'm fairly certain I call the autosize code prior to making
>> the LV visible in my apps.
>>
>> HTH
>>
>> Matt
>>
>> "Bryan Dickerson" <txprp***@netscape.net> wrote in message
>> news:egEPTU5vFHA.2064@TK2MSFTNGP09.phx.gbl...
>>>I have an old piece of code that I use to automagically resize the
>>>columns in a listview box that evidently is either outdated or has some
>>>quirk that if I call the routine before the actual display happens, it's
>>>as if I didn't call the routine at all.
>>>
>>> The code is pretty simple:
>>> ------------------------------------------------------------
>>> Private Declare Function SendMessageLong Lib "user32.dll" Alias
>>> "SendMessageA" _
>>>                        (ByVal hwnd As Long, ByVal wMsg As Long, _
>>>                         ByVal wParam As Long, ByVal lParam As Long) As
>>> Long
>>>
>>> Public Enum LvCW_Styles
>>>   LvCW_AUTOSIZE = -1
>>>   LvCW_AUTOSIZE_USEHEADER = -2
>>> End Enum
>>>
>>> Private Const LVM_FIRST As Long = &H1000
>>> Private Const LVM_SETCOLUMNWIDTH As Long = LVM_FIRST + 30
>>>
>>> Private Sub SetColWidth(ByVal ColumnIndex As Long, ByVal Style As
>>> LvCW_Styles)
>>>   With ListView
>>>      If .View = lvwReport Then
>>>         If ColumnIndex >= 1 And ColumnIndex <= .ColumnHeaders.Count Then
>>> _
>>>            Call SendMessageLong(.hwnd, LVM_SETCOLUMNWIDTH, ColumnIndex -
>>> 1, ByVal Style)
>>>      End If
>>>   End With
>>> End Sub
>>>
>>> Public Sub SetAllColWidths(ByVal Style As LvCW_Styles)
>>>   Dim iCol As Integer
>>>   Dim iCnt As Integer
>>>
>>>   iCnt = ListView.ColumnHeaders.Count
>>>   With ListView
>>>      For iCol = 1 To iCnt
>>>         SetColWidth iCol, Style
>>>      Next iCol
>>>   End With
>>> End Sub
>>>
>>> ------------------------------------------------------------
>>>
>>> The last routine "SetAllColWidths" is what gets called in the main
>>> program. Is there any quirk about running before the display actually
>>> renders that I should know about?  Or any other way of doing this that
>>> would be more reliable??
>>>
>>> TIA!
>>>
>>> --
>>> TFWBWY...A
>>>
>>
>>
>
>