winforms 如何将文本框行添加到单独的ListView列?

jdgnovmf  于 2023-10-23  发布在  其他
关注(0)|答案(2)|浏览(114)

我在谷歌上搜索了一下,但没有找到任何关于如何做到这一点的例子?我正在尝试添加文本框行到列表视图,例如。行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)})))
eblbsuwk

eblbsuwk1#

您应该考虑将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
nhjlsmyf

nhjlsmyf2#

null博士的第二个答案如下(来自上面的评论)是解决方案!
啊对。只是

ListView5.Items.Add(New ListViewItem(TextBox42.Lines.Append(Now.ToString()).ToArray()))

根据需要设置附加日期的格式。

相关问题