Timer and Random

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi,
i have a WELCOME screen. here i have a PICTURE BOX. there are 8 pictures
that go into this Picture Box.

At every 5 seconds i want to RANDOMLY load a new Picture.
this is what i am using but it is not loading the new picture.
Can any one help?


public void Presentation_Load(object sender, System.EventArgs e)
{
Timer timer1= new Timer();
timer1.Interval = 5000;
timer1.Enabled = true;
timer1.Tick += new System.EventHandler (OnTimerEvent);
}

public static void OnTimerEvent(object source, EventArgs e)
{
// 8 pictures
Random objRandom= new Random();
int num= objRandom.Next(1,8);

Presentation obj= new Presentation(); /// presentation is the name of
the class
if(num.ToString()=="1")
{
obj.DisplayCRMInfo(); ///// this function is as below
obj.Refresh();
return;
}
////// 7 more ifs follow
}

public void DisplayCRMInfo()
{
string picture = Application.StartupPath +@"\Presentation\CRM.jpg";
pictureBox1.Image = Image.FromFile(picture);
}
 
Hi VenuGupal,

It's not terribly clear to me what you are actually doing. Are you running all code inside a WinForm class called Presentation? If so, why are you creating a new Presentation on every tick?

Also, in your tick event, you might benefit from using a switch/case instead of if/else if to find out which random number you got, or even better, store all pictures addresses in an string array and load the picture without checking which number it is.

int num = objRandom.Next(1,8);
pictureBox1.Picture = Image.FromFile( pictureAddresses[num] );
 
Hi Venu,

OnTimerEvent(object source, EventArgs e), method is not a static method, its
a reference method.

public void OnTimerEvent(object source, EventArgs e)
{
// 8 pictures
Random objRandom= new Random();
int num= objRandom.Next(1,8);

Presentation obj= new Presentation(); /// presentation is the name of
//the class
if(num.ToString()=="1")
{
obj.DisplayCRMInfo(); ///// this function is as below
obj.Refresh();
return;
}
////// 7 more ifs follow
}

try it now ... GoodLuck... Veera Sekhar...
 
hi Morten,
thanks for ur help. As u suggested i am using a ARRAY and loading the
images. it is working very well.
thanks once again.


Morten Wennevik said:
Hi VenuGupal,

It's not terribly clear to me what you are actually doing. Are you running all code inside a WinForm class called Presentation? If so, why are you creating a new Presentation on every tick?

Also, in your tick event, you might benefit from using a switch/case instead of if/else if to find out which random number you got, or even better, store all pictures addresses in an string array and load the picture without checking which number it is.

int num = objRandom.Next(1,8);
pictureBox1.Picture = Image.FromFile( pictureAddresses[num] );


hi,
i have a WELCOME screen. here i have a PICTURE BOX. there are 8 pictures
that go into this Picture Box.

At every 5 seconds i want to RANDOMLY load a new Picture.
this is what i am using but it is not loading the new picture.
Can any one help?


public void Presentation_Load(object sender, System.EventArgs e)
{
Timer timer1= new Timer();
timer1.Interval = 5000;
timer1.Enabled = true;
timer1.Tick += new System.EventHandler (OnTimerEvent);
}

public static void OnTimerEvent(object source, EventArgs e)
{
// 8 pictures
Random objRandom= new Random();
int num= objRandom.Next(1,8);
Presentation obj= new Presentation(); /// presentation is the name of
the class
if(num.ToString()=="1")
{
obj.DisplayCRMInfo(); ///// this function is as below
obj.Refresh();
return;
}
////// 7 more ifs follow
}

public void DisplayCRMInfo()
{
string picture = Application.StartupPath +@"\Presentation\CRM.jpg";
pictureBox1.Image = Image.FromFile(picture);
}
 
VenuGopal said:
public static void OnTimerEvent(object source, EventArgs e)
{
// 8 pictures
Random objRandom= new Random();
int num= objRandom.Next(1,8);

[Snip]

Please take care, you shouldn't create a new random object every time
you enter the method, because this might not yield a good distribution
of the random numbers. Better initialize one random generator as a field
and re-use it.

Best regards,
Martin
 
Back
Top