Arrange 2 List to list box

Go To


i have 2 List that i want to put into my Listbox the first List contain names and the second contain numbers my problem is that some of the names long so the numbers cannot a display in the same line how can i put in in appropriate way ?

listBox.Items.Add("Name" + "\t\t\t" + "Number");
for (int i = 0; i < lists.Count; i++)
    listBox.Items.Add(lists._namesList[i] + "\t\t\t" + lists._numbersList[i]);

Update here is what I tried with a ListView

listViewProtocols.View = View.Details;

for (int i = 0; i < lists._protocolsList.Count; i++)
    listViewProtocols.Items.Add(new ListViewItem{ lists._nameList[i], lists._numbersList[i].ToString()});
2012-04-04 18:02
by user979033
You should probably be using a grid for this, not a simple ListBox - Yuck 2012-04-04 18:03
Winform has Grid - user979033 2012-04-04 18:04
Yes, DataGridView - If that's too heavy-weight you could consider using ListView instead, and put it in details view. Managing sub-items can be a pain, though - Yuck 2012-04-04 18:05
how can i fill the DataGridView from my lists - user979033 2012-04-04 18:16
@user979033: Please do not replace the original question with a new one as the answers do not follow any more. Use updates and edit to add to your postings - ja72 2012-04-04 19:10


Consider using a ListView component, with Details style. As @Yuck mentioned in the comments it will give you the effect you need.

ListView example

It is a bit akward to populate from 2 separate lists but it is doable with the code below:


string[] names= { "Abraham", "Buster", "Charlie" };
int[] numbers= { 1018001, 1027400, 1028405 };

for(int i=0; i<names.Length; i++)
        new ListViewItem(new string[] {
        names[i], numbers[i].ToString() }));                

I would strongly recommend doing an array of structures instead of separate lists like this:

public struct Record
    public string name;
    public int number;

    public string[] ToStringArray()
        return new string[] {
            number.ToString() };

and used like this:


    Record[] list=new Record[] {
        new Record() { name="Abraham", number=1018001 },
        new Record() { name="Buster", number=1027400 },
        new Record() { name="Charlie", number=1028405 }

    for(int i=0; i<list.Length; i++)
            new ListViewItem(list[i].ToStringArray()));
2012-04-04 18:31
by ja72
see my update, i received error: Cannot initialize type 'System.Windows.Forms.ListViewItem' with a collection initializer because it does not implement 'System.Collections.IEnumerable - user979033 2012-04-04 18:46
@user979033: you need to initialize the ListViewItem with a string array like this new ListViewItem(new string[] {...}). You forgot the new string[] in your code - ja72 2012-04-04 19:06
OK fix, thanks. - user979033 2012-04-04 19:30


There are couple of options I can think of:

  1. Make the listbox wider so that it can accomodate longer text or add a horizontal scrollbar to it.
  2. Constrain the max length of names to, let's say, 20 chars and replace extra characters with ....
  3. Probably the best solution is to use grid instead of listbox - you need to display two columns of data, which is exactly the grid is for.
2012-04-04 18:06
by Nikola Anusev