0

I am trying to create a method for a timer event that takes 3 arguments. I have had a look at similar questions and tried to implement the solutions shown but the solution do not work in my case

        GraphDrawingTimer.Elapsed += new ElapsedEventHandler(GraphPainter);
        GraphDrawingTimer.Interval = 350;
        GraphDrawingTimer.Enabled = true; 

Above is the timer initialization

        static void GraphPainter(object sender, ElapsedEventArgs e)
        {   
          //Show_Graph(c);
        }

thats method that will be called once the timer fires.

I want to add a PaintEventArgs c extra argument to draw a graph. I was using

private void tabPage2_Paint(object sender, PaintEventArgs e)

method but buttons do not get refreshed on my tabpage, thats why I want to create my own graph drawing method that will refresh every 350 milli-second. I have tried

        GraphDrawingTimer.Elapsed += (object sender, ElapsedEventArgs e) => { GraphPainter(sender, e, c); };
        GraphDrawingTimer.Interval = 350;
        GraphDrawingTimer.Enabled = true;

.....

        static void GraphPainter(object sender, ElapsedEventArgs e, PaintEventArgs c)
        {   
          Show_Graph(c);
        }

but that does not work. I get the error "The name 'c' does not exist in the current context". I also tried

        PaintEventArgs c; 

        GraphDrawingTimer.Elapsed += (object sender, ElapsedEventArgs e)=> { GraphPainter(sender, e,  c); };
        GraphDrawingTimer.Interval = 350;
        GraphDrawingTimer.Enabled = true;

.....

        static void GraphPainter(object sender, ElapsedEventArgs e, PaintEventArgs c)
        {   
          Show_Graph(c);
        }

I get the following error

Error CS0165 Use of unassigned local variable 'c'

Which indeed makes sense since I'm not assigning any value to it - so how to assign a value to that PaintEventArgs c to pass correct parameter of the Paint event? Or maybe there should be some other approach to invoke Paint event?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
kaygee
  • 11
  • 2
  • `ElapsedEventHandler` on has two args, see: https://learn.microsoft.com/en-us/dotnet/api/system.timers.elapsedeventhandler?view=net-5.0 – Sorceri May 03 '21 at 17:55
  • 1
    You're going about it the wrong way. You can't correctly create your own `PaintEventArgs` instance. You have to call `Invalidate()` and let the framework do the rest of the work. See duplicate. – Peter Duniho May 03 '21 at 18:01
  • Perhaps I asked the question wrongly and for some reason I was not looking for the correct solution. I just reviewed my code. When I use the private void tabPage2_Paint(object sender, PaintEventArgs e) method. and I insert my graph drawing code in it. The buttons next to the graph are not painted/refreshed correctly. I have to slightly drag or move the application for the buttons to show properly. I do use tabPage2.Invalidate(); in other parts of the code. but it does not repaint the buttons. and Form1.Invalidate causes errors. How does one invalidate the whole form for it to be repainted) – kaygee May 03 '21 at 19:09

1 Answers1

-1

PaintEventArgs might not be valid that way. The best option is to use the .Invalidate() method on the controls you want to update from your timer and that will cause their paint events to be called.

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.invalidate?view=net-5.0

This might look something like:

   GraphDrawingTimer.Elapsed +=
         (object sender, ElapsedEventArgs e) => MyGraphControl.Invalidate();
g01d
  • 651
  • 5
  • 24