【问题标题】:Select a ComboBox option and download a file选择一个组合框选项并下载一个文件
【发布时间】:2017-01-16 18:24:57
【问题描述】:

我有一个包含 30 个选项的 ComboBox。 id 喜欢做的是能够让程序知道选择了哪个选项以及用于该选项的下载链接。我可以使用 if else 语句,但有 30 个选项似乎非常不必要。

如果选项 1,则下载链接 1。否则,如果选项 2,则下载链接 2.. 等等。

这似乎太麻烦了。有没有更好的方式来说明何时选择了一个选项,使用相应的下载值?

我想以某种方式存储每个组合框选项(显示文本)的值(url),然后在我需要调用 url 链接时使用所选项目的值。我不确定如何或是否可以在 Visual Studio 中使用 Windows 窗体来完成

【问题讨论】:

  • 将选项的值存储为您要下载的链接,然后读取组合框的值。
  • 使用dictionary?这是 asp.net 中的组合框吗? wpf 还是 winforms?
  • 我正在使用 winforms
  • 创建 ComboBox 时,使用 Text=NameOfFile, Value=FileURL 加载它,然后添加 SelectedIndexChanged 事件并读取 Value 以获取要处理的文件的 URL。
  • 似乎你可以用纯 javascript 做到这一点。

标签: c# winforms combobox


【解决方案1】:

通过声明一个新类并使用DataSource 属性,您可以使用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());
        }

    }

【讨论】:

  • 我会更容易以某种方式存储每个组合框选项(显示文本)的值(url),然后在我需要调用 url 链接时使用所选项目值吗?我不确定如何或是否可以做到。
  • 这里是为您量身定制的another useful answer
【解决方案2】:
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
    }
}  

【讨论】:

    【解决方案3】:

    使用Dictionary
    这是一个示例如何通过key(组合框选定项)提取value(链接)。

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

    简单示例(仅用于理解)初始化具有与组合框值平行的键的字典:

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

    【讨论】:

      【解决方案4】:

      我将创建一个列表 URL,其中包含与组合框项的顺序相同的链接。然后使用 combobox.SelectedIndex 来引用 List 项。示例:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-09
        • 1970-01-01
        相关资源
        最近更新 更多