简体   繁体   中英

C# Winforms Shorter Way to Add Items into ComboBox

May I know is there a shorter way to add items to the 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?

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?

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.

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.

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.

Use ListBox.ObjectCollection.AddRange
You can use it like this:
comboComSymbol.Items.AddRange(new string[] {"ABC", "DEF", "GHI"});

use generic List<T> to databind.

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:

loadSharePricesComboBox(@"..\..\sharePriceXML\");

This will load all the XML file names, without their extensions, giving you the list you require.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM