?
Solved

ListView-Column width

Posted on 2002-07-18
6
Medium Priority
?
413 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 600 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 53

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…
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…
Suggested Courses

719 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