ComboBox Code Not working

Dan Cahoon 1 Reputation point
2023-01-07T19:43:15.473+00:00

Why is my combo box not displaying any items? Seems like this should be pretty straight forward. I want to display my own list of items.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ExportImages.Document;

namespace ExportImages
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

    }  
    public class MyColor  
    {  
        private string shortName;  
        private string fullName;  

        public MyColor(string strFullName, string strShortName)  
        {  
            this.shortName = strShortName;  
            this.fullName = strFullName;  
        }  

        public string ShortName  
        {  
            get { return shortName; }  
        }  

        public string FullName  
        {  
            get { return fullName; }  
        }  
    }  

    private object DeserializeObject<T>(DocumentType[] items)  
    {  
        throw new NotImplementedException();  
    }  

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)  
    {  

        ArrayList MyColors = new ArrayList();  
        MyColors.Add(new MyColor("Blue", "B"));  
        MyColors.Add(new MyColor("Red", "R"));  
        MyColors.Add(new MyColor("Green", "G"));  
        MyColors.Add(new MyColor("Cyan", "C"));  
        MyColors.Add(new MyColor("Black", "K"));  

        comboBox1.DisplayMember = "FullName";  
        comboBox1.ValueMember = "ShortName";  
       // comboBox1.DataSource = MyColors;        // would normally add the items like this  

        foreach (MyColor c in MyColors)  
        {  
            comboBox1.Items.Add(c);  
        }  

        comboBox1.SelectedIndex = 0;  
    }  
}  

}

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,825 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 81,636 Reputation points
    2023-01-07T20:16:27.12+00:00

    Fill it at beginning, after InitializeComponent for example, not inside comboBox1_SelectedIndexChanged

    0 comments No comments