Home All Groups Group Topic Archive Search About
Author
13 Apr 2005 5:47 PM
J L
When I fill a listview, I resize the columns to fit the data. I need
to know if the data will fit vertically or if there will be a vertical
scroll bar. I need to know this so I can allow for it on the overall
size of the listview.

My question therefore is, how can I tell if the items I have added
will fit in the listview at its given height?

A secondary one, just for interest sake...is there a way to determine
the exact heght needed to hold the items?

TIA,
John

Author
13 Apr 2005 6:20 PM
Jason Newell
JL,
    You do know that by setting each ColumnHeader.Width = -2, the ListView
will automatically size the Columns to with width of the data.      If this
does not meet your needs, you'll have to use windows api to achieve what
your wanting.

Windows API Messages you might consider

LVM_APPROXIMATEVIEWRECT
LVM_GETCOUNTPERPAGE
LVM_GETWORKAREAS
LVM_GETITEMRECT

Jason Newell, MCAD
Software Engineer

J L wrote:
Show quoteHide quote
> When I fill a listview, I resize the columns to fit the data. I need
> to know if the data will fit vertically or if there will be a vertical
> scroll bar. I need to know this so I can allow for it on the overall
> size of the listview.
>
> My question therefore is, how can I tell if the items I have added
> will fit in the listview at its given height?
>
> A secondary one, just for interest sake...is there a way to determine
> the exact heght needed to hold the items?
>
> TIA,
> John
Author
13 Apr 2005 7:46 PM
J L
Hi Jason,
I know about the width. It is the height that I want to know. Will
there be a scroll bar or not given the existing height of the listview
and the number of items I have added.

Thanks for the response.

John

On Wed, 13 Apr 2005 13:20:40 -0500, Jason Newell <nospam@nospam.com>
wrote:

Show quoteHide quote
>JL,
>    You do know that by setting each ColumnHeader.Width = -2, the ListView
>will automatically size the Columns to with width of the data.      If this
>does not meet your needs, you'll have to use windows api to achieve what
>your wanting.
>
>Windows API Messages you might consider
>
>LVM_APPROXIMATEVIEWRECT
>LVM_GETCOUNTPERPAGE
>LVM_GETWORKAREAS
>LVM_GETITEMRECT
>
>Jason Newell, MCAD
>Software Engineer
>
>J L wrote:
>> When I fill a listview, I resize the columns to fit the data. I need
>> to know if the data will fit vertically or if there will be a vertical
>> scroll bar. I need to know this so I can allow for it on the overall
>> size of the listview.
>>
>> My question therefore is, how can I tell if the items I have added
>> will fit in the listview at its given height?
>>
>> A secondary one, just for interest sake...is there a way to determine
>> the exact heght needed to hold the items?
>>
>> TIA,
>> John
Author
13 Apr 2005 9:57 PM
Jason Newell
JL,
    Take a look at the following code.  It would appear that the
LVM_GETCOUNTPERPAGE message is what you're looking for.  Let me know how
it goes.

Jason Newell, MCAD
Software Engineer



Public Class Form1
     Inherits System.Windows.Forms.Form

     Private Const LVM_GETCOUNTPERPAGE As Integer = &H1000 + 40

     Declare Auto Function SendMessage Lib "user32" (ByVal hWnd As
IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As
Integer) As Integer
#Region " Windows Form Designer generated code "

     Public Sub New()
         MyBase.New()

         'This call is required by the Windows Form Designer.
         InitializeComponent()

         'Add any initialization after the InitializeComponent() call

     End Sub

     'Form overrides dispose to clean up the component list.
     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
         If disposing Then
             If Not (components Is Nothing) Then
                 components.Dispose()
             End If
         End If
         MyBase.Dispose(disposing)
     End Sub

     'Required by the Windows Form Designer
     Private components As System.ComponentModel.IContainer

     'NOTE: The following procedure is required by the Windows Form Designer
     'It can be modified using the Windows Form Designer.
     'Do not modify it using the code editor.
     Friend WithEvents ListView1 As System.Windows.Forms.ListView
     Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader
     <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
         Me.ListView1 = New System.Windows.Forms.ListView
         Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader
         Me.SuspendLayout()
         '
         'ListView1
         '
         Me.ListView1.Columns.AddRange(New
System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1})
         Me.ListView1.Location = New System.Drawing.Point(32, 24)
         Me.ListView1.Name = "ListView1"
         Me.ListView1.Size = New System.Drawing.Size(184, 120)
         Me.ListView1.TabIndex = 0
         Me.ListView1.View = System.Windows.Forms.View.Details
         '
         'Form1
         '
         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
         Me.ClientSize = New System.Drawing.Size(292, 266)
         Me.Controls.Add(Me.ListView1)
         Me.Name = "Form1"
         Me.Text = "Form1"
         Me.ResumeLayout(False)

     End Sub

