Solved

ListView-Column width

Posted on 2002-07-18
6
411 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 150 total points
ID: 7162141
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
ID: 7162178
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
ID: 7164280
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 52

Expert Comment

by:Ryan Chong
ID: 7164314
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
ID: 7165488
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
ID: 7166265
thank u mdougan
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month11 days, 3 hours left to enroll

628 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