Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An error No overload for 'timer_Tick' matches delegate 'System.EventHandler<object>'

Tags:

c#

I am not sure what is wrong with my code can someone help fix the error? The error is in the timer.Tick() line. It's supposed to make a stopwatch.

namespace App3
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {                
            this.InitializeComponent();
        }
        private int myCount;

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Tick += new EventHandler<object>(timer_Tick);
            timer.Interval = TimeSpan.FromSeconds(5);
            timer.Start();    
        }


        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            myCount++;
            Label.Text = myCount.ToString();
        }
    }
like image 320
bluemagic Avatar asked Jul 22 '26 02:07

bluemagic


1 Answers

DispatcherTimer.Tick is an EventHandler, not an EventHandler<object>.

You need to change your code to specify this correctly:

 timer.Tick += new EventHandler(timer_Tick);

Note that this can also be written in short form, which is typically safer:

timer.Tick += timer_Tick;
like image 149
Reed Copsey Avatar answered Jul 23 '26 15:07

Reed Copsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!