#End Region

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
         Me.ListView1.View = View.Details
         MessageBox.Show(SendMessage(Me.ListView1.Handle,
LVM_GETCOUNTPERPAGE, 0, 0))
     End Sub

End Class

J L wrote:
Show quoteHide quote
> Hi Jason,
> I know about the width. It is the height that I want to know. Will
> there be a scroll bar or not given the existing height of the listview
> and the number of items I have added.
>
> Thanks for the response.
>
> John
>
> On Wed, 13 Apr 2005 13:20:40 -0500, Jason Newell <nospam@nospam.com>
> wrote:
>
>
>>JL,
>>    You do know that by setting each ColumnHeader.Width = -2, the ListView
>>will automatically size the Columns to with width of the data.      If this
>>does not meet your needs, you'll have to use windows api to achieve what
>>your wanting.
>>
>>Windows API Messages you might consider
>>
>>LVM_APPROXIMATEVIEWRECT
>>LVM_GETCOUNTPERPAGE
>>LVM_GETWORKAREAS
>>LVM_GETITEMRECT
>>
>>Jason Newell, MCAD
>>Software Engineer
>>
>>J L wrote:
>>
>>>When I fill a listview, I resize the columns to fit the data. I need
>>>to know if the data will fit vertically or if there will be a vertical
>>>scroll bar. I need to know this so I can allow for it on the overall
>>>size of the listview.
>>>
>>>My question therefore is, how can I tell if the items I have added
>>>will fit in the listview at its given height?
>>>
>>>A secondary one, just for interest sake...is there a way to determine
>>>the exact heght needed to hold the items?
>>>
>>>TIA,
>>>John
>
>
Author
14 Apr 2005 12:51 AM
Dennis
Check out measureString method of a Graphics object;

Dim fmt As StringFormat = New StringFormat
_(StringFormatFlags.MeasureTrailingSpaces Or StringFormatFlags.LineLimit Or _
StringFormatFlags.FitBlackBox)

h = CInt(g.MeasureString(myText, myListView.Font, myListView.Width,
fmt).Height)

This will return the height required for myText to fit into the listview
width:


Show quoteHide quote
"J L" wrote:

> Hi Jason,
> I know about the width. It is the height that I want to know. Will
> there be a scroll bar or not given the existing height of the listview
> and the number of items I have added.
>
> Thanks for the response.
>
> John
>
> On Wed, 13 Apr 2005 13:20:40 -0500, Jason Newell <nospam@nospam.com>
> wrote:
>
> >JL,
> >    You do know that by setting each ColumnHeader.Width = -2, the ListView
> >will automatically size the Columns to with width of the data.      If this
> >does not meet your needs, you'll have to use windows api to achieve what
> >your wanting.
> >
> >Windows API Messages you might consider
> >
> >LVM_APPROXIMATEVIEWRECT
> >LVM_GETCOUNTPERPAGE
> >LVM_GETWORKAREAS
> >LVM_GETITEMRECT
> >
> >Jason Newell, MCAD
> >Software Engineer
> >
> >J L wrote:
> >> When I fill a listview, I resize the columns to fit the data. I need
> >> to know if the data will fit vertically or if there will be a vertical
> >> scroll bar. I need to know this so I can allow for it on the overall
> >> size of the listview.
> >>
> >> My question therefore is, how can I tell if the items I have added
> >> will fit in the listview at its given height?
> >>
> >> A secondary one, just for interest sake...is there a way to determine
> >> the exact heght needed to hold the items?
> >>
> >> TIA,
> >> John
>
>
Author
14 Apr 2005 1:32 PM
J L
Hi Dennis,
That looks like what I need but I do not know where to get the
graphics object g?

