我用谷歌搜索了一下,但没有找到任何如何做到这一点的例子?我正在尝试将文本框行添加到列表视图,例如第 1 行 > 第 1 列,第 2 行 > 第 2 列,第 3 行 > 第 3 列,等等。如果有人能给出如何实现此目的的示例,我将非常感激(VB.NET、C...、LINQ)。
我尝试过的
我尝试使用以下改编自不同来源的代码,但它将所有文本框行添加到每列,如上图所示:
代码
For Each strLine In TextBox42.Lines
strLine.Split(vbNewLine)
ListView5.Items.Add(New ListViewItem(New String() {strLine, strLine, strLine, strLine, strLine, strLine, strLine, Today.Date}))
Next
我需要什么
每次按下按钮时,我都需要输入一个不同的单词,并根据图像将其从文本框行分类为 ListView 项目/行(比较两个图像)。连字符 表示该列没有数据输入,但这可能会随着用户添加有关每个单词/词位的信息而改变。
我尝试过的其他代码
ListView5.Items.AddRange(Array.ConvertAll(Enumerable.Range(0, TextBox42.Lines.Length).ToArray, Function(x) New ListViewItem(New String() {TextBox42.Lines(x)})))
您应该考虑
DataGridView
作为您的解决方案。不过,我确实找到了一些旧的演示代码......
Public Class Form1
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
'ListView is a Windows Explorer style view - user can switch between icons and details
lvwFiles.Columns.Clear()
lvwFiles.Columns.Add("Filename", 150, HorizontalAlignment.Left)
lvwFiles.Columns.Add("Date modified", 150, HorizontalAlignment.Left)
lvwFiles.Columns.Add("Type", 150, HorizontalAlignment.Left)
lvwFiles.Columns.Add("Size", 150, HorizontalAlignment.Left)
Dim iml As New ImageList()
iml.Images.Add("AAA", GetImage("AAA"))
iml.Images.Add("BBB", GetImage("BBB"))
iml.Images.Add("CCC", GetImage("CCC"))
iml.Images.Add("DDD", GetImage("DDD"))
iml.ImageSize = New Size(64, 64)
lvwFiles.LargeImageList = iml
lvwFiles.Items.Add("AAA.txt", "AAA").SubItems.AddRange({New Date(2017, 6, 2), "Text File", 20000})
lvwFiles.Items.Add("BBB.txt", "BBB").SubItems.AddRange({New Date(2017, 5, 3), "Text File", 10000})
lvwFiles.Items.Add("CCC.exe", "CCC").SubItems.AddRange({New Date(2017, 4, 4), "Application", 40000})
lvwFiles.Items.Add("DDD.txt", "DDD").SubItems.AddRange({New Date(2017, 3, 5), "Text File", 30000})
End Sub
Private Sub radDetails_CheckedChanged(sender As Object, e As EventArgs) Handles radDetails.CheckedChanged
If radDetails.Checked Then
lvwFiles.View = View.Details
End If
End Sub
Private Sub radIcons_CheckedChanged(sender As Object, e As EventArgs) Handles radIcons.CheckedChanged
If radIcons.Checked Then
lvwFiles.View = View.LargeIcon
End If
End Sub
Function GetImage(s As String) As Image
Dim bmp As New Bitmap(64, 64)
Using gfx As Graphics = Graphics.FromImage(bmp)
gfx.FillRectangle(Brushes.Black, New Rectangle(0, 0, 64, 64))
gfx.DrawString(s, New Font(FontFamily.GenericMonospace, 12), Brushes.Yellow, 0, 0)
End Using
Return bmp
End Function
Private Sub lvwFiles_ColumnClick(sender As Object, e As ColumnClickEventArgs) Handles lvwFiles.ColumnClick
Static sintLastColumnClicked As Integer = -1
If sintLastColumnClicked = e.Column Then
Me.lvwFiles.ListViewItemSorter = New ListViewItemComparer(e.Column, ListViewItemComparer.SortOrder_ENUM.Descending)
sintLastColumnClicked = -1
Else
Me.lvwFiles.ListViewItemSorter = New ListViewItemComparer(e.Column, ListViewItemComparer.SortOrder_ENUM.Ascending)
sintLastColumnClicked = e.Column
End If
' Call the sort method to manually sort.
lvwFiles.Sort()
End Sub
Class ListViewItemComparer
Implements IComparer
Enum SortOrder_ENUM As Integer
Ascending = 1
Descending = -1
End Enum
Private mintColumn As Integer
Private mintSortOrder As Integer
Sub New(column As Integer, sortOrder As SortOrder_ENUM)
mintColumn = column
If sortOrder = SortOrder_ENUM.Ascending Then
mintSortOrder = 1
Else
mintSortOrder = -1
End If
End Sub
Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
Dim returnVal As Integer = -1
Dim lviX As ListViewItem = DirectCast(x, ListViewItem)
Dim lviY As ListViewItem = DirectCast(y, ListViewItem)
Select Case mintColumn
Case 0, 2 'text sort
returnVal = [String].Compare(lviX.SubItems(mintColumn).Text, lviY.SubItems(mintColumn).Text)
Case 1 'date sort
returnVal = Date.Compare(CDate(lviX.SubItems(mintColumn).Text), CDate(lviY.SubItems(mintColumn).Text))
Case 3 'integer sort
If CInt(lviX.SubItems(mintColumn).Text) < CInt(lviY.SubItems(mintColumn).Text) Then
returnVal = -1
ElseIf CInt(lviX.SubItems(mintColumn).Text) = CInt(lviY.SubItems(mintColumn).Text) Then
returnVal = 0
Else
returnVal = 1
End If
End Select
Return returnVal * mintSortOrder
End Function
End Class
End Class