C# Timer

Status
Not open for further replies.

Blue

Is Stylin' On You
Original poster
senior
member
Joined
Mar 18, 2011
Posts
2,485
Reaction score
1,391
Points
147
I need help with a timer.

I honestly don't know how to implement one. My program works like this. It loads a webpage, then has to wait 1 minute before clicking a link. I have a numericupdownbox linked for the amount of times I want it to run, as well as the link's attribute. However, I can only click the html link by having to click my button1 each minute...

What could do I implement, so when I start my program, it repeats all the processes (just clicking the link) by waiting a full minute?
 

Flip

Old School
old school
member
Joined
Jul 23, 2009
Posts
263
Reaction score
69
Points
108
Add a timer into your GUI and change the interval to 1000.
You've now adjusted the interval to 1000ms so that each 'tick' is one second.

Now double click on the timer in your GUI (it should in a gray box below your actual "GUI", this is okay).

Code:
 private void timer1_Tick(object sender, EventArgs e)
        {
            if (timeLeft > 0)
            {
                // Display the new time left
                // by updating the Time Left label.
                timeLeft = timeLeft - 1;
                timeLabel.Text = timeLeft + " seconds";
            }
            else
            {
                // If time has run out, stop the timer.
                timer1.Stop();
                timeLabel.Text = "Time's up!";
            }
        }
timeLabel is a textbox that simply shows the amount of time left.

timeLeft is the integer it will count down from.

In your case you can put something like this in your code afterwards

Code:
timeLeft = 60;
timer1.Start();
and then call your method or do whatever you wanted to do after waiting the 60 seconds
 
  • Like
Reactions: Blue

Blue

Is Stylin' On You
Original poster
senior
member
Joined
Mar 18, 2011
Posts
2,485
Reaction score
1,391
Points
147
I don't get the timeLeft part, here's what I got so far though

namespace -----
{
public partial class Form1 : Form
{
public int count;
public int max;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
count = 0;
max = (int)numericUpDown1.Value;
webBrowser1.Navigate("------");

}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement link = webBrowser1.Document.GetElementById("time"); link.InvokeMember("Click");
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void timer1_Tick(object sender, EventArgs e)
{

{
if (timeLeft > 0)
{
timeLeft = 20;
timer1.Start();
// Display the new time left
// by updating the Time Left label.
timeLeft = timeLeft - 1;
timeLabel.Text = timeLeft + "seconds";
}
else
{
// If the user ran out of time, stop the timer, show
// a MessageBox, and fill in the answers.
timer1.Stop();
timeLabel.Text = "Time's up!";
}
}
}
}
}

I took out the website for personal reasons, but that's the line of code i currently have
 

Flip

Old School
old school
member
Joined
Jul 23, 2009
Posts
263
Reaction score
69
Points
108
timeLeft is simply how many seconds you wish to count down from

This code you have in Webbrowser.DocumentCompleted

Code:
HtmlElement link = webBrowser1.Document.GetElementById("time");
link.InvokeMember("Click");
You should put that into it's own method like so:

Code:
public void click() 
{
HtmlElement link = webBrowser1.Document.GetElementById("time"); 

link.InvokeMember("Click");
}
Now, add in the timer function.

Code:
public void click() 
{
HtmlElement link = webBrowser1.Document.GetElementById("time"); 

link.InvokeMember("Click");

timeLeft = 60;

timer1.Start();
}
and finally, we want this to loop every 60 seconds indefinitely so we call the method recursively (otherwise use a for loop)

Code:
public void click() 
{
HtmlElement link = webBrowser1.Document.GetElementById("time"); 

link.InvokeMember("Click");

timeLeft = 60;

timer1.Start();

click();
}
I believe that should work.
 
  • Like
Reactions: Blue

Blue

Is Stylin' On You
Original poster
senior
member
Joined
Mar 18, 2011
Posts
2,485
Reaction score
1,391
Points
147
Alright, so here's what I have,

namespace
{
public partial class Form1 : Form
{
public int count;
public int max;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
count = 0;
max = (int)numericUpDown1.Value;
webBrowser1.Navigate("/");
{
HtmlElement link = webBrowser1.Document.GetElementById("time");

link.InvokeMember("Click");

timeLeft = 60;

timer1.Start();

Click();
}

}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement link = webBrowser1.Document.GetElementById("time"); link.InvokeMember("Click");
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void timer1_Tick(object sender, EventArgs e)
{
{

{
if (timeLeft > 0)
{
timeLeft = 20;
timer1.Start();
// Display the new time left
// by updating the Time Left label.
timeLeft = timeLeft - 1;
timeLabel.Text = timeLeft + "seconds";
}
else
{
// If the user ran out of time, stop the timer, show
// a MessageBox, and fill in the answers.
timer1.Stop();

}
}
}
}
}
}

