Determining the required column width of a ListView control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geert-Pieter Hof

    Determining the required column width of a ListView control

    Hello,

    I use a ListView control (report view) to show the user a summary of error
    messages. The width of the column that holds the error messages is such that
    no horizontal scrollbar is needed. However, in some cases, the length of the
    error message text is wider than the width of the column. In this case, the
    text is hyphened with three dots.

    I want to check the width of the error message text and if necessary,
    increase the width of the ListView column. By trial and error, I know that
    the amount of text that fits in a column of width 5620 twips is exactly
    "XXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXX".

    So, I created the following piece of code (the string Message holds a
    certain error message):

    With Printer

    If .TextWidth(Mess age) >
    ..TextWidth("XX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XX") Then

    ListView1.Colum nHeaders.Item(3 ).Width = .TextWidth(Mess age) /
    ..TextWidth("XX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XX") * 5620

    End If

    End With

    Although this code functions properly, I don't think it's an outstanding
    solution. Does somebody knows a better piece of code?

    Thanks in advance,

    Geert-Pieter


  • Asperamanca

    #2
    Re: Determining the required column width of a ListView control

    "Geert-Pieter Hof" <g.m.hof@wbmt.t udelft.nl.blabl abla> wrote in message news:<bgtpcu$ga r$1@news.tudelf t.nl>...[color=blue]
    > Hello,
    >
    > I use a ListView control (report view) to show the user a summary of error
    > messages. The width of the column that holds the error messages is such that
    > no horizontal scrollbar is needed. However, in some cases, the length of the
    > error message text is wider than the width of the column. In this case, the
    > text is hyphened with three dots.
    >
    > I want to check the width of the error message text and if necessary,
    > increase the width of the ListView column. By trial and error, I know that
    > the amount of text that fits in a column of width 5620 twips is exactly
    > "XXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXX".
    >
    > So, I created the following piece of code (the string Message holds a
    > certain error message):
    >
    > With Printer
    >
    > If .TextWidth(Mess age) >
    > .TextWidth("XXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX X") Then
    >
    > ListView1.Colum nHeaders.Item(3 ).Width = .TextWidth(Mess age) /
    > .TextWidth("XXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX X") * 5620
    >
    > End If
    >
    > End With
    >
    > Although this code functions properly, I don't think it's an outstanding
    > solution. Does somebody knows a better piece of code?
    >
    > Thanks in advance,
    >
    > Geert-Pieter[/color]

    You can use the TextWidth function of some controls to "test" the true
    width of a certain text. Don't forget to assign the same font that you
    currently use in the listview.
    I use this in a loop over all my texts of a column, then select the
    widest text, add some pixels for good measure, and resize the column
    to that.

    Robert

    Comment

    • kharma
      New Member
      • Feb 2006
      • 1

      #3
      Use this

      Private Declare Function SendMessage Lib "user32" Alias _
      "SendMessag eA" (ByVal hWnd As Long, ByVal wMsg As Long, _
      ByVal wParam As Long, lParam As Any) As Long

      Private Const LVM_FIRST = &H1000
      Public Sub LV_AutoSizeColu mn(LV As ListView, Optional Column _
      As ColumnHeader = Nothing)

      Dim C As ColumnHeader
      If Column Is Nothing Then
      For Each C In LV.ColumnHeader s
      SendMessage LV.hWnd, LVM_FIRST + 30, C.Index - 1, -1
      Next
      Else
      SendMessage LV.hWnd, LVM_FIRST + 30, Column.Index - 1, -1
      End If
      LV.Refresh
      End Sub

      Comment

      Working...