我的表单上只有docked.full listview。 Listview显示计算机驱动器,因此它的内容在运行时可以更改。列表视图列标题始终可见 我希望我的表单可以使用listview调整填充项目的大小。
这是我的代码:
Dim rc As Rectangle = lvDriveInfo.Items(0).GetBounds(ItemBoundsPortion.Entire)
Me.Height = (rc.Height * lvDriveInfo.Items.Count) +
SystemInformation.CaptionHeight +
SystemInformation.BorderSize.Height
但有些事在这里错过或不正确。
如何获得带有关于items.count的标题的listview的精确高度,并使用此值正确设置表单的高度?
答案 0 :(得分:4)
ListView指标相当复杂,您要做的事情绝对是不。您忽略的一件事是列标题所需的空间。但这还不够,ListView还需要在底部有一点额外的肘部空间,以避免显示滚动条。
在我的机器上运行的最直接的代码是:
Private Sub ResizeView()
If ListView1.Items.Count = 0 Then Exit Sub
Dim last = ListView1.Items(ListView1.Items.Count - 1)
Me.ClientSize = New Size(Me.ClientSize.Width, _
ListView1.Top + last.Bounds.Bottom + 4)
End Sub
+4就是问题,我不能保证这对每个视频DPI的每个Windows版本都有效。它与物品高度无关,因此令人鼓舞。但测试需要确定。如果您可以保证列表中永远不会有太多项目,那么您可以通过将列表视图的Scrollable属性设置为False来避免此问题。
答案 1 :(得分:1)
尝试以下代码
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lvDriveInfo.BorderStyle = BorderStyle.None
lvDriveInfo.Dock = DockStyle.Fill
With lvDriveInfo
.View = View.Details
.GridLines = True
.Columns.Add("Drive")
End With
SetFormHeight()
End Sub
Private Sub SetFormHeight()
lvDriveInfo.Items.Clear()
For Each Drive In IO.DriveInfo.GetDrives
lvDriveInfo.Items.Add(Drive.Name)
Next
Dim ListViewHeaderHeight As Integer = lvDriveInfo.Items(0).Bounds.Top
Dim ListViewRowHeight As Integer = lvDriveInfo.Items(0).Bounds.Height
Dim ListViewRowsCount As Integer = lvDriveInfo.Items.Count
Dim NewHeight As Integer = ListViewHeaderHeight + (ListViewRowHeight * ListViewRowsCount)
Me.ClientSize = New Size(Me.ClientSize.Width, NewHeight)
End Sub