However, anywhere there is "timeLeft", it says it doesn't exist in the current context. Also, the click I bolded in the code says the even can only be displayed on the left hand side of += or -=
 

Flip

Old School
old school
member
Joined
Jul 23, 2009
Posts
263
Reaction score
69
Points
108
Code:
public void click()
{
}
Is a different method from

Code:
private void button1_Click(object sender, EventArgs e)
{
}
perhaps I should have called it something else.

However, anywhere there is "timeLeft", it says it doesn't exist in the current context.
You need to declare timeLeft as an integer variable above.

Code:
int timeLeft;
 

Blue

Is Stylin' On You
Original poster
senior
member
Joined
Mar 18, 2011
Posts
2,485
Reaction score
1,391
Points
147
Code:
public void click()
{
}
Is a different method from

Code:
private void button1_Click(object sender, EventArgs e)
{
}
perhaps I should have called it something else.

You need to declare timeLeft as an integer variable above.

Code:
int timeLeft;

So then what should my final code be?
 

Flip

Old School
old school
member
Joined
Jul 23, 2009
Posts
263
Reaction score
69
Points
108
Code:
public partial class Form1 : Form
{
public int count, max, timeLeft;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

count = 0;
max = (int)numericUpDown1.Value;

webBrowser1.Navigate("/");

}

public void click() 
{
HtmlElement link = webBrowser1.Document.GetElementById("time"); 

link.InvokeMember("Click");

timeLeft = 60;

timer1.Start();
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
click();
}

private void timer1_Tick(object sender, EventArgs e)
{
{
{
if (timeLeft > 0)
{
timeLeft = timeLeft - 1;
timeLabel.Text = timeLeft + "seconds";
}
else
{
timer1.Stop();
timeLabel.Text = "Time's up!";
click();
}
}
}
}
}
}
 
  • Like
Reactions: Blue

Blue

Is Stylin' On You
Original poster
senior
member
Joined
Mar 18, 2011
Posts
2,485
Reaction score
1,391
Points
147
Thanks, only one more problem now


Error 1 'FormName.Form1' does not contain a definition for 'Form1_Load' and no extension method 'Form1_Load' accepting a first argument of type 'FormName.Form1' could be found (are you missing a using directive or an assembly reference?) C:\Users\\Documents\Visual Studio 2008\Projects\PRoject1\PRoject1\Form1.Designer.cs 88 55 PRoject1
 

Flip

Old School
old school
member
Joined
Jul 23, 2009
Posts
263
Reaction score
69
Points
108
Code:
private void Form1_Load(object sender, EventArgs e)
{
}
Add that in with the other methods
 
  • Like
Reactions: Blue

Blue

Is Stylin' On You
Original poster
senior
member
Joined
Mar 18, 2011
Posts
2,485
Reaction score
1,391
Points
147
Code:
private void Form1_Load(object sender, EventArgs e)
{
}
Add that in with the other methods

Yep it worked. Ran into another problem now,

I debugged it, but the seconds count down way to fast, and they don't click the link after the amount of time has passed.
 

Flip

Old School
old school
member
Joined
Jul 23, 2009
Posts
263
Reaction score
69
Points
108
To quote my first post.

Add a timer into your GUI and change the interval to 1000.
You've now adjusted the interval to 1000ms so that each 'tick' is one second.

The second part is harder for me to debug since I don't have access to the webpage.
 
  • Like
Reactions: Blue

Blue

Is Stylin' On You
Original poster
senior
member
Joined
Mar 18, 2011
Posts
2,485
Reaction score
1,391
Points
147
To quote my first post.



The second part is harder for me to debug since I don't have access to the webpage.

Here's the webpage:

I fixed the timer in the gui, so it's counting down normal now. However, it just restarts the counting without doing anything.
 

Flip

Old School
old school
member
Joined
Jul 23, 2009
Posts
263
Reaction score
69
Points
108
The problem is with the way you're clicking it.

Code:
HtmlElement link = webBrowser1.Document.GetElementById("time"); 

link.InvokeMember("Click");

At least in my testing, this didn't click the "refresh" link, but since all this link does is refresh the page anyway we can tell the browser to do that.

Update your click method to this:

Code:
public void click() 
{
webBrowser1.Refresh();

timeLeft = 60;

timer1.Start();
}
 
  • Like
Reactions: Blue

Blue

Is Stylin' On You
Original poster
senior
member
Joined
Mar 18, 2011
Posts
2,485
Reaction score
1,391
Points
147
Ohh I see! I got it. Thanks so much for the help Flip :)

Never mind, got it. Thanks so much for the help!
 
Status
Not open for further replies.