• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

ListView-Column width

hi,
i would like to know if its possible to set the column width in listview based on the max length of the item in each of the individual columns.

for example i have name and phone no in 2 columns.

if i have names as
1.asteya
2.ishwara pranidhana

i want the column width set, such that the 2nd name is visible fully.

similarly for all the columns in the list view.
and this required to be set at runtime.


asteya
0
vim_asteya
Asked:
vim_asteya
1 Solution
 
TimCotteeHead of Software ServicesCommented:
You need to use the .TextWidth method of the form:

Debug.Print Me.TextWidth("ishwara pranidhana")

For example returns the width required to display this text, iterate this for each entry in the listview and use the max value returned as the column width (add a bit for luck of course).

Dim lviWidth As ListItem
Dim sngMaxWidth As Single
For Each lviWidth In Listview1.ListItems
  If Me.TextWidth(lviWidth.Text) > sngMaxWidth Then
    sngMaxWidth = Me.TextWidth(lviWidth.Text)
  End If
Next
ListView1.Columns(1).Width = sngMaxWidth

(Check that last property as I am typing this off the top of my head and cannot remember the appropriate column width property).
0
 
mdouganCommented:
The way I am familiar with is similar to Tim's.  First,  you would fill your listview, then for each column, you would iterate through each row in the listview and store the length of the longest string (or the longest string itself).  Then, you can make a call to the GetTextMetrics API to get the MaxCharWidth:


Private Declare Function GetTextMetrics Lib "gdi32" Alias "GetTextMetricsA" (ByVal hdc As Long, lpMetrics As TEXTMETRIC) As Long
Private Type TEXTMETRIC
        tmHeight As Long
        tmAscent As Long
        tmDescent As Long
        tmInternalLeading As Long
        tmExternalLeading As Long
        tmAveCharWidth As Long
        tmMaxCharWidth As Long
        tmWeight As Long
        tmOverhang As Long
        tmDigitizedAspectX As Long
        tmDigitizedAspectY As Long
        tmFirstChar As Byte
        tmLastChar As Byte
        tmDefaultChar As Byte
        tmBreakChar As Byte
        tmItalic As Byte
        tmUnderlined As Byte
        tmStruckOut As Byte
        tmPitchAndFamily As Byte
        tmCharSet As Byte
End Type

Private Sub FillListView()
Dim x As TEXTMETRIC
    GetTextMetrics Me.hdc, x
    ListView1.ColumnHeaders(1).Width = Len("ishwara pranidhana") * x.tmMaxCharWidth
End Sub
0
 
vim_asteyaAuthor Commented:
thank u TimCottee and mdougan.

I tried tim's logic.has worked out.

i havent work with API.But would like to work with in it.

so i have to give the pts to TimCottee.
is that ok with u mdougan.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Ryan ChongCommented:
What i would suggest is:

1. Create another question named 'Point for mdougan' with certain amount of points.

or 2. Ask the Community to split this question points.
http://www.experts-exchange.com/commspt/

cheers
0
 
mdouganCommented:
No need to give me any points.  My comment is virtually the same as Tim's, though Tim's is simpler.  I provided the GetTextMetrics call as it is useful to know how much additional information you can get about the text of the object (in this case the form) whose .hDC you are passing.  I'm just happy to help.
0
 
vim_asteyaAuthor Commented:
thank u mdougan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now