c# - Controls not shown in dynamically generated panel -


i want generate same number of panels dynamically number of distinct values read sqlcommand , display controls dynamically in panel according values read sqlcommand, refer code.

but controls i.e. buttons displaying in 1 panel instead of respective panels.

screenshot of generated result:

why code not displaying controls in panels though controls plotted in right location?

private void show_button() {     //initializecomponent();     con1.con.open();     sqlcommand cmd = new sqlcommand("select distinct floors tbl_tablemaster", con1.con);     sqldatareader rd = cmd.executereader();     int panellocationx = 10;     int panellocationy = 30;     int panelwidth = 340;              int buttonwidth = 100;     int buttonheight = 40;     int panelheight = 300;     while (rd.read())     {         panel panel = new panel();         panel.location = new point(panellocationx, panellocationy);         panel.size = new size(panelwidth, panelheight);                  panel.name = rd[0].tostring();         panel.backcolor = color.tan;           label label = new label();         label.location = new point(panellocationx, panellocationy - 20);         label.forecolor = color.red;         label.text = rd[0].tostring();         this.controls.add(label);         int buttonlocationx = panellocationx;         int buttonlocationy = panellocationy ;         con2.con.open();         sqlcommand cmd1 = new sqlcommand("select tableno,isactive,date tbl_tablemaster floors=@floor", con2.con);         cmd1.parameters.addwithvalue("@floor", rd[0].tostring());         sqldatareader rd1 = cmd1.executereader();         int j = 0;         while (rd1.read())         {              if (buttonlocationx < (panellocationx + panelwidth-10))             {                   //messagebox.show("button" + buttonlocationx + "," + buttonlocationy + "");                 if (rd1[1].tostring() == "available")                 {                     button button = new button();                     button.click += new eventhandler(button_click);                     button.location = new point(buttonlocationx, buttonlocationy);                     button.text = rd1[0].tostring();                     button.backcolor = color.turquoise;                     button.tag = j;                     button.size = new system.drawing.size(buttonwidth,buttonheight);                     button.font = new font("microsoft sans serif", 12, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(0)));                     panel.controls.add(button);                     //button.visible = true;                     button.bringtofront();                     button.flatstyle = flatstyle.popup;                 }                 if (rd1[1].tostring() == "order running")                 {                     listbox1.visible = false;                     button button = new button();                     button.click += new eventhandler(button_click);                     button.location = new point(buttonlocationx, buttonlocationy);                      system.timespan diff2 = convert.todatetime(system.datetime.now.tostring("yyyy'-'mm'-'dd't'hh':'mm':'ss")) - convert.todatetime(rd1[2].tostring());                      button.text = convert.tostring(rd1[0].tostring() + "\n\n . " + convert.tostring(diff2));                     button.text = rd1[1].tostring();                     button.backcolor = color.crimson;                     button.tag = j;                     button.size = new system.drawing.size(buttonwidth,buttonheight);                     button.forecolor = color.black;                     button.font = new font("microsoft sans serif", 7, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(0)));                       bitmap bmp = new bitmap(button.clientrectangle.width, button.clientrectangle.height);                     using (graphics g = graphics.fromimage(bmp))                     {                         g.clear(button.backcolor);                          string line1 = rd1[0].tostring();                         string line2 = "\n\n ." + diff2.tostring();                          stringformat sf = new stringformat();                         sf.alignment = stringalignment.center;                         sf.linealignment = stringalignment.near;                         using (font arial = new font("microsoft sans serif", 15))                         {                             rectangle rc = button.clientrectangle;                             rc.inflate(-5, -5);                             g.drawstring(line1, arial, brushes.white, rc, sf);                         }                          using (font courier = new font("microsoft sans serif", 10))                         {                             sf.linealignment = stringalignment.center;                             g.drawstring(line2, courier, brushes.black, button.clientrectangle, sf);                         }                     }                     button.image = bmp;                     button.imagealign = contentalignment.middlecenter;                     button.forecolor = color.crimson;                      panel.controls.add(button);                     //button.visible = true;                     //button.bringtofront();                     button.flatstyle = flatstyle.popup;                  }                 if (rd1[1].tostring() == "hold")                 {                     button button = new button();                     button.click += new eventhandler(button_click);                     button.location = new point(buttonlocationx, buttonlocationy);                     button.text = rd1[0].tostring();                     button.backcolor = color.yellow;                     button.tag = j;                     button.size = new system.drawing.size(buttonlocationx, buttonlocationy);                     button.font = new font("microsoft sans serif", 13, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(0)));                     panel.controls.add(button);                     //button.visible = true;                     button.bringtofront();                     button.flatstyle = flatstyle.popup;                 }                 if (rd1[1].tostring() == "amount")                 {                     button button = new button();                     button.click += new eventhandler(button_click);                     button.location = new point(buttonlocationx, buttonlocationy);                     button.text = rd1[0].tostring();                     button.backcolor = color.darkgray;                     button.tag = j;                     button.size = new system.drawing.size(buttonlocationx, buttonlocationy);                     button.font = new font("microsoft sans serif", 13, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(0)));                     panel.controls.add(button);                     //button.visible = true;                     //button.bringtofront();                     button.flatstyle = flatstyle.popup;                 }                 buttonlocationx = buttonlocationx + buttonwidth + 10;                 count = count + 1;                 j++;              }             else             {                 buttonlocationy = buttonlocationy + buttonheight+10;                 buttonlocationx = panellocationx;                 if (panelheight < buttonlocationy)                 {                     panel.autoscroll = false;                     panel.horizontalscroll.enabled = false;                     panel.horizontalscroll.visible = false;                     panel.horizontalscroll.maximum = 0;                     panel.autoscroll = true;                 }                 else                 {                  }                       if (rd1[1].tostring() == "available")                     {                         button button = new button();                         button.click += new eventhandler(button_click);                         button.location = new point(buttonlocationx, buttonlocationy);                         button.text = rd1[0].tostring();                         button.backcolor = color.turquoise;                         button.tag = j;                         button.size = new system.drawing.size(buttonwidth, buttonheight);                         button.font = new font("microsoft sans serif", 12, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(0)));                         panel.controls.add(button);                         //button.visible = true;                         //button.bringtofront();                         button.flatstyle = flatstyle.popup;                     }                     if (rd1[1].tostring() == "order running")                     {                         listbox1.visible = false;                         button button = new button();                         button.click += new eventhandler(button_click);                         button.location = new point(buttonlocationx, buttonlocationy);                          system.timespan diff2 = convert.todatetime(system.datetime.now.tostring("yyyy'-'mm'-'dd't'hh':'mm':'ss")) - convert.todatetime(rd1[2].tostring());                          button.text = convert.tostring(rd1[0].tostring() + "\n\n . " + convert.tostring(diff2));                         button.text = rd1[1].tostring();                         button.backcolor = color.crimson;                         button.tag = j;                         button.size = new system.drawing.size(buttonwidth, buttonheight);                         button.forecolor = color.black;                         button.font = new font("microsoft sans serif", 7, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(0)));                           bitmap bmp = new bitmap(button.clientrectangle.width, button.clientrectangle.height);                         using (graphics g = graphics.fromimage(bmp))                         {                             g.clear(button.backcolor);                              string line1 = rd1[0].tostring();                             string line2 = "\n\n ." + diff2.tostring();                              stringformat sf = new stringformat();                             sf.alignment = stringalignment.center;                             sf.linealignment = stringalignment.near;                             using (font arial = new font("microsoft sans serif", 15))                             {                                 rectangle rc = button.clientrectangle;                                 rc.inflate(-5, -5);                                 g.drawstring(line1, arial, brushes.white, rc, sf);                             }                              using (font courier = new font("microsoft sans serif", 10))                             {                                 sf.linealignment = stringalignment.center;                                 g.drawstring(line2, courier, brushes.black, button.clientrectangle, sf);                             }                         }                         button.image = bmp;                         button.imagealign = contentalignment.middlecenter;                         button.forecolor = color.crimson;                          panel.controls.add(button);                         //button.visible = true;                         //button.bringtofront();                         button.flatstyle = flatstyle.popup;                      }                     if (rd1[1].tostring() == "hold")                     {                         button button = new button();                         button.click += new eventhandler(button_click);                         button.location = new point(buttonlocationx, buttonlocationy);                         button.text = rd1[0].tostring();                         button.backcolor = color.yellow;                         button.tag = j;                         button.size = new system.drawing.size(buttonlocationx, buttonlocationy);                         button.font = new font("microsoft sans serif", 13, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(0)));                         panel.controls.add(button);                         //button.visible = true;                         //button.bringtofront();                         button.flatstyle = flatstyle.popup;                     }                     if (rd1[1].tostring() == "amount")                     {                         button button = new button();                         button.click += new eventhandler(button_click);                         button.location = new point(buttonlocationx, buttonlocationy);                         button.text = rd1[0].tostring();                         button.backcolor = color.darkgray;                         button.tag = j;                         button.size = new system.drawing.size(buttonlocationx, buttonlocationy);                         button.font = new font("microsoft sans serif", 13, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(0)));                         panel.controls.add(button);                         //button.visible = true;                         //button.bringtofront();                         button.flatstyle = flatstyle.popup;                  }                      j++;                 count = count + 1;                 //if (panelheight < 500)                 //{                 //panelheight = panelheight + 60;                 ////}                 ////else                 ////{                 ////    bar.visible = true;                 ////    panel.controls.add(bar);                 ////}                 //panel.size = new size(panelwidth, panelheight);                 //buttonlocationy = buttonlocationy + buttonheight + 10;                 //buttonlocationx = panellocationx + 10;                 //button button = new button();                 //button.location = new point(buttonlocationx, buttonlocationy);                 //this.controls.add(button);                 //button.bringtofront();                 ////messagebox.show("button" + buttonlocationx + "," + buttonlocationy + "");                 //button.size = new size(buttonwidth, buttonheight);                 //button.name = rd1[0].tostring();                 //button.text = rd1[0].tostring();                 //button.backcolor = color.brown;                 buttonlocationx = buttonlocationx + buttonwidth + 10;              }          }         this.controls.add(panel);         con2.con.close();         panellocationx = panellocationx + panelwidth + 50;      }     con1.con.close(); } 


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -