ZNackasha
ZNackasha

Reputation: 1089

Updating graphics on panels

OK so I am new to C# and I am confused about how events work. currently I have a panel in which I am drawing rectangles. the event is called when the panel is initialized but I want to call it after I do something in my data Grid. I simply want to know how to tell the object to update.

Do I not use an event in this situation? If so do I just create a new graphics instance and start?

Here is my code:

private void panel6_Paint(object sender, PaintEventArgs e)
{
   if(dataGridView1.RowCount != 0 )
   {
      Pen lightRed = new Pen(Brushes.LightSalmon);
      lightRed.Width = 1.0F;
      lightRed.LineJoin = System.Drawing.Drawing2D.LineJoin.Miter;
      int counter = 0;
      foreach (var pair in currentPosition)
      {  
         if(dataGridView1[0, counter].Style.BackColor == Color.Red)
         {
            e.Graphics.DrawRectangle(lightRed, new Rectangle(0, currentPosition.Count / panel6.Height * counter, 66, currentPosition.Count / panel6.Height * counter));
         }
      }
      lightRed.Dispose();
   }
}

Upvotes: 0

Views: 161

Answers (1)

DonBoitnott
DonBoitnott

Reputation: 11025

Whenever you want to force a redraw, call:

Invalidate();

...or

Refresh();

Invalidate() is preferred...Refresh() attempts to be more immediate.

So, for your panel:

panel6.Invalidate();

You would call this at any point where you want to signal to the control that it should repaint itself. That will result in your Paint event firing.

Upvotes: 1

Related Questions