Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How exactly do I create a multicolumn listbox in Visual Basic?

Tags:

vb.net

What I'm looking for is a list box that has multiple columns for example a list box for books where each row would have a title, price, author.

Bonus points for anyone who can give me some pointers on how exactly I can add items to the list as well. I'm guessing

 listBox1.Items.Add("Harry Potter", "JK Rowling", 5.99);

wont work?

like image 346
weeurey Avatar asked Dec 25 '22 06:12

weeurey


1 Answers

You need to change property:

Me.listBox1.MultiColumn = True

to add single:

listBox1.Items.Add("Item")

to add multi:

With Me.listBox1
    Me.listBox1.ColumnCount = 2
    .AddItem
    .List(i, 0) = "something for first column"
    .List(i, 1) = "something for second column"
    i = i + 1
end with

Learn more about listbox 1 2

Now you should really use ListView:

    'Add Three Columns To ListView 2
    ListView2.Columns.Add("Zodiac", 100, HorizontalAlignment.Center) 'Column 1
    ListView2.Columns.Add("From", 100, HorizontalAlignment.Left) 'Column 2
    ListView2.Columns.Add("To", 100, HorizontalAlignment.Right) 'Column 3

    'Show Small Images Next To Zodiac Sign
    ListView2.SmallImageList = ImageList2

    'Declare Array For ListView Items
    Dim arrLVItem(11) As ListViewItem

    Dim j As Integer 'Loop Counter

    'Loop Through Each ListViewItem Array Item
    For j = 0 To arrLVItem.Length - 1

        'Initialize ListViewItem Array
        arrLVItem(j) = New ListViewItem

        'Add Text To First ListView Item - The Zodiac Sign
        arrLVItem(j).SubItems(0).Text = arrZodiac(j)

        'Add From and To SubItems On Zodiac ListView Item
        arrLVItem(j).SubItems.Add(arrFrom(j))
        arrLVItem(j).SubItems.Add(arrTo(j))

        'Connect ListView Item With Its Associated Picture
        arrLVItem(j).ImageIndex = j

    Next j

    'Add Completed Arrays To [ListView][3]
    ListView2.Items.AddRange(arrLVItem)
like image 186
Claudius Avatar answered Jan 30 '23 00:01

Claudius