In this example,we’ll learn to display list of all colors in a ComboBox and setting the colours in background color of a C# Windows Form.
Source Code:
Form_Load
1 2 3 4 5 6 7 8 9 10 11 12 | private void Form1_Load(object sender, EventArgs e) { foreach(System.Reflection.PropertyInfo prop in typeof(Color).GetProperties()) { if(prop.PropertyType.FullName=="System.Drawing.Color") { cmbColor.Items.Add(prop.Name); } } } |
comboBox1_SelectedIndexChanged
1 2 3 4 5 6 7 | private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { Color color = Color.FromName(cmbColor.Text); this.BackColor = color; } |
When the above code is set, and the project is executed using Visual Studio, you will get the below output.