[英]How do I populate a listbox with List<string> data
I have a listbox, named listBox2, that I am trying to populate. 我有一个名为listBox2的列表框,我正在尝试填充。 "ProSailor" is a class and the three numbers at the end are values from said class.
“ProSailor”是一个类,最后的三个数字是来自所述类的值。
I need to add these values to listBox2. 我需要将这些值添加到listBox2。
public string[] listBoxBoard = new string[10];
List<string> _scoreboard = new List<string>();
ProSailor sailor1 = new ProSailor(1, 24, 7);
ProSailor sailor2 = new ProSailor(2, 23, 14);
ProSailor sailor3 = new ProSailor(3, 20, 5);
However, if I try this: 但是,如果我试试这个:
_scoreboard.Add(sailor1);
I just get two errors that say: 我刚刚得到两个错误:
The best overloaded method match for 'System.Collections.Generic.List.Add(string)' has some invalid arguments 'System.Collections.Generic.List.Add(string)'的最佳重载方法匹配具有一些无效参数
Argument 1: cannot convert from 'SailingEvent.ProSailor' to 'string' 参数1:无法从'SailingEvent.ProSailor'转换为'string'
I have also tried: 我也尝试过:
_scoreboard.Add(listBoxBoard[0]);
This doesn't return any errors but does not populate the listbox. 这不会返回任何错误,但不会填充列表框。
In your ProSailer
class you override the ToString()
Function so the Listbox will know what to display. 在
ProSailer
类中,您重写ToString()
函数,以便Listbox知道要显示的内容。 Add you collection of ProSailor
to the Listbox
since it adds items as Objects that includes class objects. 将
ProSailor
集合添加到Listbox
因为它将项目添加为包含类对象的对象。
public override string ToString()
{
return "your string to display";
}
The reason you're getting a type mismatch is because you've initialized your list as List<string>
and then try to add a ProSailor
type object to the list. 您遇到类型不匹配的原因是您已将列表初始化为
List<string>
,然后尝试将ProSailor
类型对象添加到列表中。 If sailor has some string property you could try the following: 如果水手有一些字符串属性,您可以尝试以下方法:
say ProSailor looks something like this: 说ProSailor看起来像这样:
class ProSailor
{
public string MyStringProperty { get; set; }
public ProSailor(int a, int b, int c)
{
}
}
then to accomplish what you're doing: 然后完成你正在做的事情:
List<ProSailor> sailorList = (new ProSailor[]
{
new ProSailor(1, 24, 7),
new ProSailor(2, 23, 14),
new ProSailor(3, 20, 5)
}).ToList();
List<string> stringList = sailorList.Select(s => s.MyStringProperty).ToList();
If your class looks like this one: 如果你的班级看起来像这样:
class ProSailor
{
public ProSailor(int num1, int num2, int num3)
{
Num1 = num1;
Num2 = num2;
Num3 = num3;
}
public int Num1 { get; set; }
public int Num2 { get; set; }
public int Num3 { get; set; }
}
And you want to get List of string with values you should create new method to get this list: 并且您希望获得带有值的字符串列表,您应该创建新方法来获取此列表:
class ProSailor
{
public ProSailor(int num1, int num2, int num3)
{
Num1 = num1;
Num2 = num2;
Num3 = num3;
}
public int Num1 { get; set; }
public int Num2 { get; set; }
public int Num3 { get; set; }
public List<string> GetAllValues()
{
return new List<string> { Num1.ToString(), Num2.ToString(), Num3.ToString() };
}
}
Now if you want create one List of string and combine values from few object you can use AddRange
methond: 现在,如果你想创建一个字符串列表并组合来自少数对象的值,你可以使用
AddRange
methond:
List<string> _scoreboard = new List<string>();
ProSailor sailor1 = new ProSailor(1, 24, 7);
ProSailor sailor2 = new ProSailor(2, 23, 14);
ProSailor sailor3 = new ProSailor(3, 20, 5);
_scoreboard.AddRange(sailor1.GetAllValues());
_scoreboard.AddRange(sailor2.GetAllValues());
_scoreboard.AddRange(sailor3.GetAllValues());
Now you can populate your list using following code: 现在,您可以使用以下代码填充列表:
listBox2.DataSource = _scoreboard;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.