In this example,we’ll learn to display list of all colors in a ListBox and setting the colours in background color of a C# Windows Form.
When the code is set, and the project is executed using Visual Studio, you will get the below output.
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") { listBox1.Items.Add(prop.Name); } } } |
listBox1_SelectedIndexChanged Code:
1 2 3 4 5 6 7 |
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { Color color = Color.FromName(listBox1.Text); this.BackColor = color; } |