tags:

views:

90

answers:

2

I have a timer and in 30 minutes I want to count clicks and show it in a textbox. but how? here is timer code:

decimal sure = 10;
private void button1_Click(object sender, EventArgs e)
{
  button1.Enabled = true;
  timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
  sure--;
  label3.Text = sure.ToString();
  if (sure == 0) 
  {
    timer1.Stop();
    MessageBox.Show("Süre doldu");
  }
}
A: 

Declare your clickCounter at global, and raise your counter++ in Mouse Click Event. If you do it more specific, you can use Background Worker, to track time. and use Application.DoEvents() to write remaining to to textBox Put a button, 2 labels, and a timer. rename labels with lblClickCount and lblRemainingTime

private int clickCounter = 0;
    private void button1_Click(object sender, EventArgs e)
    {
        clickCounter++;
        lblClickCount.Text = clickCounter.ToString();
    }

    decimal sure = 10;
    private void timer1_Tick(object sender, EventArgs e)
    {
        sure--;
        lblRemainingTime.Text = sure.ToString();
        Application.DoEvents();
        if (sure == 0)
        {
            timer1.Stop();
            MessageBox.Show("Süre doldu. Toplam tiklama sayisi:" + clickCounter.ToString());
        }
    }
Serkan Hekimoglu
Serkan bey kod olarak açıklarmısınız? Genel olarak eksik olduğum konu her tıklamada bir işlem yapmak. mesela bir int a=0; olsun. 1. tıklamada a=1, 2. tıklamada a=2 olsun istiyorum.
karatekid
I am editing my post, writing the code you want
Serkan Hekimoglu
where? should i wait? thanx for helping..
karatekid
you can check now. The code runs what do u want. c/p my code and run
Serkan Hekimoglu
YEAH it totaly work! thank youuuuuuuuuuu !!!!!
karatekid
Nice, your welcome. iyi calismalar dilerim. kod konusunda profilimde email adresim var, daha hizli sonuc alabilirsin birsey yazmak istediginde aklinda bulunsun :)
Serkan Hekimoglu
çok teşekkürler Serkan Bey. mail adresinizi alıyorum umarım çok rahatsızlık vermem =)one more last question?what is Application.DoEvents(); ? what's its job?
karatekid
for ex while in process, you may want update some fields in UI during process, after update command, Application.DoEvents() comes to below, to update your UI changes. This code can work without Application.DoEvents() also
Serkan Hekimoglu
A: 

If you wanted to reuse buttoN1 to count the clicks but not Start new timer you can add a if around the code you want to protect.

bool hasTimerStarted = false;
int numberOfClicks = 0;
private void button1_Click(object sender, EventArgs e)
{
  if(!hasTimerStarted)
  {
      button1.Enabled = true;
      timer1.Start();
      hasTimerStarted = true;
  }
  ++numberOfClicks;
}

When the timer expires you reset the count and if the timer has started.

private void timer1_Tick(object sender, EventArgs e)
{

    TimeSpan ts = stopWatch.Elapsed;

    // Format and display the TimeSpan value.
    string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
        ts.Hours, ts.Minutes, ts.Seconds,
        ts.Milliseconds / 10);

    label3.Text = elapsedTime;
    labelClicks.Text = "User clicked " + clicksNo.toString() + "nt times..";

    if (stopWatch.ElapsedMilliseconds >= this.minutes * 60 * 1000) 
    {
        timer1.Stop();
        MessageBox.Show("Time elapsed.");
        hasTimerStarted = false;
        numberOfClicks = 0;
    }
}
Wesley