473,325 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Timer and Random

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
3 6019
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Todd | last post by:
I have an ASP.NET application and I would like to have some code run on the server automatically once a day at a specified time. I create a timer thread to call a simple callback in the...
4
by: Todd | last post by:
I have an ASP.NET application and I would like to have some code run on the server automatically once a day at a specified time. I create a timer thread to call a simple callback in the...
8
by: Todd | last post by:
I have an ASP.NET application and I would like to have some code run on the server automatically once a day at a specified time. I create a timer thread to call a simple callback in the...
7
by: Grahmmer | last post by:
I have a few timers that are added to a form at runtime. I can handle the event fine, but I cannot identify which timer fired. Is there a way to do this? Timer Creation: -------------...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.