c# - very simple program to draw a filled rectangle .. why it does not work ? -


i new using graphics , trying draw filled rectangle when form opens.. nothing working , not know reason

here code :

private void result_load(object sender, eventargs e) {     system.drawing.solidbrush mybrush = new       system.drawing.solidbrush(system.drawing.color.green);     system.drawing.graphics formgraphics = this.creategraphics();     formgraphics.fillrectangle(mybrush, new rectangle(0, 0, 200,300));     mybrush.dispose();     formgraphics.dispose(); } 

where result form supposed draw rectangle on when loaded

but when load form nothing happens @

where problem ?

thanks in advance

add handler paint event in form's constructor:

/// <summary> /// form constructor /// </summary> public frmmain() {     initializecomponent();      this.paint += frmmain_paint; } 

and create method frmmain_paint:

void frmmain_paint(object sender, painteventargs e) {     using (system.drawing.solidbrush mybrush = new system.drawing.solidbrush(system.drawing.color.green))     {         e.graphics.fillrectangle(mybrush, new rectangle(0, 0, 200, 300));     } } 

tips

  1. use e.graphics draw (not this.creategraphics())
  2. use using keyword in example above.

Comments