Solved

ListView-Column width

Posted on 2002-07-18
6
403 Views
Last Modified: 2010-05-02
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
Comment
Question by:vim_asteya
6 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 150 total points
Comment Utility
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
 
LVL 18

Expert Comment

by:mdougan
Comment Utility
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
 
LVL 1

Author Comment

by:vim_asteya
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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
 
LVL 18

Expert Comment

by:mdougan
Comment Utility
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
 
LVL 1

Author Comment

by:vim_asteya
Comment Utility
thank u mdougan
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now