Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listview resizing question 1

Status
Not open for further replies.

mattKnight

Programmer
May 10, 2002
6,222
0
36
GB
I have a routine to resize the width of the columns in a listview control (.view = lvwReport).

My problem is that when it runs the columns resize, but I get a horizontal scroll bar. What I want is the columns to fit the available width exactly! ( no scrolling etc)

Code:
Dim sngWidth As Single
Dim intCol As Integer
Dim sngUsedWidth As Single
Dim sngLastcol As Single

sngWidth = lvwEnds.Width

    With lvwEnds.ColumnHeaders
        
        intCol = CInt(sngWidth / 8)
        
        .Item("EndName").Width = CInt(intCol * 3)
        Debug.Print CInt(intCol * 3); " "; .Item("EndName").Width
        sngUsedWidth = sngUsedWidth + .Item("EndName").Width
        
        .Item("Thread").Width = CInt(intCol * 2)
        Debug.Print CInt(intCol * 2); " "; .Item("Thread").Width
        sngUsedWidth = sngUsedWidth + .Item("Thread").Width
        
        .Item("Material").Width = CInt(intCol * 2)
        Debug.Print CInt(intCol * 2); " "; .Item("Material").Width
        sngUsedWidth = sngUsedWidth + .Item("Material").Width
        
        sngLastcol = sngWidth - sngUsedWidth
        .Item("Revision").Width = sngLastcol
        
        Debug.Print sngLastcol; " "; .Item("Revision").Width
        Debug.Print lvwEnds.Width; " "; sngUsedWidth + .Item("Revision").Width
    End With

Results from running (cut & pasted from immediate window)
Code:
 1986   1985.953 
 1324   1323.78 
 1324   1323.78 
 661.4883   661.6063 
 5295   5295.118

and as a final observation, if I remove the Cint() from the system , the results are unchanged.

I seems to me to be some obscure (ish) rounding / conversion issue between integer and single.

Does anyone have an idea of a work around or fix? my Platform is W2k SP3, vb6 sp5 and I am using the version of the control shipped with SP4 (latest revision shown on my machine).

All help gratefully received

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Dear Matt,

Try this API Substituted function, created by Randy Birch at the
'=======================================================
' Copyright ©1996-2003 VBnet, Randy Birch, All Rights Reserved.

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const LVM_SETCOLUMNWIDTH As Long = (LVM_FIRST + 30)
Private Const LVSCW_AUTOSIZE As Long = -1
Private Const LVSCW_AUTOSIZE_USEHEADER As Long = -2

Private Sub lvAutosizeControl(lv As ListView)

Dim col2adjust As Long

'/* Size each column based on the maximum of
'/* EITHER the columnheader text width, or,
'/* if the items below it are wider, the
'/* widest list item in the column
For col2adjust = 0 To lv.ColumnHeaders.Count - 1

Call SendMessage(lv.hwnd, _
LVM_SETCOLUMNWIDTH, _
col2adjust, _
ByVal LVSCW_AUTOSIZE_USEHEADER)

Next


End Sub
'=======================================================
Call lvAutosizeControl(Form1.ListView1)
'=======================================================

Hope it helped you.
Best Regards,
Praveen Menon

All the Best
Praveen Menon
pcmin@rediffmail.com
 
Praveen, welcome back after a long time.

The LVM_SETCOLUMNWIDTH solution works very fine and I also thought of posting this solution but unfortunately, it does not exactly solves Matt's problem. It fits the columns to the width of their contents. (which is equivalent to pressing [Ctrl] + [Numpad Plus] key in a listview in report view.)
What Matt wants is to proportionally adjust column width when the list view is sized.

Matt, here is a solution to your problem but it works properly only with version 6 listview control as I mentioned in thread222-577105.
Start a new project, add a (version 6) listview to the form, name it lvwEnds and paste the following code.
___
Code:
Option Explicit
Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
Private Sub Form_Load()
    lvwEnds.View = lvwReport
    Dim N As Integer, V
    V = Split(" EndName Thread Material Revision")
    For N = 1 To 4 'add sample columns
        lvwEnds.ColumnHeaders.Add , V(N), V(N)
    Next
    For N = 1 To 12 'add sample items
        lvwEnds.ListItems.Add , , MonthName(N)
    Next
End Sub

Private Sub Form_Resize()
    lvwEnds.Move 0, 0, ScaleWidth, ScaleHeight
    Dim Widths As Variant, N As Integer, R As RECT
    Widths = Split(" 3 2 2 1") 'Proportional widths
    'get the client width of list view excluding a possible scrollbar
    GetClientRect lvwEnds.hwnd, R
    'Convert client area width to twips
    R.Right = R.Right * Screen.TwipsPerPixelX
    For N = 1 To 4 'set column widths
        lvwEnds.ColumnHeaders(N).Width = R.Right * Widths(N) / 8
    Next
End Sub
___
Run the program and resize your form to see how columns are sized proportionally
 
Hypetia

Thats the solution! Thank you (plus *)

I had assumed (ass out of u and me) that lvwend.width would give the internal width.

Now you show me how its obvious where I was going wrong

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Praveen

Thanks for the thoughts, I am sure that code wsill be useful somewhere!

Its nice to see you back here

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top