Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access thread: Automatically set the width of columns in a listview control


Message #1 by brian.skelton@b... on Thu, 25 Oct 2001 10:04:39
Hi Everyone



My evil project manager has forced me to use ListView activex controls for 

displaying data in my current project, rather than data sheets!



One of the fantastic things about data sheets is the ability to have the 

columns automatically resize to accomodate the data they contained. 

ListView's don't seem to have the same functionality.

So, does anyone know how to:



1)Force a ListView to auto resize.



or



2)Given a string and a point size, work out how wide a piece of text will 

be?



Many thanks



Brian
Message #2 by "Yehuda Rosenblum" <Yehuda@I...> on Thu, 25 Oct 2001 08:21:54 -0400
Brian,



Here is how to AutoResize the columns so they open to the proper width:



 For col =3D 1 To LV.ColumnHeaders.count

        maxWidth =3D 0

        If AccountForHeaders Then

            maxWidth =3D LV.Parent.TextWidth(LV.ColumnHeaders(col).Text) 

+

200

        End If

       

        For row =3D 1 To LV.ListItems.count

            If col =3D 1 Then

                cellText =3D LV.ListItems(row).Text

            Else

                cellText =3D LV.ListItems(row).ListSubItems(col - 

1).Text

            End If

           

            width =3D LV.Parent.TextWidth(cellText) + 200

           

            If width > maxWidth Then maxWidth =3D width

        Next

       

        LV.ColumnHeaders(col).width =3D maxWidth

    Next



-----Original Message-----

From: brian.skelton@b...

[mailto:brian.skelton@b...]

Sent: Thursday, October 25, 2001 6:05 AM

To: Access

Subject: [access] Automatically set the width of columns in a listview

control





Hi Everyone



My evil project manager has forced me to use ListView activex controls

for

displaying data in my current project, rather than data sheets!



One of the fantastic things about data sheets is the ability to have the



columns automatically resize to accomodate the data they contained.

ListView's don't seem to have the same functionality.

So, does anyone know how to:



1)Force a ListView to auto resize.



or



2)Given a string and a point size, work out how wide a piece of text

will

be?



Many thanks



Brian

---

You are currently subscribed to access as: yehuda@i... To

unsubscribe send a blank email to $subst('Email.Unsub')



Message #3 by brian.skelton@b... on Mon, 29 Oct 2001 09:04:14
Yehuda,



I've given this a try, and it all makes perfect sense, except for one 

thing:



I'm using Access 2000, and the Form object, which is the parent to the 

ListView, doesn't have a TextWidth method. As far as I can discover, only 

the Report object has TextWidth as one of its methods.



Can you suggest anything else?



Thanks



Brian



> Brian,

> 

> Here is how to AutoResize the columns so they open to the proper width:

> 

>  For col =3D 1 To LV.ColumnHeaders.count

>         maxWidth =3D 0

>         If AccountForHeaders Then

>             maxWidth =3D LV.Parent.TextWidth(LV.ColumnHeaders(col).Text) 



> +

> 200

>         End If

>        

>         For row =3D 1 To LV.ListItems.count

>             If col =3D 1 Then

>                 cellText =3D LV.ListItems(row).Text

>             Else

>                 cellText =3D LV.ListItems(row).ListSubItems(col - 

> 1).Text

>             End If

>            

>             width =3D LV.Parent.TextWidth(cellText) + 200

>            

>             If width > maxWidth Then maxWidth =3D width

>         Next

>        

>         LV.ColumnHeaders(col).width =3D maxWidth

>     Next

> 

> -----Original Message-----

> From: brian.skelton@b...

> [mailto:brian.skelton@b...]

> Sent: Thursday, October 25, 2001 6:05 AM

> To: Access

> Subject: [access] Automatically set the width of columns in a listview

> control

> 

> 

> Hi Everyone

> 

> My evil project manager has forced me to use ListView activex controls

> for

> displaying data in my current project, rather than data sheets!

> 

> One of the fantastic things about data sheets is the ability to have the

> 

> columns automatically resize to accomodate the data they contained.

> ListView's don't seem to have the same functionality.

> So, does anyone know how to:

> 

> 1)Force a ListView to auto resize.

> 

> or

> 

> 2)Given a string and a point size, work out how wide a piece of text

> will

> be?

> 

> Many thanks

> 

> Brian

Message #4 by "Yehuda Rosenblum" <Yehuda@I...> on Mon, 29 Oct 2001 16:40:20 -0500
I don't know.  I have only used ListView from VB.



		-----Original Message-----

		From: brian.skelton@b...

		Sent: Mon 10/29/2001 4:04 AM

		To: Access

		Cc:

		Subject: [access] RE: Automatically set the width of

columns in a listview control
Message #5 by brian.skelton@b... on Tue, 30 Oct 2001 16:36:34
> Hi Everyone

> 

For anyone else with a similar problem, here's the solution I found after 

much internet trawling:



'Declare the API function and some constants

Declare Function SendMessage Lib "user32.dll" _

  Alias "SendMessageA" (ByVal hWnd As Long, _

  ByVal Msg As Long, ByVal wParam As Long, _

  ByVal lParam As Long) As Long

Public Const LVM_FIRST = &H1000

Public Const LVM_SETCOLUMNWIDTH = (LVM_FIRST + 30)

'Use this as a parameter to autosize on column data only

Public Const LVSCW_AUTOSIZE = -1

'Use this as a parameter to autosize on column data and Column titles

Public Const LVSCW_AUTOSIZE_USEHEADER = -2



Public Sub AutoSizeListView(lstvwToSize As Object, bolAccountForHeaders As 

Boolean)

Dim intCol As Integer

Dim intRow As Integer

Dim lngMaxWidth As Long

Dim lngWidth As Long

Dim lngCurrentWidth As Long

Dim strCellText As String

On Error GoTo errAutoSizeListView



    For intCol = 1 To lstvwToSize.ColumnHeaders.Count

        'Find current column width

        lngCurrentWidth = lstvwToSize.ColumnHeaders(intCol).Width

        'If column width is zero, this is a hidden column

        If lngCurrentWidth <> 0 Then

            'Use magic SendMessage function to size columns!

            If bolAccountForHeaders Then

                SendMessage lstvwToSize.hWnd, LVM_SETCOLUMNWIDTH, intCol, 

LVSCW_AUTOSIZE_USEHEADER

            Else

                SendMessage lstvwToSize.hWnd, LVM_SETCOLUMNWIDTH, intCol, 

LVSCW_AUTOSIZE

            End If

        End If

    Next



exitAutoSizeListView:

    Exit Sub

errAutoSizeListView:

    MsgBox Err.Description

    Resume exitAutoSizeListView

    

End Sub





So obvious really! ;-)



-BDS


  Return to Index