Handle the Form Paint event, and add a Timer to your form.
Set the Timer to an interval of a few seconds (the Interval Property is in milliseconnds, so 1000 is a second) and handle the timer Tick event.
Add a variable to your form class, int will do for the moment, but an enum would be better.
In the Tick event handler, increase the value of your variable by one, and call the Invalidate method.
In the Paint event, check the value of the variable, modulo 4 - this will gace a value between 0 and 3 inclusive.
If it is 0, do not draw anything
If it is 1, Draw your circle in the middle.
If it is 2, Draw your circle to the left.
If it is 3, Draw your circle to the right.
You can draw the circle using the e.Graphics context provided in the handler, and by calling its
DrawEllipse method[
^]
Pen blackPen = new Pen(Color.Black, 3);
int width = 200;
int height = 200;
int x = (Width - width) / 2;
int y = 0;
e.Graphics.DrawEllipse(blackPen, x, y, width, height);
That will draw your circle, horizontally centred.
You will have to play with the positions of your left and right circles, as screen sizes vary, so moving "5 inches to the left" is harder to work out.
"Above code is not working ?
I have 3 Images (Image1, Image2, Image3),
Using a timer control with interval value of 5000, i.e 5second, I
want to accomplish the following:
- Initially, only Image1 is visible to the user middle of the form, i.e the 2 images
are set to be invisible.
- When load the form appear and image1
appears exact middle of the form
- After 5 seconds, the image1 disappear and image 2 appears right side.
- After 5 seconds, the image2 disappear and image3 appears left side
- After 5 seconds, the image3 disappear and the image1 appear
I am setting timer control's Enable property to "True" in Design
time.
I am setting timer control's Interval property value to 5000 in
Design Time.
I assume that I do all the coding but not working only displaying two images
Can anyone help me code what I want to accomplish?"
First, check that you are handling the Timer Tick event - highlight the timer then look in the properties pane, click the Events button (it looks like a lightning bolt) and check the Tick event. If you have no handler, creat one by double clicking on it.
In the event handler method you will need:
A class level "mode" variable:
private enum ShowImage
{
FirstOnly,
SecondOnly,
ThirdOnly,
}
private ShowImage showWhichImage = ShowImage.FirstOnly;
In the handler method, you use this to determine what to show next:
switch(showWhichImage)
{
case ShowImage.FirstOnly:
Image1.Visible = false;
Image2.Visible = true;
Image3.Visible = false;
showWhichImage = ShowImage.SecondOnly;
break;
case ShowImage.SecondOnly:
Image1.Visible = false;
Image2.Visible = false;
Image3.Visible = true;
showWhichImage = ShowImage.ThirdOnly;
break;
case ShowImage.ThirdOnly:
Image1.Visible = true;
Image2.Visible = false;
Image3.Visible = false;
showWhichImage = ShowImage.FirstOnly;
break;
default:
throw new Exception("Unknown ShowImage encountered: " + showWhichImage.ToString());
}