how to make dynamically added buttons clickable in C#


i creating c# application fetches data database, , dynamically creates 5 textboxes , 1 button in single row.

number of rows present in database equals number of rows of textboxes , buttons created.

create rows of textboxes , buttons, textboxes capable of displaying data being fetched database.

trouble button generated, nothing when clicked, not unexpected since haven't created handler handle click event. confused on how dynamically create click handler buttons again generated dynamically.

below code sample generated textboxes , buttons.

   
for (int = 3; <= count; i++)                      {                                                    com.parameters[0].value = i;                          using (sqlcedatareader rd = com.executereader())                              if (rd.read())                              {                                  pname = (rd["pname"].tostring());                                  cname = (rd["cname"].tostring());                                  budget = (rd["budget"].tostring());                                  advance = (rd["advance"].tostring());                                  ddate = (rd["ddate"].tostring());                                    textbox tobj = new textbox();                                  tobj.location = new point(10, (40 + ((i - 2) * 20)));                                  tobj.tag = 1;                                  tobj.text = pname;                                  tobj.autosize = false;                                  tobj.width = 150;                                  tobj.readonly = true;                                  this.controls.add(tobj);                                    textbox tobj1 = new textbox();                                  tobj1.location = new point(160, (40 + ((i - 2) * 20)));                                  tobj1.tag = 2;                                  tobj1.text = cname;                                  tobj1.autosize = false;                                  tobj1.width = 150;                                  tobj1.readonly = true;                                  this.controls.add(tobj1);                                    textbox tobj2 = new textbox();                                  tobj2.location = new point(310, (40 + ((i - 2) * 20)));                                  tobj2.tag = 3;                                  tobj2.text = budget;                                  tobj2.autosize = false;                                  tobj2.width = 100;                                  tobj2.readonly = true;                                  this.controls.add(tobj2);                                    textbox tobj3 = new textbox();                                  tobj3.location = new point(410, (40 + ((i - 2) * 20)));                                  tobj3.tag = 4;                                  tobj3.text = advance;                                  tobj3.autosize = false;                                  tobj3.width = 100;                                  tobj3.readonly = true;                                  this.controls.add(tobj3);                                    textbox tobj4 = new textbox();                                  tobj4.location = new point(510, (40 + ((i - 2) * 20)));                                  tobj4.tag = 5;                                  tobj4.text = ddate;                                  tobj4.autosize = false;                                  tobj4.width = 100;                                  tobj4.readonly = true;                                    int due = 0;                                  due = int.parse(ddate);                                  if (due < 5)                                  {                                      tobj4.backcolor = system.drawing.color.red;                                  }                                  this.controls.add(tobj4);                                    button button = new button();                                  button.left = 620;                                  button.tag = i;                                  button.height = 20;                                  button.text = "details";                                  button.top = (40 + ((i - 2) * 20));                                  this.controls.add(button);                                  }                      }



please give me ideas on how generate click event handler.

i solved myself using following code

for (int = 3; <= count; i++) {     com.parameters[0].value = i;     using (sqlcedatareader rd = com.executereader())     if (rd.read())     {        ....        ...        button.top = (40 + ((i - 2) * 20));        button.click += handletheclick;         this.controls.add(button);       } }  private void handletheclick(object sender, eventargs e)         {             button btn = sender button;             int row = (int)btn.tag;                      }



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'