Solved

ListView-Column width

Posted on 2002-07-18
6
409 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
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 51

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to debug this code 7 64
using web browser with BING 40 132
backup program with robocopy 6 47
VBA- How to change font, color and format in outlook e-mail sent from Access 2 70
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

821 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