none
How to add a header and detial line to a listview

    Question

  • Listbox has six columns, I want to have one column and three rows reserved for name and address information(Header if I may) and then continue displaying  detail information after that.   This header could be in any column. Listbox is populated from a text file, and address information come from a couple of textboxes. My open to other ways or better way to do it.  Thanks for your  time

    lviStudentCopy.View = View.Details
            lviStudentCopy.Columns.Add("Subject", 200, HorizontalAlignment.Left)
            lviStudentCopy.Columns.Add("Hours", 150, HorizontalAlignment.Left)
            lviStudentCopy.Columns.Add("Days", 100, HorizontalAlignment.Left)
            lviStudentCopy.Columns.Add("Class Room", 100, HorizontalAlignment.Left)
            lviStudentCopy.Columns.Add("Building", 100, HorizontalAlignment.Left)

    Friday, February 09, 2018 6:57 AM

Answers

  • Listbox has six columns, I want to have one column and three rows reserved for name and address information(Header if I may) and then continue displaying  detail information after that.   This header could be in any column. Listbox is populated from a text file, and address information come from a couple of textboxes. My open to other ways or better way to do it.  Thanks for your  time

    lviStudentCopy.View = View.Details
            lviStudentCopy.Columns.Add("Subject", 200, HorizontalAlignment.Left)
            lviStudentCopy.Columns.Add("Hours", 150, HorizontalAlignment.Left)
            lviStudentCopy.Columns.Add("Days", 100, HorizontalAlignment.Left)
            lviStudentCopy.Columns.Add("Class Room", 100, HorizontalAlignment.Left)
            lviStudentCopy.Columns.Add("Building", 100, HorizontalAlignment.Left)

    Hi

    Here is some example code that sets up a ListView1 and a Button1.

    Inserts some rows of dummy data.

    When Button Clicked, will insert Address Details at top of Column1.

    (NOTE: no checks made to see if Address Details already inserted)

    Also, the example sets more than the 3 lines you asked for, but that is fully controllable by setting the Address contents to whatever you need.

    This is a stand alone Project example, and needs just a blank Form1.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim listView1 As New ListView()
      Dim b As New Button
      Dim Address As New List(Of String)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	' set updummy address details
    	' these details could be from TextBoxes
    	' or any other source
    	Address.Add("Freddy Bloggs")
    	Address.Add("Suite 1001")
    	Address.Add("10 EastWest Street")
    	Address.Add("Northern Tavern")
    	Address.Add("Buffalo")
    	Address.Add("London SW3")
    	Address.Add("Bolivia")
    	' ------------------------
    
    	' set up initial ListView with some
    	' dunny test data
    	With listView1
    	  .View = View.Details
    
    	  .Columns.Add("Item Column", 100, HorizontalAlignment.Left)
    	  .Columns.Add("Column 2", -2, HorizontalAlignment.Left)
    	  .Columns.Add("Column 3", -2, HorizontalAlignment.Left)
    	  .Columns.Add("Column 4", -2, HorizontalAlignment.Center)
    	  .Columns.Add("Column 5", -2, HorizontalAlignment.Center)
    	  .Columns.Add("Column 6", -2, HorizontalAlignment.Center)
    
    	  .Bounds = New Rectangle(New Point(10, 10), New Size(380, 200))
    	  .View = View.Details
    
    	  ' add some test data
    	  For i As Integer = 1 To 20
    		Dim item As New ListViewItem("item" & i.ToString, 0)
    		item.Checked = True
    		item.SubItems.Add(i.ToString)
    		item.SubItems.Add((i + 1).ToString)
    		item.SubItems.Add((i + 2).ToString)
    		item.SubItems.Add((i + 3).ToString)
    		item.SubItems.Add((i + 4).ToString)
    		listView1.Items.Add(item)
    	  Next
    	End With
    
    	' set up a Button
    	With b
    	  .Text = "Add Address"
    	  .Location = New Point(10, 220)
    	  .AutoSize = True
    	End With
    	Controls.AddRange({listView1, b})
    	AddHandler b.Click, AddressOf B_Click
      End Sub
      Private Sub B_Click(sender As Object, e As EventArgs)
    	' Button Click inserts Address data
    	' NOTE: no checks for already used
    	' so each Click inserts a set of
    	' Address details.
    	For i As Integer = Address.Count - 1 To 0 Step -1
    	  listView1.Items.Insert(0, Address(i))
    	Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, February 09, 2018 1:16 PM
    • Marked as answer by alobi Saturday, February 10, 2018 11:02 AM
    Friday, February 09, 2018 1:15 PM

