How do you add content to a VB list box?
In VB, there are several ways to add content to a list box control.
- Add a single item using the Add method.
ListBox1.Items.Add("项目1")
ListBox1.Items.Add("项目2")
ListBox1.Items.Add("项目3")
- Add multiple items using the AddRange method.
Dim items As String() = {"项目1", "项目2", "项目3"}
ListBox1.Items.AddRange(items)
- Add items using data binding (applicable for cases where data source is bound).
Dim dt As New DataTable()
dt.Columns.Add("ID")
dt.Columns.Add("Name")
dt.Rows.Add("1", "项目1")
dt.Rows.Add("2", "项目2")
dt.Rows.Add("3", "项目3")
ListBox1.DataSource = dt
ListBox1.DisplayMember = "Name"
ListBox1.ValueMember = "ID"
Here are some common ways to add content to the list box. You can choose the method that best suits your needs.