C# - مؤقت العد التنازلي باستخدام Numericupdown كفاصل زمني

StackOverflow https://stackoverflow.com/questions/1579878

  •  21-09-2019
  •  | 
  •  

سؤال

أنا متأكد من أن هذا قد طلب من قبل ، لكن لا يمكنني العثور على حل يعمل. لدي رقمي على النموذج الخاص بي وعلامة مع توقيت وزر. أريد أن يبدأ الموقت عند الضغط على الزر وسيساوي الفاصل الزمني للموقت من الرقم العدد وسيتم عرض العد التنازلي في الملصق. أعلم أن هذا يجب أن يكون سهلاً. أي مساعدة؟

حتى الآن:

   int tik = Convert.ToInt32(TimerInterval.Value);

    if (tik >= 0)
    {
        TimerCount.Text = (tik--).ToString();
    }

    else
    {
        TimerCount.Text = "Out of Time";
    }

لا يبدو أنه يتم تحديثه كقرادات مؤقت.

هل كانت مفيدة؟

المحلول

إليك مثال سريع على ما تبحث عنه. هذا يجب أن يمنحك فكرة أساسية حول ما عليك القيام به

    //class variable
    private int totNumOfSec;

    //set the event for the tick
    //and the interval each second
    timer1.Tick += new EventHandler(timer1_Tick);
    timer1.Interval = 1000;


    private void button1_Click(object sender, EventArgs e)
    {
        totNumOfSec = (int)this.numericUpDown1.Value; 
        timer1.Start();
    }

    void timer1_Tick(object sender, EventArgs e)
    {
        //check the timer tick
        totNumOfSec--;
        if (totNumOfSec == 0)
        {
            //do capture
            MessageBox.Show("Captured");
            timer1.Stop();
        }
        else
            label1.Text = "Caputring in " + totNumOfSec.ToString();
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top