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()});
}
DataGridView
- http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx 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
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()));
}
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
There are couple of options I can think of:
...
.
ListBox
- Yuck 2012-04-04 18:03