Listview size

  • Thread starter Thread starter J L
  • Start date Start date
J

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
 
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
 
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
 
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
 
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:
 
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
 
http://www.vbdotnetheaven.com/

J said:
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
 
Back
Top