c# - Custom Control Doesn't Redraw In Designer Or When Running When A Property Is Changed -


i'm creating custom control based on panel control , using draw grid of rectangles inside of it. have made columns , rows properties allow them changed once control added form. if values changed in designer or in form code rectangles never drawn , form appears blank. i've tried different suggestions such this.invalidate() , this.refresh() in set portion of property still can't seem paint new rectangle grid. i'm sure i'm missing somewhere. here's code i'm using:

public class pixel {     public rectangle bounds { get; set; }     public bool ison { get; set; }     public bool isselected { get; set; } } 

public class pixelgridcontrol : panel {     private int columns = 99;     private int rows = 63;     private pixel[,] pixels;     private bool leftmouseisdown = false;     private bool rightmouseisdown = false;      public int columns     {         { return columns; }         set         {             if (value > 0)             {                 columns = value;                 createpixelgrid();                 this.refresh();             }             else             {                 throw new argumentoutofrangeexception("columns", "must > 0");             }         }     }     public int rows     {         { return rows; }         set         {             if (value > 0)             {                 rows = value;                 createpixelgrid();                 this.refresh();             }             else             {                 throw new argumentoutofrangeexception("rows", "must > 0");             }         }     }      public pixelgridcontrol()     {         this.doublebuffered = true;         this.resizeredraw = true;          createpixelgrid();     }      // adjust each column , row fit entire client area:     protected override void onresize(eventargs e)     {         int top = 0;         (int y = 0; y < rows; ++y)         {             int left = 0;             int height = (this.clientsize.height - top) / (rows - y);             (int x = 0; x < columns; ++x)             {                 int width = (this.clientsize.width - left) / (columns - x);                 pixels[x, y].bounds = new rectangle(left, top, width, height);                 left += width;             }             top += height;         }         base.onresize(e);     }      protected override void onpaint(painteventargs e)     {         e.graphics.smoothingmode = smoothingmode.antialias;         (int y = 0; y < rows; ++y)         {             (int x = 0; x < columns; ++x)             {                 if (pixels[x, y].ison)                 {                     e.graphics.fillellipse(brushes.gold, pixels[x, y].bounds);                     e.graphics.drawellipse(pens.goldenrod, pixels[x, y].bounds);                 }                 else                 {                     e.graphics.fillrectangle(brushes.black, pixels[x, y].bounds);                     e.graphics.drawrectangle(pens.white, pixels[x, y].bounds);                 }             }         }         base.onpaint(e);     }     private void createpixelgrid()     {         // initialize pixel grid:         pixels = new pixel[columns, rows];         (int y = 0; y < rows; ++y)         {             (int x = 0; x < columns; ++x)             {                 pixels[x, y] = new pixel();             }         }     } } 

the problem don't populate grid cell bounds when rows or columns change, remain rectangle.empty.

to fix that, move code onresize separate method:

private void updatepixelgrid() {     // adjust each column , row fit entire client area:     int top = 0;     (int y = 0; y < rows; ++y)     {         int left = 0;         int height = (this.clientsize.height - top) / (rows - y);         (int x = 0; x < columns; ++x)         {             int width = (this.clientsize.width - left) / (columns - x);             pixels[x, y].bounds = new rectangle(left, top, width, height);             left += width;         }         top += height;     } } 

and call both onresize , createpixelgrid:

protected override void onresize(eventargs e) {     updatepixelgrid();     base.onresize(e); }  private void createpixelgrid() {     // initialize pixel grid:     pixels = new pixel[columns, rows];     (int y = 0; y < rows; ++y)     {         (int x = 0; x < columns; ++x)         {             pixels[x, y] = new pixel();         }     }     updatepixelgrid(); } 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -