2
protected override void OnPaint(PaintEventArgs e)
{
    if (comboBox1.Text == "Circle")
    {
        e.Graphics.FillEllipse(Brushes.Red, new Rectangle(105, 120, 64, 64));
    }

    if (comboBox1.Text == "Rectangle")
    {
        e.Graphics.FillRectangle(Brushes.Red, new Rectangle(105, 120, 75, 50));
    }

    if(comboBox1.Text == "Triangle")
    {
        Point[] points = { new Point(140, 110), new Point(230, 190), new Point(50, 190) };
        e.Graphics.FillPolygon(Brushes.Red, points);
    }
}

I've created these shapes above and they're within a combo box so but I'm trying to get it so my second combo box will change the color of the shape, is there any way to do this? I can't seem to find it anywhere. The second combo box contains Red, Green, Blue, Custom Colour. I have it so the colour palette comes up but I don't know how to set it to the shape when selected either.

How would I reference the shapes and change the brush color is the part I'm struggling with

Twyxz
  • 199
  • 16
  • You just need to populate the 2nd combobix by iterating over System.Drawing.Colors then use the selected colour in the OnPaint() – Jeremy Thompson Feb 14 '18 at 09:55
  • Use the tags please. Many UIs have combo boxes: WPF/WebForms/WinForms/XamarinForms/WinRT/etc. – György Kőszeg Feb 14 '18 at 09:56
  • 1
    How would I assign that to my shapes? – Twyxz Feb 14 '18 at 09:58
  • Along with combobox1 you also need to check what is selected in combobox2 and based on that you need to apply appropriate brush – Chetan Feb 14 '18 at 09:58
  • Have you got any code example for this? @ChetanRanpariya – Twyxz Feb 14 '18 at 10:14
  • You'll have to create the appropriate brush to paint with. So instead of Brushes.Red, you need to use the [SolidBrush constructor](https://msdn.microsoft.com/en-us/library/system.drawing.solidbrush.solidbrush(v=vs.110).aspx) with the selected color. – Hans Passant Feb 14 '18 at 10:44

2 Answers2

1

May be outdated but just to simplify your code and make it clearer you can use a switch statement

 if ((comboBox1.Text != defaultTextShape) && (comboBox2.Text != defaultTextColour))
        {
            Brush b;
            switch (comboBox1.Text)
            {
                case "Red"   : b = Brushes.Red; break;
                case "Green" : b = Brushes.Green; break;
                case "Blue"  : b = Brushes.Blue; break;
                default      : b = new SolidBrush(CustomColour); break;
            }

            switch (comboBox2.Text)
            {
                case "Rectangle": e.Graphics.FillRectangle(b, new Rectangle(105, 120, 75, 50)); break;
                case "Circle"   : e.Graphics.FillEllipse(b, new Rectangle(105, 120, 64, 64)); break;                    
                default:
                    Point[] points = { new Point(140, 110), new Point(230, 190), new Point(50, 190) };
                    e.Graphics.FillPolygon(b, points); break;
            }


        }

Custom colour is a public const which assigns your Custom colour picker value to it and then assigns it to B

0

As Hans Passant already pointed out: You need to use the SolidBrush object to set the custom colour. This is described in the Post convert from Color to brush.

In your case I would populate the colour ComboBox the following way:

comboBox_Colour.DataSource = new List<Color> { Color.Red, Color.Blue, Color.Yellow, Color.Green };

Now in the OnPaint event you can check whether any items are chosen already and then grab the Color directly from the SelectedItem of comboBox_Colour. Then you plug it into the constructor of a SolidBrush object and feed the SolidBrush to Fill... methods. It will work because SolidBrush inherits from Brush

public sealed class SolidBrush : Brush

protected override void OnPaint(PaintEventArgs e)
{
    if (comboBox_Shape.SelectedIndex != -1 && comboBox_Colour.SelectedIndex != -1)
    {
        Color c = (Color)comboBox_Colour.SelectedItem;
        SolidBrush sb = new SolidBrush(c);
        if (comboBox_Shape.Text == "Circle")
        {
            e.Graphics.FillEllipse(sb, new Rectangle(105, 120, 64, 64));
        }
        else if (comboBox_Shape.Text == "Rectangle")
        {
            e.Graphics.FillRectangle(sb, new Rectangle(105, 120, 75, 50));
        }
        else if (comboBox_Shape.Text == "Triangle")
        {
            Point[] points = { new Point(140, 110), new Point(230, 190), new Point(50, 190) };
            e.Graphics.FillPolygon(sb, points);
        }
    }
}

EDIT:

If the choice of colours that I provided here is not sufficient for you you can dump all colours that the struct Color has to offer into the comboBox_Colour:

List<Color> allColours = typeof(Color).GetProperties(
                     System.Reflection.BindingFlags.Static | 
                     System.Reflection.BindingFlags.Public)
                    .Select(x => (Color)x.GetValue(null)).ToList();

comboBox_Colour.DataSource = allColours;
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • @Twyxz Glad to hear. You are welcome. Was a fun problem to solve :) – Mong Zhu Feb 14 '18 at 13:15
  • Is it possible for the colour to be selected from a Colour palette? Rather than the list of colours @MongZhu – Twyxz Feb 14 '18 at 13:25
  • @Twyxz everything is possible ;) you need the [ColorDialog](https://msdn.microsoft.com/en-us/library/system.windows.forms.colordialog(v=vs.110).aspx) class. Try it. Tt is not very complex. If you have difficulties then you can ask a new question and poke me again ;) I will answer it good luck – Mong Zhu Feb 14 '18 at 14:04