2

I've been working on a project for class where I need to display on screen polygons (drawed in a Panel), but I've been reading arround here that I should work with Paint event, tho I can't make it work (started learning C# a little ago).

private void drawView()
{
//"playerView" is the panel I'm working on
Graphics gr = playerView.CreateGraphics();
Pen pen = new Pen(Color.White, 1);


 //Left Wall 1
 Point lw1a = new Point(18, 7); 
 Point lw1b = new Point(99, 61); 
 Point lw1c = new Point(99, 259); 
 Point lw1d = new Point(18, 313);

 Point[] lw1 = { lw1a, lw1b, lw1c, lw1d };

 gr.DrawPolygon(pen, lw1);
}

I was doing something like this to draw it on screen, it would be possible to do this with a Paint method? (it is called method, or event? I'm really lost here).

Thanks!

  • Hello @Tchangla and welcome to StackOverflow! In C#, an event can be subscribed to by something known as an `event handler`. an event handler looks just like a method, and it basically is. the difference is that this method will be called every time the event it subscribed to fires. this does require it to have a specific signature (in simple terms : the arguments need to match for the kind of event.) – Timothy Groote Nov 06 '15 at 08:55

1 Answers1

1

I think you are referring to the Control.Paint event from windows forms.

Basically, you would attach a listener to the Paint event of a windows forms element, like this :

//this should happen only once! put it in another handler, attached to the load event of your form, or find a different solution
//as long as you make sure that playerView is instantiated before trying to attach the handler, 
//and that you only attach it once.
playerView.Paint += new System.Windows.Forms.PaintEventHandler(this.playerView_Paint);

private void playerView_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    // Create a local version of the graphics object for the playerView.
    Graphics g = e.Graphics;
    //you can now draw using g

    //Left Wall 1
    Point lw1a = new Point(18, 7); 
    Point lw1b = new Point(99, 61); 
    Point lw1c = new Point(99, 259); 
    Point lw1d = new Point(18, 313);

    Point[] lw1 = { lw1a, lw1b, lw1c, lw1d };

    //we need to dispose this pen when we're done with it.
    //a handy way to do that is with a "using" clause
    using(Pen pen = new Pen(Color.White, 1))
    {
        g.DrawPolygon(pen, lw1);
    }
}
Timothy Groote
  • 8,614
  • 26
  • 52
  • How I should call the event? I mean, I'm trying with "playerView_Paint();" and this doesn't work (I don't know what to write inside "()" ). –  Nov 06 '15 at 08:53
  • You don't have to :) The `windows.forms` controls fire the paint event on their own when it is needed. all you have to do, is subscribe to the event with an eventhandler. (that is what my example code does) – Timothy Groote Nov 06 '15 at 08:54
  • 2
    You trigger the event by calling `panel.Invalidate();` @timothy: No, he __has__ to call/trigger it whenever his __drawing data change__. Windows will __not__ pick this up. Of course, as long as he doesn't have variable drawing data this is moot but this will change, I bet.. Also: Do __Dispose__ of the `Pen` or use `Pens.White` ! – TaW Nov 06 '15 at 08:56
  • @TaW yes, that's a way to force a windows.forms element to trigger the event. – Timothy Groote Nov 06 '15 at 08:57
  • So as not to flood the place with all kinds of half-duplicate answers, if you're looking for how that should be done, head over to http://stackoverflow.com/questions/18602531/moving-object-with-paint-event – Timothy Groote Nov 06 '15 at 09:02
  • @TimothyGroote , @TaW , thanks, many thanks from the bottom of my heart! I was about to ask about how to trigger the event again, and now it works! I was using `panel.Invalidate()` in the past and didn't work, but now I know why didn't work in the past. Thanks again! My teacher didn't know how to help me with that. –  Nov 06 '15 at 09:03
  • 1
    @TaW good point about disposing the pen. i updated the example – Timothy Groote Nov 06 '15 at 09:13