Creating controls and events dynamically
-
Hello, I'm trying to execute the following mechanism .. >>>>> get records from the DB , ( e.g. record for each product ) >>>>> create a "link label" for each record ( product ) >>>>> when any of the link labels is clicked .. it assign the ProductID value from the record to a Global variable named ProductID >>>>> add all the link lables to a panel that's what've done, My problem is that i always get the value of the last record
DataTable dtUserOffers = ProductsQuery; int NewY = 0; for ( i = 0; i < dtUserOffers.Rows.Count; i++) { LinkLabel lnkUserOffers = new LinkLabel(); lnkUserOffers.AutoSize = true; lnkUserOffers.Location = new System.Drawing.Point(18, NewY); lnkUserOffers.Name = "lnkUserOffers" + i.ToString(); lnkUserOffers.Size = new System.Drawing.Size(59, 14); //lnkUserOffers.TabIndex = 51; lnkUserOffers.TabStop = true; lnkUserOffers.Text = "View"; Global.Product_id = int.Parse(dtUserOffers.Rows[i]["ProductID"].ToString()); EventHandler handler = new EventHandler(LinkLabel_Click); lnkUserOffers.Click += handler; NewY += 15; panel1.Controls.Add(lnkUserOffers); } private void LinkLabel_Click(object sender, EventArgs e) { MessageBox.Show(Global.ProductID); }
How can i Handle this an assign a specific action to each created link label -
Hello, I'm trying to execute the following mechanism .. >>>>> get records from the DB , ( e.g. record for each product ) >>>>> create a "link label" for each record ( product ) >>>>> when any of the link labels is clicked .. it assign the ProductID value from the record to a Global variable named ProductID >>>>> add all the link lables to a panel that's what've done, My problem is that i always get the value of the last record
DataTable dtUserOffers = ProductsQuery; int NewY = 0; for ( i = 0; i < dtUserOffers.Rows.Count; i++) { LinkLabel lnkUserOffers = new LinkLabel(); lnkUserOffers.AutoSize = true; lnkUserOffers.Location = new System.Drawing.Point(18, NewY); lnkUserOffers.Name = "lnkUserOffers" + i.ToString(); lnkUserOffers.Size = new System.Drawing.Size(59, 14); //lnkUserOffers.TabIndex = 51; lnkUserOffers.TabStop = true; lnkUserOffers.Text = "View"; Global.Product_id = int.Parse(dtUserOffers.Rows[i]["ProductID"].ToString()); EventHandler handler = new EventHandler(LinkLabel_Click); lnkUserOffers.Click += handler; NewY += 15; panel1.Controls.Add(lnkUserOffers); } private void LinkLabel_Click(object sender, EventArgs e) { MessageBox.Show(Global.ProductID); }
How can i Handle this an assign a specific action to each created link labelyou may use anonumous methods and closure . // you can use method with no name this is delegate (args ){} aka anonymous methodes EventHandler handler = new EventHandler (new delegate (object sender,EventArgs e){ MessageBox.Show(Global.ProductID); // you can use variables global that is closure })) or you can use command objects this is objects that actually encapsulate a method ec ClickCommand command = new ClickCommand(product_id) EventHandler handler = new EventHandler (command.ExecuteOnClickAction); where class Clickcommand { int productid; public void ExecuteOnClickAction() { MessageBox.Show(productID); } } or many more improvise
f(yf) = yf