Bernice
Bernice

Reputation: 2592

Override paint method C#

I have two forms Form 1 and Form 2.

Form 2 is inheriting from Form 1. In Form 1 I have a method on_Paint for a particular panel, in which I am drawing an image. Since Form 2 is inheriting from Form 1, the panel is being shown as painted before I even run the program (on the design of Form 2).

The problem is, that now I need to paint some things over this image after the image is painted in Form 2. How can I make this possible?

This is some code to make the problem more clear.

// This is Form 1 on_Paint method

private void grid_Paint(object sender, PaintEventArgs e)
{
    Image img = Image.FromFile(resourcesPath+ "grid.fw.png");

    gridGraphics = grid.CreateGraphics(); 
    gridGraphics.DrawImage(img, 0, 0, 650, 550);
}

// This is the method which I want to be invoked after the grid is painted.. This is located in Form 1:

public void paintSprites(int row, int column, int value)
{
     int yLoc = 0;
     int xLoc = 0;
     Graphics g = grid.CreateGraphics();

     switch (row)
     {
         case 0: yLoc = 435; break;
         case 1: yLoc = 355; break;
         case 2: yLoc = 275; break;
         case 3: yLoc = 195; break;
         case 4: yLoc = 115; break;
         case 5: yLoc = 35; break;
     }

     switch (column)
     {
         case 0: xLoc = 35; break;
         case 1: xLoc = 120; break;
         case 2: xLoc = 205; break;
         case 3: xLoc = 290; break;
         case 4: xLoc = 375; break;
         case 5: xLoc = 460; break;
         case 6: xLoc = 545; break;
     }

     if (value == 1)
     {
         g.DrawImage(red, xLoc, yLoc, 65, 65);
     }
     else g.DrawImage(gold, xLoc, yLoc, 65, 65);
 }

This method is located in Form 2 and calls the method above:

public void checkWhichToPaint()
{
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLUMNS; j++)
        {
            if (gridMap[i, j] != 0)
            {
                paintSprites(i, j, gridMap[i, j]);
            }
        }
    }
}

and I am calling the method checkWhichToPaint() after creating a new instance of Form 2 and Showing it:

Tournament tournament = new Tournament();
tournament.LoadGame();
tournament.Show();  
tournament.checkWhichToPaint();

where can I call this method such as it is painted after the grid_Paint is invoked?

Thanks for your help :)

Upvotes: 2

Views: 1591

Answers (2)

Bernice
Bernice

Reputation: 2592

This is the answer to whoever might need it!

In Form 1:

    protected virtual void grid_Paint(object sender, PaintEventArgs e)
    {
        Image img = Image.FromFile(resourcesPath+ "grid.fw.png");

        gridGraphics = grid.CreateGraphics();
        gridGraphics.DrawImage(img, 0, 0, 650, 550);
    }

In Form 2:

   protected override void grid_Paint(object sender, PaintEventArgs e)
   {
       checkWhichToPaint();
       base.grid_Paint(null, null);
   }

Enjoy ! :)

Upvotes: 3

Mechanik
Mechanik

Reputation: 679

Override the paint method in the second form and call base paint method before you run your form 2 code.

Upvotes: 1

Related Questions