Add multiple items to a listbox on the same line

一曲冷凌霜 提交于 2019-12-06 19:44:25

It sounds like you still want to add one "item", but you want it to contain more than one piece of text. Simply do some string concatenation (or using string.Format), eg.

listBox1.Items.Add(string.Format("{0} | {1}", first_name.Text, last_name.Text));

Usually you don't want to include multiple columns into a ListBox, because ListBox is meant to have only one column.

I think what you're looking for is a ListView, which allows to have multiple columns. In a ListView you first create the columns you need

ListView myList = new ListView();
ListView.View = View.Details; // This enables the typical column view!

// Now create the columns
myList.Columns.Add("First Name", -2, HorizontalAlignment.Left);
myList.Columns.Add("Last Name", -2, HorizontalAlignment.Left);
myList.Columns.Add("Date of Birth", -2, HorizontalAlignment.Right);

// Now create the Items
ListViewItem item = new ListViewItem(first_name.Text);
item.SubItems.Add(last_name.Text);
item.SubItems.Add(dob.Text);

myList.Items.Add(item);

Here has a solution to add multiples items at the same time.

public enum itemsEnum {item1, item2, itemX}

public void funcTest2(Object sender, EventArgs ea){
    Type tp = typeof(itemsEnum);
    String[] arrItemEnum = Enum.GetNames(tp);
    foreach (String item in arrItemEnum){
        ListBox1.Items.Add(item);
    }
}

Hope this can help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!