Reputation: 268
So, I'm making a homework assignment in which I need to build an intersection along with automatic traffic lights, etc. in Windows Form.
Now, for testing purposes I made a button that changes the traffic light status from on to off (or vice-versa). The status is a enum called LampStatus which uses Aan (On), Uit (Off) and Storing (This doesn't need to get used yet).
The lamp class looks like this:
public class Lamp
{
protected Color kleur;
protected int x, y, straal;
protected LampStatus status;
public Color Kleur
{
get
{
if (status == LampStatus.Uit)
return Color.Gray;
else
return kleur;
}
set { kleur = value; }
}
public LampStatus Status
{
set
{
status = value;
}
get
{
return status;
}
}
// constructor
public Lamp()
{
kleur = Color.Red;
x = y = 0;
straal = 1;
status = LampStatus.Uit;
}
// constructor
public Lamp(Color kleur, int x, int y, int r)
{
this.kleur = kleur;
this.x = x;
this.y = y;
this.straal = r;
status = LampStatus.Uit;
}
public virtual void Teken(Graphics g)
{
if (g != null)
{
SolidBrush Brush = new SolidBrush(Kleur);
g.FillEllipse(Brush, x, y, straal, straal);
g.DrawEllipse(new Pen(Color.Black), x, y, straal, straal);
}
}
}
Now, when I press the button in Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Lamp lamp = new Lamp(Color.Red, 15, 15, 50);
private void Form1_Paint(object sender, PaintEventArgs e)
{
lamp.Teken(e.Graphics);
}
private void testButton_Click(object sender, EventArgs e)
{
if (lamp.Status == LampStatus.Uit)
lamp.Status = LampStatus.Aan;
else
lamp.Status = LampStatus.Uit;
}
}
Nothing seems to happen, though when I debug the object lamp, both the color has changed to Color.Red and the status has changed to LampStatus.Aan.
When I hardcode: lamp.Status = LampStatus.Aan in the Form1_Paint method the color does change to red.
Edit; if there is any confusion, just comment and I'll try to explain.
Upvotes: 0
Views: 99
Reputation: 268
Using this.Refresh() fixed the problem and made the lamp color properly change.
Upvotes: 2