WPF C# - Timer countdown

前端 未结 4 1004

How can I implement the following in my piece of code written in WPF C#?

I have a ElementFlow control in which I have implemented a SelectionChanged event which (by defi

相关标签:
4条回答
  • 2021-01-23 10:44

    I've figured the complete code out as such:

    DispatcherTimer _timer;
    
    public MainWindow()
    {
        _myTimer = new DispatcherTimer();
        _myTimer.Tick += MyTimerTick;
        _myTimer.Interval = new TimeSpan(0,0,0,1);
    }
    
    private void ElementFlowSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        _counter = 0;
        _myTimer.Stop();
        _myTimer.Interval = new TimeSpan(0, 0, 0, 1);
        _myTimer.Start();
    }
    
    private int _counter;
    public int Counter
    {
        get { return _counter; }
        set
            {
                _counter = value;
                OnPropertyChanged("Counter");
            }
    }
    private void MyTimerTick(object sender, EventArgs e)
    {
        Counter++;
        if (Counter == 2)
        {
            _myTimer.Stop();
            MessageBox.Show(“Reached the 2 second countdown”);
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler e = PropertyChanged;
        if (e != null)
            {
                e(this, new PropertyChangedEventArgs(propertyName));
    }
    }
    
    0 讨论(0)
  • 2021-01-23 10:44

    look here is the code of how to use DispatherTimer and you can add your own logic in it. that will depends on you..

     private void ListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(2000);
            timer.Tick += timer_Tick;
            timer.Start();
    
        }
    
        void  timer_Tick(object sender, object e)
        {
           // show your message here..
        }
    
    0 讨论(0)
  • 2021-01-23 10:50

    To use a DispatcherTimer:

        private DispatcherTimer _timer;
        public void StartTimer()
        {
            if (_timer == null)
            {
                _timer = new DispatcherTimer();
                _timer.Tick += _timer_Tick;
            }
    
            _timer.Interval = TimeSpan.FromSeconds(2);
            _timer.Start();
        }
    
        void _timer_Tick(object sender, EventArgs e)
        {
            MessageBox.Show("Hi there");
            _timer.Stop();
        }
    
        void SelectionChangedEvent()
        {
            StartTimer();
        }
    
    0 讨论(0)
  • 2021-01-23 10:53

    Try this:

    private int timerTickCount = 0;
    private bool hasSelectionChanged = false;
    private DispatcherTimer timer;
    

    In your constructor or relevant method:

    timer = new DispatcherTimer();
    timer.Interval = new TimeSpan(0, 0, 1); // will 'tick' once every second
    timer.Tick += new EventHandler(Timer_Tick);
    timer.Start();
    

    And then an event handler:

    private void Timer_Tick(object sender, EventArgs e)
    {
        DispatcherTimer timer = (DispatcherTimer)sender;
        if (++timerTickCount == 2)
        {
            if (hasSelectionChanged) timer.Stop();
            else MessageBox.Show("Hi there");
        }
    }
    

    Finally, to make this work, you just need to set the hasSelectionChanged variable when the selection has changed according to your SelectionChanged event.

    0 讨论(0)
提交回复
热议问题