vb.net 如何在 Visual Basic 中创建多列列表框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36801384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:12:08  来源:igfitidea点击:

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

vb.net

提问by weeurey

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 aswell. Im guessing

任何可以给我一些关于我如何将项目添加到列表中的指针的人的奖励积分。我正在猜测

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

listBox1.Items.Add(“哈利波特”,“JK 罗琳”,5.99);

wont work?

行不通?

采纳答案by Capellan

I've run into the same question before and turned to ListViews instead. Something like:

我之前遇到过同样的问题,而是转向 ListViews。就像是:

Public Class Form1
Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Dim lv As New ListView
    With lv
        .View = View.Details
        .FullRowSelect = True
        .Columns.Add("Title")
        .Columns.Add("Author")
        .Columns.Add("Price")
    End With

    Me.Controls.Add(lv)
    lv.Dock = DockStyle.Fill

    lv.Items.Add(New ListViewItem({"Harry Potter", "J.K. Rowling", 5.99}))
End Sub
End Class

回答by Claudius

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 12

了解更多关于列表框1 2

Now you should really use ListView:

现在你应该真正使用 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)

回答by Olivier Jacot-Descombes

You can add any type of object to list boxes. Create a class containing the desired properties and override the ToString method. The list box uses it to display the items.

您可以将任何类型的对象添加到列表框。创建一个包含所需属性的类并覆盖 ToString 方法。列表框使用它来显示项目。

Public Class Book
    Public Property Title As String
    Public Property Author As String
    Public Property Price As Decimal

    Public Overrides Function ToString() As String
        Return String.Format("{0} by {1} at {2:c}", Title, Author, Price)
    End Function
End Class

Then

然后

Dim p = New Person With {.Title = "Harry Potter", .Author = "JK Rowling", Price = 5.99D}
listBox1.Items.Add(p)

回答by Supa Stix

 Me.ListBox1 = New System.Windows.Forms.ListBox()
    Me.SuspendLayout()
    ' 
    ' listBox1
    ' 
    Me.ListBox1.FormattingEnabled = True
    Me.ListBox1.HorizontalScrollbar = True
    Me.ListBox1.Items.AddRange(New Object() {"Item 1, column 1", "Item 2, column 1", "Item 3, column 1", "Item 4, column 1", "Item 5, column 1", "Item 1, column 2", "Item 2, column 2", "Item 3, column 2"})
    Me.ListBox1.Location = New System.Drawing.Point(0, 0)
    Me.ListBox1.MultiColumn = True
    Me.ListBox1.Name = "listBox1"
    Me.ListBox1.ScrollAlwaysVisible = True
    Me.ListBox1.Size = New System.Drawing.Size(120, 95)
    Me.ListBox1.TabIndex = 0
    Me.ListBox1.ColumnWidth = 85
    ' 
    ' Form1
    ' 
    Me.ClientSize = New System.Drawing.Size(292, 273)
    Me.Controls.Add(ListBox1)
    Me.Name = "Form1"
    Me.ResumeLayout(False)
End Sub