By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,247 Members | 871 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,247 IT Pros & Developers. It's quick & easy.

Timer and Random

P: n/a
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);
}

Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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] );
On Mon, 11 Jul 2005 09:34:01 +0200, VenuGopal <Ve*******@discussions.microsoft.com> wrote:
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);
}


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2

P: n/a
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...

"VenuGopal" wrote:
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);
}

Nov 17 '05 #3

P: n/a
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" wrote:
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] );
On Mon, 11 Jul 2005 09:34:01 +0200, VenuGopal <Ve*******@discussions.microsoft.com> wrote:
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);
}


--
Happy coding!
Morten Wennevik [C# MVP]

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.