排列2 List< string>到列表框 [英] Arrange 2 List<string> to list box

查看:86
本文介绍了排列2 List< string>到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个要放入列表框的列表 第一个列表包含名称,第二个列表包含数字 我的问题是某些名称过长,因此数字无法在同一行中显示 我该如何以适当的方式放置?

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]);
}

更新,这是我尝试使用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()});
}

推荐答案

考虑使用具有Details样式的ListView组件.正如@Yuck在评论中提到的那样,它将为您提供所需的效果.

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

从2个单独的列表进行填充有点笨拙,但是可以使用以下代码进行操作:

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() };
    }
}

并以此方式使用:

    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()));
    }

这篇关于排列2 List&lt; string&gt;到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