Arrange 2 List<string> to list box

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;
listViewProtocols.Columns.Add("Name");
listViewProtocols.Columns.Add("Number");

for (int i = 0; i < lists._protocolsList.Count; i++)
{
    listViewProtocols.Items.Add(new ListViewItem{ lists._nameList[i], lists._numbersList[i].ToString()});
}

Answers


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

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

listView1.View=View.Details;
listView1.Columns.Add("Name");
listView1.Columns.Add("Number");

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

for(int i=0; i<names.Length; i++)
{
    listView1.Items.Add(
        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[] {
            name,
            number.ToString() };
    }
}

and used like this:

    listView1.View=View.Details;
    listView1.Columns.Add("Name");
    listView1.Columns.Add("Number");

    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++)
    {
        listView1.Items.Add(
            new ListViewItem(list[i].ToStringArray()));
    }

Need Your Help

Bound DataGrid not updating on collection change

c# wpf bind

I am a noob for the WPF. Hope i can find answer.

How can I read HDD volume serial number using VB.NET?

hard-drive vb.net

How can I read HDD volume serial number using VB.NET but without any third party addons for Visual Studio or any external libraries - I neeed native VB.NET code for this if possible.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.