Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello
OK my question might be a bit vague. Say i have two images i-e "image 1" and "image 2" and i want both of them to be displayed one after the other one in the same picturebox say picturebox1. Now one way that might not be a smart way of doing it is
C#
while(true)
{
picturebox1.image=image 1;
picturebox1.refresh();
pictureboz1.image=image 2;
picturebox1.refresh();

}

Is there any other way of doing it.?
Thanks anyways
Posted
Updated 21-Sep-13 0:11am
v2

Well, no, that's not a smart way - for a couple of reasons:
Firstly, it'll probably not show any image at all because it sits in a loop changing them and never gets a chance to do the Paint event for anything...
Secondly, because if it did show anything, it would be replaced so quickly that the user probably wouldn't notice it at all...

It's difficult to say exactly what to do, because you don;t specify the environment you are running in, but the use of PictureBox implies WinForms, so I'll go with that:
1) Add a Timer to your form, and set it's Interval property to 1000 - that's a one second interval. Start the timer.
3) Add a private bool field to your form class, call it showFirst and set it to true
2) Handle the Timer.Tick event, and do this:
C#
pictureBox1.Image = showFirst ? image1 : image2;
showFirst = !showFirst;
That should change your image every second.
 
Share this answer
 
Comments
loraloper_22 21-Sep-13 6:32am    
That is fine but the problem is i want to display it in a loop which i am not able to do by your solution. Say i want something like user to pres a button and then it should start switching images in a loop I-e continuously doing this task. I hope i am not bothering but i am just a beginner in this programming world
OriginalGriff 21-Sep-13 6:40am    
Um. Did you try it? :laugh:
That's what the showFirst variable is there for - it toggles from true to false each time, and causes the image to change. Because the Tick event runs continually once the timer is started, each second the value of showFirst changes, and so does the image.

If you want it to start when the user presses a button, start the timer running in the button click handler.
loraloper_22 21-Sep-13 6:48am    
my bad :P
loraloper_22 21-Sep-13 6:56am    
Still now working ok let me explain it a bit further i created ShowFirst and set it to true then i created a windows.form timer and now this is inside my button clic handler
{
t1=1000;
t1.start();
picturebox1.image=showFirst ? image 1: image 2;
showFirst=!showFirst;
}
and when i debug it the code it runs only for one iteration and then leaves the event handler?Do i need to call the tickevent somewhere?
Beginner so excuse my silly mistakes
OriginalGriff 21-Sep-13 7:34am    
No, read the instructions: the last two lines should be in the Timer.Tick event handler so they happen each time the timer fires.
(and when you show code, it's a good idea to copy and paste it - the sample you show won't compile) ;)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900