首页 > 解决方案 > vb.net listview上的分页项

问题描述

我想将我的ListView项目分开到每一页。我在分析代码时遇到了一个问题,我的代码如何简化为干净的代码或为我的编写方法获得一些新技巧?对不起,我的英语不好。

我试过计算我需要多少页,计算我修改的最后一个项目,我也尝试过考虑最好的方法来做到这一点。

Dim Tabs_count As Integer = Nothing
Dim Tabs_item As Integer = Nothing
Public Sub Tabs(item_s As Integer)
    Dim Tab As Double = item_s / 10
    Dim LastItem As Array = Tab.ToString.Split(",")        
    Dim totalfile As Integer = item_s Mod (10 * LastItem(0))
    'tab is a number of page I need it
    'totalfiles is a number of the last item i hope to array it
End Sub

编程逻辑:假设我有 100 个项目。我想将它们分开每页 10 个项目,以便我可以从文件中获取ListView项目和子项目。JSON我想通过链接将 JSON 文件放在一个数组中,如下所示:

Dim bunch_of_files as new list(of string)
this is loop ' 
Dim files as string = "link of JSON files"
    bunch_of files.add(files)
end of loop

当我调用第 1 页时,它会像:

Public sub page_called(str as int32) ' input will be (1) (2) (3) (4) ... (+)
Dim item_end as int32 = str * 10 'ex : 1 * 10 = 10
 str = str * 10 - 10
  for i as int32 = str to item_end ' 1 to 10
  listview1.item.add( bunch_of_item(i) )
next
End Sub

标签: vb.netwinforms

解决方案


如果我对您的理解正确,这应该可以让您朝着正确的方向开始:

Private Sub SplitListViewButton_Click(sender As Object, e As EventArgs) Handles SplitListViewButton.Click
    Try

        'I want to split my source list view into tabs with 11 list items per tab page
        splitListViewToTabs(11)

    Catch ex As Exception
        MessageBox.Show(String.Concat("An error occurred: ", ex.Message))
    End Try
End Sub

Private Sub splitListViewToTabs(listItemsPerTabPage As Integer)

    'how many list items do I have in my source list view?
    Dim sourceListItemCount As Integer = FilesListView.Items.Count
    'how many tabs do I need?
    Dim tabCount As Integer = ((sourceListItemCount / listItemsPerTabPage) + 0.5)

    'place holder used as we copy list items from source to new listview
    Dim lastSourceListViewIndex As Integer

    'reset tab control
    SplitTabControl.TabPages.Clear()

    'start adding tabs
    For tabAdd As Integer = 1 To tabCount
        'add tab page
        SplitTabControl.TabPages.Add(tabAdd.ToString)

        'add list view
        Dim lv As New ListView With {
            .View = FilesListView.View,
            .Dock = DockStyle.Fill
        }
        'copy column headers over from source list view
        For Each listviewCol As ColumnHeader In FilesListView.Columns
            lv.Columns.Add(listviewCol.Clone)
        Next

        'add the listview to the tabpage
        SplitTabControl.TabPages(SplitTabControl.TabPages.Count - 1).Controls.Add(lv)

        'copy list items from source listview to new list view
        For listItemIndex As Integer = lastSourceListViewIndex To lastSourceListViewIndex + listItemsPerTabPage - 1
            If listItemIndex > sourceListItemCount - 1 Then
                Exit For
            End If
            lv.Items.Add(FilesListView.Items(listItemIndex).Clone)
        Next

        lastSourceListViewIndex = lastSourceListViewIndex + listItemsPerTabPage

    Next

End Sub

推荐阅读