Forum Xamarin.Android

My repeat button doesn't work well

shaywshayw Member ✭✭

Hi
I wrote a simple repeat button using a button and a timer

 private Button upsec ;
private  textview sec;
 private Timer timerupsec = new Timer();
 upsec.Touch += Upsec_Touch;
private void Upsec_Touch(object sender, View.TouchEventArgs e)
        {
            // throw new NotImplementedException();
            switch (e.Event.Action)
            {
                case MotionEventActions.Down:
                    if(seconds < 60)
                    {
                        timerupsec.Start();
                        timerupsec.Elapsed += OnTimeupsecEvent;
                    }
                    break;

                case MotionEventActions.Up:
                    timerupsec.Stop();

                    break;
            }
        }

private void OnTimeupsecEvent(object sender, ElapsedEventArgs e)
        {
            seconds++;
            if (seconds < 60)
            {
                sec.text = seconds.tostring();
               timerupsec.Interval = 200;
            }
        }

When i run the first time and the button is pressed down the sec displays 1 2 3 4 as well
but if i release the button and press down again i get 6 8 10.
Every time i release and press the button the gap increases !!

Thanks

Tagged:

Best Answer

  • Tracy320Tracy320 Member ✭✭✭
    Accepted Answer

    I did a test, and I found that the value of seconds is increased sequentially, but the text of sec.Text is not displayed correctly using code sec.Text = seconds.ToString();.
    In other words, the value of seconds and the text of sec.Text are not identical.

Answers

  • Tracy320Tracy320 Member ✭✭✭
    Accepted Answer

    I did a test, and I found that the value of seconds is increased sequentially, but the text of sec.Text is not displayed correctly using code sec.Text = seconds.ToString();.
    In other words, the value of seconds and the text of sec.Text are not identical.

Sign In or Register to comment.