1

I have a ComboBox with 30 options in it. What id like to do is be able to have the program know which option was selected and which download link to use for that option. I can use if else statements but with 30 options that seem incredibly unnecessary.

If option 1, then download link 1. else if option 2, then download link 2.. etc, etc.

This seems like too big of a hassle. Is there a better way to state when an option is picked, use the corresponding download value?

I would like to somehow store a value(the url) of each combobox option (the display text), and then use the selected items value when I need to call the url link. I'm not sure how or if that can be done with windows forms in Visual Studio

cmit
  • 11
  • 2
  • Store the value of the option as the link you wish to download, then just read the value of the combobox. – entropic Sep 08 '16 at 17:59
  • 2
    Use a [dictionary](http://stackoverflow.com/search?q=c%23+dictionary)? Is this combox in asp.net. wpf or winforms? – rene Sep 08 '16 at 18:02
  • I'm using winforms – cmit Sep 08 '16 at 18:07
  • 1
    When creating the ComboBox, load it with Text=NameOfFile, Value=FileURL, then add a SelectedIndexChanged event and read the Value to get the URL of the file to process. – Shannon Holsinger Sep 08 '16 at 18:23
  • seems like you could do that in pure javascript. – Jeremy Holovacs Sep 08 '16 at 18:57
  • @jeremy Holovacs its winforms there is no javascript. – Jonathan Applebaum Sep 08 '16 at 22:44
  • Would it be easier for me to somehow store a value(the url) of each combobox option (the display text), and then use the selected items value when I need to call the url link? I'm not sure how or if that can be done with windows forums in Visual Studio. – cmit Sep 09 '16 at 12:26

4 Answers4

1

By declaring a new class and using DataSource property, you can handle complex objects with ComboBox

    class Item
    {
        public string Name { get; set; }
        public string Url { get; set; }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        List<Item> items = new List<Item>() { new Item() { Name = "Item1", Url = "http://item1" }, new Item() { Name = "Item2", Url = "http://item2" } };
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "Url";
        comboBox1.DataSource = items;
        comboBox1.SelectedValueChanged += ComboBox1_SelectedValueChanged;
    }

    private void ComboBox1_SelectedValueChanged(object sender, EventArgs e)
    {
        if (comboBox1.SelectedValue != null)
        {
            MessageBox.Show(comboBox1.SelectedValue.ToString());
        }

    }
Halis S.
  • 446
  • 2
  • 11
  • Would it be easier for me to somehow store a value(the url) of each combobox option (the display text), and then use the selected items value when I need to call the url link? I'm not sure how or if that can be done. – cmit Sep 09 '16 at 12:25
  • Here is [another useful answer](http://stackoverflow.com/a/3063421/969278) tailored for your needs. – Halis S. Sep 09 '16 at 13:51
0
private void addComboBox(){
   ComboBox cB = new ComboBox();
   ArrayList items = new ArrayList();
   cB.Items.Add(new items("Select a File",""));
   cB.Items.Add(new items("myFile.txt",@"c:\myfile.txt"));
   cB.Items.Add(new items("yourFile.csv",@"d:\oldFiles\yourfile.csv"));
   cB.SelectedIndex=0;
   cB.AutoPostBack = true;
   cB.SelectedIndexChanged = process_CB_File;
   this.Controls.Add(cB);
}
private void process_CB_File(object sender, EventArgs e){
    ComboBox c = sender as ComboBox;
    if(c.SelectedIndex>0){
        string fileURL = c.SelectedValue.ToString();
        //process file
    }
}  
Shannon Holsinger
  • 2,293
  • 1
  • 15
  • 21
0

Use Dictionary.
here is an example how to extract the value (link) by key (combobox selected item).

  private string Example()
        {
            // could be called via button event / selectedIndexChange of event comboBox etc...
            if (Ht.ContainsKey(comboBox1.SelectedItem.ToString()))
                return Ht[comboBox1.SelectedItem.ToString()];
            else
                throw new Exception("Error: Dictionary has no key");
        }

simple example (only for understanding) to initialize Dictionary that has keys that are parallel to combobox values:

    Dictionary<string, string> Ht;
    private void initHtAndCombo()
    {

        Ht = new Dictionary<string, string>();
        Ht.Add("index0ValueOfComboBox", "SomeLink1");
        Ht.Add("index1ValueOfComboBox", "SomeLink2");
        Ht.Add("index2ValueOfComboBox", "SomeLink3");
        Ht.Add("index3ValueOfComboBox", "SomeLink4");

        comboBox1.Items.Add("index0ValueOfComboBox");
        comboBox1.Items.Add("index1ValueOfComboBox");
        comboBox1.Items.Add("index2ValueOfComboBox");
        comboBox1.Items.Add("index3ValueOfComboBox");
    }
Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52
0

I would create a List URLS with containing the links in the same order of the combobox items. Then use the combobox.SelectedIndex to reference the List item. Example:

    List<string> URLS = new List<string>(); //fill this list with your URLs in order of combobox options

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Download(comboBox1.SelectedIndex);
    }
    private void Download(int LinkIndex)
    {
        string Download_Link = URLS[LinkIndex];
    }
Robert Altman
  • 161
  • 11