Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Creating controls and events dynamically

Creating controls and events dynamically

Scheduled Pinned Locked Moved C#
databasegraphicshelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    halm1985
    wrote on last edited by
    #1

    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

    P 1 Reply Last reply
    0
    • H halm1985

      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

      P Offline
      P Offline
      papadimitriou
      wrote on last edited by
      #2

      you 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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups