473,386 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Listview size

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
Nov 21 '05 #1
12 7441
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

Nov 21 '05 #2
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 <no****@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


Nov 21 '05 #3
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:
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 <no****@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


Nov 21 '05 #4
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 <no****@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


Nov 21 '05 #5
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
<De****@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 <no****@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



Nov 21 '05 #6
http://www.vbdotnetheaven.com/

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
<De****@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 <no****@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
>
>



Nov 21 '05 #7
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:
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


Nov 21 '05 #8
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


Nov 21 '05 #9
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 <no****@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



Nov 21 '05 #10
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 <no****@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

Nov 21 '05 #11
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 <no****@nospam.com>
wrote:
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 <no****@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


Nov 21 '05 #12
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.

"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
<De****@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 <no****@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


Nov 21 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Wiktor Zychla | last post by:
today we've found a critical issue regarding the ListView from Windows.Forms. it was confirmed on several machines with Win2K and XP. here's the problem: create a ListView with about 50000 rows....
2
by: Mad Joe | last post by:
Hello! Can someone help me, please? It's 6am, I don't have concentration, feel desperate and I have a deadLine for my project (exam at my university).. How can I display ListView onMouse...
0
by: Martin Streller | last post by:
Hello, The code below represents a simple ownerdrawn, Listview class in C#. Its purpose is to avoid the flicker of the MS ListView. So I can't fall back to their one. Does anybody know why I...
6
by: Vanessa | last post by:
With this program I can do one selection, but upon the second I get an error where ///////////////// is indicated. Please help. using System; using System.Drawing; using System.Collections;...
4
by: Paddy | last post by:
How do I select a subitem from a listview after clicking on the first column? Couldn't find it in MSDN. Thank you. Paddy.
0
by: willow1480 | last post by:
I am developing a small little Service Control Application. I am using a listview control with checkboxes and getting the list of services I want to control from a text file. When you check a...
0
by: Andrew | last post by:
If item is a ListViewItem and str is a string, why do the following two lines not have the same effect ? item.SubItems.Add(new ListViewItem.ListViewSubItem()).Text = str;...
5
by: luis | last post by:
Hi I would like to use a listview to show the tumbnails of images, the problem is the the images are shown too small, Is there any way to change the image size If it is not possible, is there...
21
by: StriderBob | last post by:
Situation : FormX is mdi child form containing 2 ListViews ListView1 contains a list of table names and 4 sub items with data about each table. ListView2 contains a list of the columns on each...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.