[英]C# Winforms Shorter Way to Add Items into ComboBox
May I know is there a shorter way to add items to the comboBox? 我可以知道有没有更短的方法将项目添加到comboBox? Currently I am only adding 20 items which already seems very long, what if I have a 100 items to add into the comboBox?
目前,我只添加20个看起来已经很长的项目,如果我有100个项目要添加到comboBox中,该怎么办?
My code: 我的代码:
private void loadSharePricesComboBox()
{
comboComSymbol.Items.Add("BARC");
comboComSymbol.Items.Add("DEB");
comboComSymbol.Items.Add("DOM");
comboComSymbol.Items.Add("EZJ");
comboComSymbol.Items.Add("GFS");
comboComSymbol.Items.Add("IHG");
comboComSymbol.Items.Add("JD.");
comboComSymbol.Items.Add("LAD");
comboComSymbol.Items.Add("LLOY");
comboComSymbol.Items.Add("MRW");
comboComSymbol.Items.Add("NXT");
comboComSymbol.Items.Add("OCDO");
comboComSymbol.Items.Add("RBS");
comboComSymbol.Items.Add("SMWH");
comboComSymbol.Items.Add("SPD");
comboComSymbol.Items.Add("STAN");
comboComSymbol.Items.Add("SYR");
comboComSymbol.Items.Add("TALK");
comboComSymbol.Items.Add("TSCO");
comboComSymbol.Items.Add("WMH");
comboComSymbol.SelectedIndex = -1;
}
Your help is much appreciated! 非常感谢您的帮助! Thank you.
谢谢。 :)
:)
Addition code (for the question i asked Simon Whitehead): 附加代码(针对我问西蒙·怀特海德的问题):
private void btnDownloadXML_Click(object sender, EventArgs e)
{
using (WebClient client = new WebClient())
{
client.DownloadFile("http://www.lse.co.uk/chat/" + comboDownloadXML.SelectedItem,
@"..\..\sharePriceXML\" + comboDownloadXML.SelectedItem + ".xml");
}
MessageBox.Show("Download Completed! File has been placed in the folder sharePriceXML!");
}
Have you tried the AddRange()
method? 您是否尝试过
AddRange()
方法?
I haven't tested: 我还没有测试:
private void loadSharePricesComboBox()
{
comboComSymbol.Items.AddRange(new string[]{"BARC", "DEB", ... etc});
comboComSymbol.SelectedIndex = -1;
}
The MSDN on .AddRange
might give you a better idea. .AddRange
上的MSDN可能会给您带来更好的主意。
foreach(var item in "BARC,DEB,DOM,...".Split(',')) comboComSybol.Items.Add(item);
or 要么
var items = new [] { "BARC", "DEV", "DOM" };
foreach(var item in items) comboComSymbol.Items.Add(item);
or you can save even more code and use AddRange on the above 2 methods. 或者您可以保存更多代码,并在上述2种方法上使用AddRange。
var items = new [] { "BARC", "DEV", "DOM" };
comboComSymbol.Items.AddRange(items);
If you are starting a new project though, have a look at WPF instead of winforms. 但是,如果您要开始一个新项目,请查看WPF而不是Winforms。
Use ListBox.ObjectCollection.AddRange 使用ListBox.ObjectCollection.AddRange
You can use it like this: 您可以像这样使用它:
comboComSymbol.Items.AddRange(new string[] {"ABC", "DEF", "GHI"});
use generic List<T>
to databind. 使用通用
List<T>
进行数据绑定。
class Symbols
{
public string Name{get;set;}
}
var Symb= new List<Symbols> { new Symbols() { Name = "Abc"}, new Person() { Name = "BC" }};
comboBox1.DisplayMember = "Name";
comboBox1.DataSource = Symb;
comboBox1.DataBindings.Add("SelectedItem", Symb, "Name");
To save on code size.. why not list them in a file? 为了节省代码大小。为什么不将它们列出在文件中?
void loadSharePricesComboBox(string fileName) {
using (StreamReader sr = new StreamReader(fileName)) {
while (!sr.EndOfStream) {
comboComSymbol.Items.Add(sr.ReadLine());
}
}
}
EDIT: In response to your comment.. I would just load the files, without extensions.. that would be much easier: 编辑:响应您的评论..我只是加载文件,没有扩展名..那会容易得多:
void loadSharePricesComboBox(string path) {
foreach (string file in Directory.GetFiles(path, "*.xml")) {
comboComSymbol.Items.Add(Path.GetFileNameWithoutExtension(file));
}
}
Pass in the path you want to load the XML file names from, perhaps like this: 传递您要从中加载XML文件名的路径,也许像这样:
loadSharePricesComboBox(@"..\..\sharePriceXML\");
This will load all the XML file names, without their extensions, giving you the list you require. 这将加载所有XML文件名(不带扩展名),为您提供所需的列表。
this code : 此代码:
string[] str = {
"BARC","DEB","DOM","EZJ","GFS","IHG","JD.","LAD","LLOY","MRW",
"NXT","OCDO","RBS","SMWH","SPD","STAN","SYR","TALK","TSCO","WMH"
};
loadSharePricesComboBox(str);
your method : 您的方法:
private void loadSharePricesComboBox(string[] strArr)
{
comboComSymbol.Items.AddRange(strArr);
comboComSymbol.SelectedIndex = -1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.