All replies

  • Listbox has six columns, I want to have one column and three rows reserved for name and address information(Header if I may) and then continue displaying  detail information after that.

    There are many constructors available for a ListViewItem, but the simplest just takes a string as the item text.
    https://msdn.microsoft.com/en-us/library/2c277wk5(v=vs.110).aspx

    Once you have created the  item, you can add to the sub items property of the item for the subsequent columns:
    https://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.subitems(v=vs.110).aspx

    Then add the ListViewItem to the ListView items collection:
    https://msdn.microsoft.com/en-us/library/system.windows.forms.listview.items(v=vs.110).aspx

    Friday, February 09, 2018 7:23 AM
  • Listbox has six columns, I want to have one column and three rows reserved for name and address information(Header if I may) and then continue displaying  detail information after that.   This header could be in any column. Listbox is populated from a text file, and address information come from a couple of textboxes. My open to other ways or better way to do it.  Thanks for your  time

    lviStudentCopy.View = View.Details

    Hi alobi,

    You said that you use ListBox, but I guess that you use ListView, am I right?

    You said Listbox has six columns, I want to have one column and three rows reserved for name and address information(Header if I may) and then continue displaying  detail information after that, I am not sure what you want to do. Can you please explain clearly?

    Here is about adding multiple columns in ListView, you can take a look:

    http://www.dotnetheaven.com/article/listview-control-with-multiple-column-in-vb.net

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, February 09, 2018 9:28 AM
    Moderator
  • Listbox has six columns, I want to have one column and three rows reserved for name and address information(Header if I may) and then continue displaying  detail information after that.   This header could be in any column. Listbox is populated from a text file, and address information come from a couple of textboxes. My open to other ways or better way to do it.  Thanks for your  time

    lviStudentCopy.View = View.Details
            lviStudentCopy.Columns.Add("Subject", 200, HorizontalAlignment.Left)
            lviStudentCopy.Columns.Add("Hours", 150, HorizontalAlignment.Left)
            lviStudentCopy.Columns.Add("Days", 100, HorizontalAlignment.Left)
            lviStudentCopy.Columns.Add("Class Room", 100, HorizontalAlignment.Left)
            lviStudentCopy.Columns.Add("Building", 100, HorizontalAlignment.Left)

    Hi

    Here is some example code that sets up a ListView1 and a Button1.

    Inserts some rows of dummy data.

    When Button Clicked, will insert Address Details at top of Column1.

    (NOTE: no checks made to see if Address Details already inserted)

    Also, the example sets more than the 3 lines you asked for, but that is fully controllable by setting the Address contents to whatever you need.

    This is a stand alone Project example, and needs just a blank Form1.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim listView1 As New ListView()
      Dim b As New Button
      Dim Address As New List(Of String)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	' set updummy address details
    	' these details could be from TextBoxes
    	' or any other source
    	Address.Add("Freddy Bloggs")
    	Address.Add("Suite 1001")
    	Address.Add("10 EastWest Street")
    	Address.Add("Northern Tavern")
    	Address.Add("Buffalo")
    	Address.Add("London SW3")
    	Address.Add("Bolivia")
    	' ------------------------
    
    	' set up initial ListView with some
    	' dunny test data
    	With listView1
    	  .View = View.Details
    
    	  .Columns.Add("Item Column", 100, HorizontalAlignment.Left)
    	  .Columns.Add("Column 2", -2, HorizontalAlignment.Left)
    	  .Columns.Add("Column 3", -2, HorizontalAlignment.Left)
    	  .Columns.Add("Column 4", -2, HorizontalAlignment.Center)
    	  .Columns.Add("Column 5", -2, HorizontalAlignment.Center)
    	  .Columns.Add("Column 6", -2, HorizontalAlignment.Center)
    
    	  .Bounds = New Rectangle(New Point(10, 10), New Size(380, 200))
    	  .View = View.Details
    
    	  ' add some test data
    	  For i As Integer = 1 To 20
    		Dim item As New ListViewItem("item" & i.ToString, 0)
    		item.Checked = True
    		item.SubItems.Add(i.ToString)
    		item.SubItems.Add((i + 1).ToString)
    		item.SubItems.Add((i + 2).ToString)
    		item.SubItems.Add((i + 3).ToString)
    		item.SubItems.Add((i + 4).ToString)
    		listView1.Items.Add(item)
    	  Next
    	End With
    
    	' set up a Button
    	With b
    	  .Text = "Add Address"
    	  .Location = New Point(10, 220)
    	  .AutoSize = True
    	End With
    	Controls.AddRange({listView1, b})
    	AddHandler b.Click, AddressOf B_Click
      End Sub
      Private Sub B_Click(sender As Object, e As EventArgs)
    	' Button Click inserts Address data
    	' NOTE: no checks for already used
    	' so each Click inserts a set of
    	' Address details.
    	For i As Integer = Address.Count - 1 To 0 Step -1
    	  listView1.Items.Insert(0, Address(i))
    	Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, February 09, 2018 1:16 PM
    • Marked as answer by alobi Saturday, February 10, 2018 11:02 AM
    Friday, February 09, 2018 1:15 PM
  • Thanks for the information, I will go an read it up. Al
    Saturday, February 10, 2018 7:12 AM
  • Thanks for the correction I meant listview not a listbox. I will go and read up the information

    Al

    Saturday, February 10, 2018 7:18 AM
  • Alobi,

    You say Listbox has six columns, that is an often made mistake. 

    A Listbox has one column of items and can have endless subitems. 

    The way you present it makes that the items and subitems can be shown in a column style (List and Details)

    The ListView is just the right pane which is also in File Explorer. 

    If you want columns, than take a DataGridView which is in fact a modern version of the old MS FlexGrid


    Success Cor


    Saturday, February 10, 2018 8:31 AM
  • Thanks Les, It is what I am looking  for, with a few changes I will  be up and running.
    Saturday, February 10, 2018 11:02 AM