在运行时排列列表视图中的项目
Arrange Items in Listview at Runtime
我有一个包含列表视图、文本框和添加按钮的表单。当我单击添加按钮时,我在文本框中键入的文本将显示到列表视图
我想在单击添加按钮后按字母或数字 rearrange/order(在运行时并且不单击 header 列)。
这是我的代码:
Private Sub Add_Click ()
Listview.listitems.add(1).text = text1.text
Listview.listitems.item(1).subitems(1) = text2.text
End Sub
Private Sub Form_Load()
With Listview.columnheaders
.Add, , "Column 1"
.Add, , "Column 2"
End with
End sub
ListView控件有一个Sorted
属性,您应该将其设置为True
。您可以使用设计器或使用代码来做到这一点。在这种情况下,您的 Form_Load
代码应该类似于:
Private Sub Form_Load()
With ListView.ColumnHeaders
.Add , , "Column 1"
.Add , , "Column 2"
End With
' This will sort by the first column
ListView.SortKey = 0
' Sort in an ascending order
ListView.SortOrder = lvwAscending
ListView.Sorted = True
End Sub
另请注意,添加项目时无需指定索引,因为列表已排序。另请注意,您现在添加项目的方式将始终 更改第一项 的sub-item。如果您需要更改最近添加的项目的 sub-item(即在同一行的两列中添加项目),您需要将其更改为如下所示:
Private Sub Add_Click()
Dim newItem As ListItem
Set newItem = ListView.ListItems.Add()
newItem.Text = Text1.Text
newItem.SubItems(1) = Text2.Text
End Sub
我有一个包含列表视图、文本框和添加按钮的表单。当我单击添加按钮时,我在文本框中键入的文本将显示到列表视图
我想在单击添加按钮后按字母或数字 rearrange/order(在运行时并且不单击 header 列)。
这是我的代码:
Private Sub Add_Click ()
Listview.listitems.add(1).text = text1.text
Listview.listitems.item(1).subitems(1) = text2.text
End Sub
Private Sub Form_Load()
With Listview.columnheaders
.Add, , "Column 1"
.Add, , "Column 2"
End with
End sub
ListView控件有一个Sorted
属性,您应该将其设置为True
。您可以使用设计器或使用代码来做到这一点。在这种情况下,您的 Form_Load
代码应该类似于:
Private Sub Form_Load()
With ListView.ColumnHeaders
.Add , , "Column 1"
.Add , , "Column 2"
End With
' This will sort by the first column
ListView.SortKey = 0
' Sort in an ascending order
ListView.SortOrder = lvwAscending
ListView.Sorted = True
End Sub
另请注意,添加项目时无需指定索引,因为列表已排序。另请注意,您现在添加项目的方式将始终 更改第一项 的sub-item。如果您需要更改最近添加的项目的 sub-item(即在同一行的两列中添加项目),您需要将其更改为如下所示:
Private Sub Add_Click()
Dim newItem As ListItem
Set newItem = ListView.ListItems.Add()
newItem.Text = Text1.Text
newItem.SubItems(1) = Text2.Text
End Sub