What I have is a subroutine that gets passed a reference to the
listview. I am then filling and sizing the listview in that
subroutine.

Sub FillListView( ByRef theListView As ListView)

Thanks,
John

On Wed, 13 Apr 2005 17:51:02 -0700, Dennis
<Den***@discussions.microsoft.com> wrote:

Show quoteHide quote
>Check out measureString method of a Graphics object;
>
>Dim fmt As StringFormat = New StringFormat
>_(StringFormatFlags.MeasureTrailingSpaces Or StringFormatFlags.LineLimit Or _
>StringFormatFlags.FitBlackBox)
>
>h = CInt(g.MeasureString(myText, myListView.Font, myListView.Width,
>fmt).Height)
>
>This will return the height required for myText to fit into the listview
>width:
>
>
>"J L" wrote:
>
>> Hi Jason,
>> I know about the width. It is the height that I want to know. Will
>> there be a scroll bar or not given the existing height of the listview
>> and the number of items I have added.
>>
>> Thanks for the response.
>>
>> John
>>
>> On Wed, 13 Apr 2005 13:20:40 -0500, Jason Newell <nospam@nospam.com>
>> wrote:
>>
>> >JL,
>> >    You do know that by setting each ColumnHeader.Width = -2, the ListView
>> >will automatically size the Columns to with width of the data.      If this
>> >does not meet your needs, you'll have to use windows api to achieve what
>> >your wanting.
>> >
>> >Windows API Messages you might consider
>> >
>> >LVM_APPROXIMATEVIEWRECT
>> >LVM_GETCOUNTPERPAGE
>> >LVM_GETWORKAREAS
>> >LVM_GETITEMRECT
>> >
>> >Jason Newell, MCAD
>> >Software Engineer
>> >
>> >J L wrote:
>> >> When I fill a listview, I resize the columns to fit the data. I need
>> >> to know if the data will fit vertically or if there will be a vertical
>> >> scroll bar. I need to know this so I can allow for it on the overall
>> >> size of the listview.
>> >>
>> >> My question therefore is, how can I tell if the items I have added
>> >> will fit in the listview at its given height?
>> >>
>> >> A secondary one, just for interest sake...is there a way to determine
>> >> the exact heght needed to hold the items?
>> >>
>> >> TIA,
>> >> John
>>
>>
Author
14 Apr 2005 3:45 PM
Supra
http://www.vbdotnetheaven.com/

J L wrote:

Show quoteHide quote
>Hi Dennis,
>That looks like what I need but I do not know where to get the
>graphics object g?
>
>What I have is a subroutine that gets passed a reference to the
>listview. I am then filling and sizing the listview in that
>subroutine.
>
>Sub FillListView( ByRef theListView As ListView)
>
>Thanks,
>John
>
>On Wed, 13 Apr 2005 17:51:02 -0700, Dennis
><Den***@discussions.microsoft.com> wrote:
>

>
>>Check out measureString method of a Graphics object;
>>
>>Dim fmt As StringFormat = New StringFormat
>>_(StringFormatFlags.MeasureTrailingSpaces Or StringFormatFlags.LineLimit Or _
>>StringFormatFlags.FitBlackBox)
>>
>>h = CInt(g.MeasureString(myText, myListView.Font, myListView.Width,
>>fmt).Height)
>>
>>This will return the height required for myText to fit into the listview
>>width:
>>
>>
>>"J L" wrote:
>>
>>   
>>
>>>Hi Jason,
>>>I know about the width. It is the height that I want to know. Will
>>>there be a scroll bar or not given the existing height of the listview
>>>and the number of items I have added.
>>>
>>>Thanks for the response.
>>>
>>>John
>>>
>>>On Wed, 13 Apr 2005 13:20:40 -0500, Jason Newell <nospam@nospam.com>
>>>wrote:
>>>
>>>     
>>>
>>>>JL,
>>>>    You do know that by setting each ColumnHeader.Width = -2, the ListView
>>>>will automatically size the Columns to with width of the data.      If this
>>>>does not meet your needs, you'll have to use windows api to achieve what
>>>>your wanting.
>>>>
>>>>Windows API Messages you might consider
>>>>
>>>>LVM_APPROXIMATEVIEWRECT
>>>>LVM_GETCOUNTPERPAGE
>>>>LVM_GETWORKAREAS
>>>>LVM_GETITEMRECT
>>>>
>>>>Jason Newell, MCAD
>>>>Software Engineer
>>>>
>>>>J L wrote:
>>>>       
>>>>
>>>>>When I fill a listview, I resize the columns to fit the data. I need
>>>>>to know if the data will fit vertically or if there will be a vertical
>>>>>scroll bar. I need to know this so I can allow for it on the overall
>>>>>size of the listview.
>>>>>
>>>>>My question therefore is, how can I tell if the items I have added
>>>>>will fit in the listview at its given height?
>>>>>
>>>>>A secondary one, just for interest sake...is there a way to determine
>>>>>the exact heght needed to hold the items?
>>>>>
>>>>>TIA,
>>>>>John
>>>>>         
>>>>>
>>>     
>>>
>

>
Author
14 Apr 2005 10:59 PM
Dennis
Sorry, g can be obtained from:

dim g as graphics = myListView.CreateGraphics()

Note, be sure to dispose of g (g.dispose) when you're done.

Show quoteHide quote
"J L" wrote:

> Hi Dennis,
> That looks like what I need but I do not know where to get the
> graphics object g?
>
> What I have is a subroutine that gets passed a reference to the
> listview. I am then filling and sizing the listview in that
> subroutine.
>
> Sub FillListView( ByRef theListView As ListView)
>
> Thanks,
> John
>
> On Wed, 13 Apr 2005 17:51:02 -0700, Dennis
> <Den***@discussions.microsoft.com> wrote:
>
> >Check out measureString method of a Graphics object;
> >
> >Dim fmt As StringFormat = New StringFormat
> >_(StringFormatFlags.MeasureTrailingSpaces Or StringFormatFlags.LineLimit Or _
> >StringFormatFlags.FitBlackBox)
> >
> >h = CInt(g.MeasureString(myText, myListView.Font, myListView.Width,
> >fmt).Height)
> >
> >This will return the height required for myText to fit into the listview
> >width:
> >
> >
> >"J L" wrote:
> >
> >> Hi Jason,
> >> I know about the width. It is the height that I want to know. Will
> >> there be a scroll bar or not given the existing height of the listview
> >> and the number of items I have added.
> >>
> >> Thanks for the response.
> >>
> >> John
> >>
> >> On Wed, 13 Apr 2005 13:20:40 -0500, Jason Newell <nospam@nospam.com>
> >> wrote:
> >>
> >> >JL,
> >> >    You do know that by setting each ColumnHeader.Width = -2, the ListView
> >> >will automatically size the Columns to with width of the data.      If this
> >> >does not meet your needs, you'll have to use windows api to achieve what
> >> >your wanting.
> >> >
> >> >Windows API Messages you might consider
> >> >
> >> >LVM_APPROXIMATEVIEWRECT
> >> >LVM_GETCOUNTPERPAGE
> >> >LVM_GETWORKAREAS
> >> >LVM_GETITEMRECT
> >> >
> >> >Jason Newell, MCAD
> >> >Software Engineer
> >> >
> >> >J L wrote:
> >> >> When I fill a listview, I resize the columns to fit the data. I need
> >> >> to know if the data will fit vertically or if there will be a vertical
> >> >> scroll bar. I need to know this so I can allow for it on the overall
> >> >> size of the listview.
> >> >>
> >> >> My question therefore is, how can I tell if the items I have added
> >> >> will fit in the listview at its given height?
> >> >>
> >> >> A secondary one, just for interest sake...is there a way to determine
> >> >> the exact heght needed to hold the items?
> >> >>
> >> >> TIA,
> >> >> John
> >>
> >>
>
>
Author
14 Apr 2005 7:16 PM
J L
For those interested in this thread, here is what I have found that
seems to work:

Dim r As Rectangle = theListView.Items(0).Bounds
Dim htItems As Integer
' get whole number of items that will fit
' in the listview
htItems = theListView.Height/r.Height
' allow for column headers...not sure why 2 instead of 1
If htItems < theListView.Items.Count + 2
   theListView.Width = sumOfColumnWidths + _
      (SystemInformation.Border3DSize.Width * 2) + _
      SystemInformation.VerticalScrollBarWidth
Else
   theListView.Width = sumOfColumnWidths + _
      (SystemInformation.Border3DSize.Width * 2)
End If

This does fail on the condition that the last row that fills the list
view only partially fits. It puts the vertical scrow bar in the
listview but does not increase the size. I can write code to size th e
list view to an exact row count but if this is called repeatedly, I
loose track of the initial design height...does that make any sense to
any one...the code to size it to a whole number of rows is to add
before the If...End If clause:

theListView.Height = (htItems + 1) * r.Height

(i.e. use the number of items and the height of each, allow for the
headers)


If anyone has a comment, sees an error or knows a better approach, I
would appreciate your inputs.

TIA,
John

On Wed, 13 Apr 2005 10:47:50 -0700, J L <ystem> wrote:

Show quoteHide quote
>When I fill a listview, I resize the columns to fit the data. I need
>to know if the data will fit vertically or if there will be a vertical
>scroll bar. I need to know this so I can allow for it on the overall
>size of the listview.
>
>My question therefore is, how can I tell if the items I have added
>will fit in the listview at its given height?
>
>A secondary one, just for interest sake...is there a way to determine
>the exact heght needed to hold the items?
>
>TIA,
>John
Author
14 Apr 2005 7:38 PM
Jason Newell
JL,
    Just curiuos, did sending the LVM_GETCOUNTPERPAGE meesage work for you?
  The LVM_GETCOUNTPERPAGE will tell you how man ListViewItems can be
displayed before the scrollbar appears.

MSDN Quote: "LVM_GETCOUNTPERPAGE calculates the number of items that can
fit vertically in the visible area of a list-view control when in list
or report view. Only fully visible items are counted."

Just curious.

Jason Newell, MCAD
Software Engineer

J L wrote:
Show quoteHide quote
> For those interested in this thread, here is what I have found that
> seems to work:
>
> Dim r As Rectangle = theListView.Items(0).Bounds
> Dim htItems As Integer
> ' get whole number of items that will fit
> ' in the listview
> htItems = theListView.Height/r.Height
> ' allow for column headers...not sure why 2 instead of 1
> If htItems < theListView.Items.Count + 2
>    theListView.Width = sumOfColumnWidths + _
>       (SystemInformation.Border3DSize.Width * 2) + _
>       SystemInformation.VerticalScrollBarWidth
> Else
>    theListView.Width = sumOfColumnWidths + _
>       (SystemInformation.Border3DSize.Width * 2)
> End If
>
> This does fail on the condition that the last row that fills the list
> view only partially fits. It puts the vertical scrow bar in the
> listview but does not increase the size. I can write code to size th e
> list view to an exact row count but if this is called repeatedly, I
> loose track of the initial design height...does that make any sense to
> any one...the code to size it to a whole number of rows is to add
> before the If...End If clause:
>
> theListView.Height = (htItems + 1) * r.Height
>
> (i.e. use the number of items and the height of each, allow for the
> headers)
>
>
> If anyone has a comment, sees an error or knows a better approach, I
> would appreciate your inputs.
>
> TIA,
> John
>
> On Wed, 13 Apr 2005 10:47:50 -0700, J L <ystem> wrote:
>
>
>>When I fill a listview, I resize the columns to fit the data. I need
>>to know if the data will fit vertically or if there will be a vertical
>>scroll bar. I need to know this so I can allow for it on the overall
>>size of the listview.
>>
>>My question therefore is, how can I tell if the items I have added
>>will fit in the listview at its given height?
>>
>>A secondary one, just for interest sake...is there a way to determine
>>the exact heght needed to hold the items?
>>
>>TIA,
>>John
>
>
Author
14 Apr 2005 8:06 PM
J L
Hi Jason,
OMG...I  did not try your suggestion and that was the one I needed!!!
THANK YOU

so now here is what I do:

1. first size each column to fit the data (width = -1) (Note: width =
-2 sizes the column to the header, so actually I get the header width
first and then use the larger of the two widths).

