473,586 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7468
JL,
You do know that by setting each ColumnHeader.Wi dth = -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_APPROXIMATE VIEWRECT
LVM_GETCOUNTPER PAGE
LVM_GETWORKAREA S
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.Wi dth = -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_APPROXIMAT EVIEWRECT
LVM_GETCOUNTPE RPAGE
LVM_GETWORKARE AS
LVM_GETITEMREC T

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_GETCOUNTPER PAGE 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_GETCOUNTPER PAGE 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.
InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er

'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.ColumnHea der
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.ListView1 = New System.Windows. Forms.ListView
Me.ColumnHeader 1 = New System.Windows. Forms.ColumnHea der
Me.SuspendLayou t()
'
'ListView1
'
Me.ListView1.Co lumns.AddRange( New
System.Windows. Forms.ColumnHea der() {Me.ColumnHeade r1})
Me.ListView1.Lo cation = New System.Drawing. Point(32, 24)
Me.ListView1.Na me = "ListView1"
Me.ListView1.Si ze = New System.Drawing. Size(184, 120)
Me.ListView1.Ta bIndex = 0
Me.ListView1.Vi ew = System.Windows. Forms.View.Deta ils
'
'Form1
'
Me.AutoScaleBas eSize = 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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Me.ListView1.Vi ew = View.Details
MessageBox.Show (SendMessage(Me .ListView1.Hand le,
LVM_GETCOUNTPER PAGE, 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.Wi dth = -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_APPROXIMA TEVIEWRECT
LVM_GETCOUNTP ERPAGE
LVM_GETWORKAR EAS
LVM_GETITEMRE CT

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
_(StringFormatF lags.MeasureTra ilingSpaces Or StringFormatFla gs.LineLimit Or _
StringFormatFla gs.FitBlackBox)

h = CInt(g.MeasureS tring(myText, myListView.Font , myListView.Widt h,
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.Wi dth = -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_APPROXIMAT EVIEWRECT
LVM_GETCOUNTPE RPAGE
LVM_GETWORKARE AS
LVM_GETITEMREC T

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****@discuss ions.microsoft. com> wrote:
Check out measureString method of a Graphics object;

Dim fmt As StringFormat = New StringFormat
_(StringFormat Flags.MeasureTr ailingSpaces Or StringFormatFla gs.LineLimit Or _
StringFormatFl ags.FitBlackBox )

h = CInt(g.MeasureS tring(myText, myListView.Font , myListView.Widt h,
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.Wi dth = -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_APPROXIMAT EVIEWRECT
>LVM_GETCOUNTPE RPAGE
>LVM_GETWORKARE AS
>LVM_GETITEMREC T
>
>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****@discus sions.microsoft .com> wrote:
Check out measureString method of a Graphics object;

Dim fmt As StringFormat = New StringFormat
_(StringForma tFlags.MeasureT railingSpaces Or StringFormatFla gs.LineLimit Or _
StringFormatF lags.FitBlackBo x)

h = CInt(g.MeasureS tring(myText, myListView.Font , myListView.Widt h,
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.Wi dth = -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_APPROXI MATEVIEWRECT
LVM_GETCOUN TPERPAGE
LVM_GETWORK AREAS
LVM_GETITEM RECT

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.Ite ms(0).Bounds
Dim htItems As Integer
' get whole number of items that will fit
' in the listview
htItems = theListView.Hei ght/r.Height
' allow for column headers...not sure why 2 instead of 1
If htItems < theListView.Ite ms.Count + 2
theListView.Wid th = sumOfColumnWidt hs + _
(SystemInformat ion.Border3DSiz e.Width * 2) + _
SystemInformati on.VerticalScro llBarWidth
Else
theListView.Wid th = sumOfColumnWidt hs + _
(SystemInformat ion.Border3DSiz e.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.Hei ght = (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_GETCOUNTPER PAGE meesage work for you?
The LVM_GETCOUNTPER PAGE will tell you how man ListViewItems can be
displayed before the scrollbar appears.

MSDN Quote: "LVM_GETCOUNTPE RPAGE 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.Ite ms(0).Bounds
Dim htItems As Integer
' get whole number of items that will fit
' in the listview
htItems = theListView.Hei ght/r.Height
' allow for column headers...not sure why 2 instead of 1
If htItems < theListView.Ite ms.Count + 2
theListView.Wid th = sumOfColumnWidt hs + _
(SystemInformat ion.Border3DSiz e.Width * 2) + _
SystemInformati on.VerticalScro llBarWidth
Else
theListView.Wid th = sumOfColumnWidt hs + _
(SystemInformat ion.Border3DSiz e.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.Hei ght = (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_GETCOUNTPER PAG message will return a count which allows for a
horizontal scroll bar.)

3. Read the number of items that will fit using the
LVM_GETCOUNTPER PAGE 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_GETCOUNTPER PAGE meesage work for you?
The LVM_GETCOUNTPER PAGE will tell you how man ListViewItems can be
displayed before the scrollbar appears.

MSDN Quote: "LVM_GETCOUNTPE RPAGE 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.Ite ms(0).Bounds
Dim htItems As Integer
' get whole number of items that will fit
' in the listview
htItems = theListView.Hei ght/r.Height
' allow for column headers...not sure why 2 instead of 1
If htItems < theListView.Ite ms.Count + 2
theListView.Wid th = sumOfColumnWidt hs + _
(SystemInformat ion.Border3DSiz e.Width * 2) + _
SystemInformati on.VerticalScro llBarWidth
Else
theListView.Wid th = sumOfColumnWidt hs + _
(SystemInformat ion.Border3DSiz e.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.Hei ght = (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

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

Similar topics

15
3857
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. now use task manager to see the GDI usage of the process. everything seems normal. now catch the ListView's scroller and start to move it...
2
4194
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 Click event? Let's say that I just need a detailed view of columnHeaders. Somewhere in my code there is already prepared array:
0
2608
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 cannot change the size of the ListView's font (ListView.Font) from e.g.
6
2524
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; using System.ComponentModel; using System.Windows.Forms; using System.Data;
4
9701
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
2323
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 checkbox, it should either stop the service or start the service depending on each respective service's status. I want the listView to update the...
0
12274
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; item.SubItems.Add(str); In the example code that follows, using the first line (in the ListView constructor) causes the later redisplay of values (to some specified...
5
11639
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 another control suitable to do this? Thanks
21
5189
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 table and 11 sub items with data about each column. When a Row in ListView1 is selected the Data in ListVies2 is loaded to show the correct data....
0
7912
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8202
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.