From: Tony Johansson on
Hi!

Here I have a fully working custom control that shows a traffic light where
I can select a StateColourTrafficLight property in this object and change
the colour on the traffic light to red or yellow or green. This work
perfect.

There is one this that I don't fully understand and that is if I move this
row
public enum TrafficLight { red, yellow, green}
within the class. I get the following error when I try to open the form
designer where I will use the custom control.
"The field 'red' could not be found on the target object. make sure that the
field is defined as an instance variable on the target object and has the
correct scope"

So what do I have to change if I want to have this row
public enum TrafficLight { red, yellow, green}
within the class instead of outside the class ?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Lab5
{
public enum TrafficLight { red, yellow, green}

public partial class MyCustomControl : Control
{
private TrafficLight stateColourTrafficLight;
private const int circleSize = 80;
private const int borderSize = 10;

public TrafficLight StateColourTrafficLight
{
get { return stateColourTrafficLight; }
set { stateColourTrafficLight = value; }
}

public MyCustomControl()
{
InitializeComponent();
stateColourTrafficLight = TrafficLight.red;
base.Size = new Size(110, 265);
}

protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
pe.Graphics.FillRectangle(Brushes.Black, new Rectangle(5, 5, 90,
270));

switch (stateColourTrafficLight)
{
case TrafficLight.red:
pe.Graphics.FillEllipse(Brushes.Red, new
Rectangle(borderSize, borderSize, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, 95, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, 180, circleSize, circleSize));
break;

case TrafficLight.yellow:
pe.Graphics.FillEllipse(Brushes.Yellow, new
Rectangle(borderSize, 95, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, borderSize, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, 180, circleSize, circleSize));
break;

case TrafficLight.green :
pe.Graphics.FillEllipse(Brushes.Green, new
Rectangle(borderSize, 180, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, borderSize, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, 95, circleSize, circleSize));
break;
}
}
}
}

//Tony