2. Set the listview width to be the sum of all the column widths + 2
times the Border3DSize + the width of a vertical scrow bar (Note, if
you do not include the width of the vertical scrow bar the
LVM_GETCOUNTPERPAG message will return a count which allows for a
horizontal scroll bar.)

3. Read the number of items that will fit using the
LVM_GETCOUNTPERPAGE message.

4. If the number of actual items is less than the number items that
will fit, I reset the listview width to not include the width of a
vertical scroll bar since there will not be one there.

This works like a champ. I have to admit, I usually stear away from
API calls but this works great. Are there any downsides to using it?

Thanks again Jason!! Great suggestion.

John

On Thu, 14 Apr 2005 14:38:11 -0500, Jason Newell <nospam@nospam.com>
wrote:

Show quoteHide quote
>JL,
>    Just curiuos, did sending the LVM_GETCOUNTPERPAGE meesage work for you?
>  The LVM_GETCOUNTPERPAGE will tell you how man ListViewItems can be
>displayed before the scrollbar appears.
>
>MSDN Quote: "LVM_GETCOUNTPERPAGE calculates the number of items that can
>fit vertically in the visible area of a list-view control when in list
>or report view. Only fully visible items are counted."
>
>Just curious.
>
>Jason Newell, MCAD
>Software Engineer
>
>J L wrote:
>> For those interested in this thread, here is what I have found that
>> seems to work:
>>
>> Dim r As Rectangle = theListView.Items(0).Bounds
>> Dim htItems As Integer
>> ' get whole number of items that will fit
>> ' in the listview
>> htItems = theListView.Height/r.Height
>> ' allow for column headers...not sure why 2 instead of 1
>> If htItems < theListView.Items.Count + 2
>>    theListView.Width = sumOfColumnWidths + _
>>       (SystemInformation.Border3DSize.Width * 2) + _
>>       SystemInformation.VerticalScrollBarWidth
>> Else
>>    theListView.Width = sumOfColumnWidths + _
>>       (SystemInformation.Border3DSize.Width * 2)
>> End If
>>
>> This does fail on the condition that the last row that fills the list
>> view only partially fits. It puts the vertical scrow bar in the
>> listview but does not increase the size. I can write code to size th e
>> list view to an exact row count but if this is called repeatedly, I
>> loose track of the initial design height...does that make any sense to
>> any one...the code to size it to a whole number of rows is to add
>> before the If...End If clause:
>>
>> theListView.Height = (htItems + 1) * r.Height
>>
>> (i.e. use the number of items and the height of each, allow for the
>> headers)
>>
>>
>> If anyone has a comment, sees an error or knows a better approach, I
>> would appreciate your inputs.
>>
>> TIA,
>> John
>>
>> On Wed, 13 Apr 2005 10:47:50 -0700, J L <ystem> wrote:
>>
>>
>>>When I fill a listview, I resize the columns to fit the data. I need
>>>to know if the data will fit vertically or if there will be a vertical
>>>scroll bar. I need to know this so I can allow for it on the overall
>>>size of the listview.
>>>
>>>My question therefore is, how can I tell if the items I have added
>>>will fit in the listview at its given height?
>>>
>>>A secondary one, just for interest sake...is there a way to determine
>>>the exact heght needed to hold the items?
>>>
>>>TIA,
>>>John
>>
>>
Author
14 Apr 2005 8:34 PM
Jason Newell
JL,
    There should not be any downsides that I know of.  Internally, the .NET
ListView is making the same API calls, you just don't know it.
    I'm verse with the ListView API calls because I wrote my own .NET
ListView control.  So I browsed around through the LVM_'s definitions
looking for one to do what you wanted.
    I'm glad it worked for you.  You hadn't responded to my 2nd post so I
thought it wasn't what you were needing (again like in 1st post).

Jason Newell, MCAD
Software Engineer


J L wrote:
Show quoteHide quote
> Hi Jason,
> OMG...I  did not try your suggestion and that was the one I needed!!!
> THANK YOU
>
> so now here is what I do:
>
> 1. first size each column to fit the data (width = -1) (Note: width =
> -2 sizes the column to the header, so actually I get the header width
> first and then use the larger of the two widths).
>
> 2. Set the listview width to be the sum of all the column widths + 2
> times the Border3DSize + the width of a vertical scrow bar (Note, if
> you do not include the width of the vertical scrow bar the
> LVM_GETCOUNTPERPAG message will return a count which allows for a
> horizontal scroll bar.)
>
> 3. Read the number of items that will fit using the
> LVM_GETCOUNTPERPAGE message.
>
> 4. If the number of actual items is less than the number items that
> will fit, I reset the listview width to not include the width of a
> vertical scroll bar since there will not be one there.
>
> This works like a champ. I have to admit, I usually stear away from
> API calls but this works great. Are there any downsides to using it?
>
> Thanks again Jason!! Great suggestion.
>
> John
>
> On Thu, 14 Apr 2005 14:38:11 -0500, Jason Newell <nospam@nospam.com>
> wrote:
>
>
>>JL,
>>    Just curiuos, did sending the LVM_GETCOUNTPERPAGE meesage work for you?
>> The LVM_GETCOUNTPERPAGE will tell you how man ListViewItems can be
>>displayed before the scrollbar appears.
>>
>>MSDN Quote: "LVM_GETCOUNTPERPAGE calculates the number of items that can
>>fit vertically in the visible area of a list-view control when in list
>>or report view. Only fully visible items are counted."
>>
>>Just curious.
>>
>>Jason Newell, MCAD
>>Software Engineer
>>
>>J L wrote:
>>
>>>For those interested in this thread, here is what I have found that
>>>seems to work:
>>>
>>>Dim r As Rectangle = theListView.Items(0).Bounds
>>>Dim htItems As Integer
>>>' get whole number of items that will fit
>>>' in the listview
>>>htItems = theListView.Height/r.Height
>>>' allow for column headers...not sure why 2 instead of 1
>>>If htItems < theListView.Items.Count + 2
>>>   theListView.Width = sumOfColumnWidths + _
>>>      (SystemInformation.Border3DSize.Width * 2) + _
>>>      SystemInformation.VerticalScrollBarWidth
>>>Else
>>>   theListView.Width = sumOfColumnWidths + _
>>>      (SystemInformation.Border3DSize.Width * 2)
>>>End If
>>>
>>>This does fail on the condition that the last row that fills the list
>>>view only partially fits. It puts the vertical scrow bar in the
>>>listview but does not increase the size. I can write code to size th e
>>>list view to an exact row count but if this is called repeatedly, I
>>>loose track of the initial design height...does that make any sense to
>>>any one...the code to size it to a whole number of rows is to add
>>>before the If...End If clause:
>>>
>>>theListView.Height = (htItems + 1) * r.Height
>>>
>>>(i.e. use the number of items and the height of each, allow for the
>>>headers)
>>>
>>>
>>>If anyone has a comment, sees an error or knows a better approach, I
>>>would appreciate your inputs.
>>>
>>>TIA,
>>>John
>>>
>>>On Wed, 13 Apr 2005 10:47:50 -0700, J L <ystem> wrote:
>>>
>>>
>>>
>>>>When I fill a listview, I resize the columns to fit the data. I need
>>>>to know if the data will fit vertically or if there will be a vertical
>>>>scroll bar. I need to know this so I can allow for it on the overall
>>>>size of the listview.
>>>>
>>>>My question therefore is, how can I tell if the items I have added
>>>>will fit in the listview at its given height?
>>>>
>>>>A secondary one, just for interest sake...is there a way to determine
>>>>the exact heght needed to hold the items?
>>>>
>>>>TIA,
>>>>John
>>>
>>>
>
Author
14 Apr 2005 9:06 PM
J L
Hi Jason,
Thank you for your pursuing the question. It was indeed what I needed.
The reason for my last question was whether or not the .NET framework
supported these API.

Thanks again.

John

On Thu, 14 Apr 2005 15:34:21 -0500, Jason Newell <nospam@nospam.com>
wrote:

Show quoteHide quote
>JL,
>    There should not be any downsides that I know of.  Internally, the .NET
>ListView is making the same API calls, you just don't know it.
>    I'm verse with the ListView API calls because I wrote my own .NET
>ListView control.  So I browsed around through the LVM_'s definitions
>looking for one to do what you wanted.
>    I'm glad it worked for you.  You hadn't responded to my 2nd post so I
>thought it wasn't what you were needing (again like in 1st post).
>
>Jason Newell, MCAD
>Software Engineer
>
>
>J L wrote:
>> Hi Jason,
>> OMG...I  did not try your suggestion and that was the one I needed!!!
>> THANK YOU
>>
>> so now here is what I do:
>>
>> 1. first size each column to fit the data (width = -1) (Note: width =
>> -2 sizes the column to the header, so actually I get the header width
>> first and then use the larger of the two widths).
>>
>> 2. Set the listview width to be the sum of all the column widths + 2
>> times the Border3DSize + the width of a vertical scrow bar (Note, if
>> you do not include the width of the vertical scrow bar the
>> LVM_GETCOUNTPERPAG message will return a count which allows for a
>> horizontal scroll bar.)
>>
>> 3. Read the number of items that will fit using the
>> LVM_GETCOUNTPERPAGE message.
>>
>> 4. If the number of actual items is less than the number items that
>> will fit, I reset the listview width to not include the width of a
>> vertical scroll bar since there will not be one there.
>>
>> This works like a champ. I have to admit, I usually stear away from
>> API calls but this works great. Are there any downsides to using it?
>>
>> Thanks again Jason!! Great suggestion.
>>
>> John
>>
>> On Thu, 14 Apr 2005 14:38:11 -0500, Jason Newell <nospam@nospam.com>
>> wrote:
>>
>>
>>>JL,
>>>    Just curiuos, did sending the LVM_GETCOUNTPERPAGE meesage work for you?
>>> The LVM_GETCOUNTPERPAGE will tell you how man ListViewItems can be
>>>displayed before the scrollbar appears.
>>>
>>>MSDN Quote: "LVM_GETCOUNTPERPAGE calculates the number of items that can
>>>fit vertically in the visible area of a list-view control when in list
>>>or report view. Only fully visible items are counted."
>>>
>>>Just curious.
>>>
>>>Jason Newell, MCAD
>>>Software Engineer
>>>
>>>J L wrote:
>>>
>>>>For those interested in this thread, here is what I have found that
>>>>seems to work:
>>>>
>>>>Dim r As Rectangle = theListView.Items(0).Bounds
>>>>Dim htItems As Integer
>>>>' get whole number of items that will fit
>>>>' in the listview
>>>>htItems = theListView.Height/r.Height
>>>>' allow for column headers...not sure why 2 instead of 1
>>>>If htItems < theListView.Items.Count + 2
>>>>   theListView.Width = sumOfColumnWidths + _
>>>>      (SystemInformation.Border3DSize.Width * 2) + _
>>>>      SystemInformation.VerticalScrollBarWidth
>>>>Else
>>>>   theListView.Width = sumOfColumnWidths + _
>>>>      (SystemInformation.Border3DSize.Width * 2)
>>>>End If
>>>>
>>>>This does fail on the condition that the last row that fills the list
>>>>view only partially fits. It puts the vertical scrow bar in the
>>>>listview but does not increase the size. I can write code to size th e
>>>>list view to an exact row count but if this is called repeatedly, I
>>>>loose track of the initial design height...does that make any sense to
>>>>any one...the code to size it to a whole number of rows is to add
>>>>before the If...End If clause:
>>>>
>>>>theListView.Height = (htItems + 1) * r.Height
>>>>
>>>>(i.e. use the number of items and the height of each, allow for the
>>>>headers)
>>>>
>>>>
>>>>If anyone has a comment, sees an error or knows a better approach, I
>>>>would appreciate your inputs.
>>>>
>>>>TIA,
>>>>John
>>>>
>>>>On Wed, 13 Apr 2005 10:47:50 -0700, J L <ystem> wrote:
>>>>
>>>>
>>>>
>>>>>When I fill a listview, I resize the columns to fit the data. I need
>>>>>to know if the data will fit vertically or if there will be a vertical
>>>>>scroll bar. I need to know this so I can allow for it on the overall
>>>>>size of the listview.
>>>>>
>>>>>My question therefore is, how can I tell if the items I have added
>>>>>will fit in the listview at its given height?
>>>>>
>>>>>A secondary one, just for interest sake...is there a way to determine
>>>>>the exact heght needed to hold the items?
>>>>>
>>>>>TIA,
>>>>>John
>>>>
>>>>
>>