【问题标题】:Adding more comboboxItems in combobox with value and Text在带有值和文本的组合框中添加更多组合框项
【发布时间】:2013-03-27 16:58:18
【问题描述】:

我自己创建了一个名为 ComboBoxitem 的类,它有两个属性:Value 和 text。

public class ComboBoxItem
{
    public string Value;

    public string Text;

    public ComboBoxItem(string val, string text)
    {

        Value = val;

        Text = text;
    }
    public override string ToString()
    {
        return Text;
    }

}

现在我想每次都向组合框项添加一个值和一个文本

类似这样的:

public ComboBoxItem busstops;

        private void Form1_Load(object sender, EventArgs e)
        {
            lblText.Text = "Timetable Bushours for " + "New Bridge Street-St Dominics";

            busstops = new ComboBoxItem("410000015503", "New Bridge Street-St Dominics");
            busstops = new ComboBoxItem("410000015552", "Bothal Street (N-Bound), Byker ");

        /*    comboBox1.Items.Add(new ComboBoxItem ("410000015503", "New Bridge Street-St Dominics"));
            comboBox1.Items.Add(new ComboBoxItem("410000015552", "Bothal Street (N-Bound), Byker "));*/


            comboBox1.Items.Add(busstops);
        }

但问题是只添加了最后一项(正常,因为我总是说new ComboboxItem)但是如何更改代码,他总是可以添加新的comboboxitems?

谢谢!

【问题讨论】:

  • 主要是因为我很好奇...为什么您注释掉的代码不起作用?它看起来与它应该的完全一样,并且应该可以正常工作。您是否需要其他地方的 busstops 变量?在这种情况下,您应该将其设为某种集合或列表,而不是单个变量......并且它也包含不同的问题......

标签: c# combobox


【解决方案1】:

两个 ComboBox 项是不同的对象,因此您需要两个 ComboBox 变量来存储它们。

 busstops1 = new ComboBoxItem("410000015503", "New Bridge Street-St Dominics");
 busstops2 = new ComboBoxItem("410000015552", "Bothal Street (N-Bound), Byker ");


 comboBox1.Items.Add(busstops1);
 comboBox1.Items.Add(busstops2);

【讨论】:

    【解决方案2】:

    每次更新busstops 实例时将其添加到comboBox1.Items

            private void Form1_Load(object sender, EventArgs e)
            {
                lblText.Text = "Timetable Bushours for " + "New Bridge Street-St Dominics";
    
                busstops = new ComboBoxItem("410000015503", "New Bridge Street-St Dominics");
                comboBox1.Items.Add(busstops);
    
                busstops = new ComboBoxItem("410000015552", "Bothal Street (N-Bound), Byker ");
                comboBox1.Items.Add(busstops);
            }
    

    【讨论】:

      【解决方案3】:

      你应该在每次分配之后添加。

              busstops = new ComboBoxItem("410000015503", "New Bridge Street-St Dominics");
      
              comboBox1.Items.Add(busstops); // Add this line
      
              busstops = new ComboBoxItem("410000015552", "Bothal Street (N-Bound), Byker ");
              comboBox1.Items.Add(busstops);
      

      【讨论】:

        【解决方案4】:

        制作一个ComboBoxItems列表,添加项目到列表中,并将comboBox1的DataSource设置到列表中:

            List<ComboBoxItem> Items = new List<ComboBoxItem>();
            comboBox1.DataSource = Items;
        

        【讨论】:

          【解决方案5】:
              private void Form1_Load(object sender, EventArgs e)
              {
                  lblText.Text = "Timetable Bushours for " + "New Bridge Street-St Dominics";
          
                  busstops = new ComboBoxItem("410000015503", "New Bridge Street-St Dominics");
                  comboBox1.Items.Add(busstops);//add this line here
          
                  busstops = new ComboBoxItem("410000015552", "Bothal Street (N-Bound), Byker ");
                  comboBox1.Items.Add(busstops);//and again here
              }
          

          由于您要添加相同的值,因此每次更改时都必须添加该值。通过声明new,您实际上是在替换和覆盖旧值。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-12-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多