Populating a listview multi-column

Viewed 72971

Regarding Listbox to ListView migration.

Hello.

I have a Listbox I add entries like this to:

1;content

Where 1 is always an int and content is always a string. I can access each one seperately.

Now I want the result to be sorted descendingly, ie:

1;content
4;content2
2;content3

=>

4;content2
2;content3
1;content

As this doesn't look good, I want to use a Listview instead. Like this:

Frequency | Content
===============
4 | content2
2 | content3
1 | content

Problem is, the tabular property does not seem to exist, all entries are being put in like symbols in a listview in explorer. Also I have problems "reaching" the 2nd column(content), ie I only see 4,2,1.

How would I prepare and populate a listview in c# .net 4 for that?

5 Answers

To add the list view headers and add items to list view, try this code:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Lstv1.Columns.Add("Paramname", CInt(Lstv1.Width / 2))
        Lstv1.Columns.Add("Paramorder", CInt(Lstv1.Width / 2))
    End Sub


Private Sub appendlistview(ByVal Paramname As String, ByVal Paramorder As String)

    Dim newitem As New ListViewItem(Paramname)
    newitem.SubItems.Add(Paramorder)

    Lstv1.Items.Add(newitem)

End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click

    Call appendlistview(TextBox1.Text, TextBox2.Text)
End Sub

Form with multi column populated listview

Here is the full form code-Compile it from command window::

Imports System.Windows.Forms Imports System.Drawing Public Class Form1 Inherits System.Windows.Forms.Form

  Public ListView1 As ListView
  Public Col_Header1 As ColumnHeader
  Public Col_Header2 As ColumnHeader
  Public Col_Header3 As ColumnHeader
  Public Col_Header4 As ColumnHeader

  Public Sub New()
    ListView1 = New ListView
    Col_Header1 = New ColumnHeader
    Col_Header2 = New ColumnHeader
    Col_Header3 = New ColumnHeader
    Col_Header4 = New ColumnHeader
    ListView1.Columns.AddRange(New ColumnHeader() { _
                                       Col_Header1, _
                                       Col_Header2, _
                                       Col_Header3, _
                                       Col_Header4})
        Col_Header1.Text = "Header1"
        Col_Header1.Width = 200
        Col_Header2.Text = "Header2"
        Col_Header2.Width = 200
        Col_Header3.Text = "Header3"
        Col_Header3.Width = 200
        Col_Header4.Text = "Header4"
        Col_Header4.Width = 400
   Dim myItems As String() = New String() { _
                            "Item1", _
                            "Item2", _
                            "Item3", _
                            "Item4"}
   Dim myItems1 As String() = New String() { _
                            "Item11", _
                            "Item12", _
                            "Item13", _
                            "Item14"}
   Dim myItems2 As String() = New String() { _
                            "Item21", _
                            "Item22", _
                            "Item23", _
                            "Item24"}
   Dim myItems3 As String() = New String() { _
                            "Item31", _
                            "Item32", _
                            "Item33", _
                            "Item34"}
   Dim myItems4 As String() = New String() { _
                            "Item41", _
                            "Item42", _
                            "Item43", _
                            "Item44"}
           Dim lvi As ListViewItem = New ListViewItem(myItems)
           ListView1.Items.Add(lvi)
           Dim lvi1 As ListViewItem = New ListViewItem(myItems1)
           ListView1.Items.Add(lvi1)
           Dim lvi2 As ListViewItem = New ListViewItem(myItems2)
           ListView1.Items.Add(lvi2)
           Dim lvi3 As ListViewItem = New ListViewItem(myItems3)
           ListView1.Items.Add(lvi3)
           Dim lvi4 As ListViewItem = New ListViewItem(myItems4)
           ListView1.Items.Add(lvi4)
        ListView1.AllowColumnReorder = True
        ListView1.Font = New System.Drawing.Font("Segoe UI",15.0!, _
                       System.Drawing.FontStyle.Bold, System.Drawing. _
                                     GraphicsUnit.Point,CType(0, Byte))
        ListView1.FullRowSelect = True
        ListView1.HideSelection = False
        ListView1.Dock = DockStyle.Fill
        ListView1.Name = "listView1"            
        ListView1.View = View.Details
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(800,600)
        Me.Controls.Add(ListView1)
        Me.Text = "Deep_Research_For_ListView_Developement"
        Me.Name = "Research_Form1"            
        Me.ResumeLayout(False)
    End Sub

    Public Shared Sub Main()
      Dim form1 As Form1 = New Form1()
      form1.ShowDialog()
    End Sub 

    Private Sub Research_Form1_Load(ByVal sender As Object, _
                         ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Location = New System.Drawing.Point(0,0)

'''###########################-Both Codes work-########################### ' For Each i As ListViewItem In listView1.Items ' i.ForeColor = Color.Green ' i.BackColor = Color.Yellow
' Next

            For Each i As ListViewItem In ListView1.Items
              If (i.Index Mod 2) = 1 Then
                 i.BackColor = Color.FromArgb(230, 150, 255)
                 i.UseItemStyleForSubItems = True
              End If
            Next

'''###########################-Both Codes work-########################### End Sub End Class

Compiler Call From CMD:-

vbc.exe -target:exe [path]\Form1.vb [path]\Form1.exe

Related