How to write dynamic pictureBox Click event in C#? -


private void button1_click(object sender, eventargs e) {         picturebox dynamicpicture1 = new picturebox();         dynamicpicture1.tag = i;         dynamicpicture1.location = new point(x, y);         dynamicpicture1.name = "picturebox" + i;         dynamicpicture1.size = new system.drawing.size(30, 27);         dynamicpicture1.imagelocation =              "c:\\users\\newfolder\\downloads\\x`enter code here`ryrvc.jpg";         panel1.controls.add(dynamicpicture1); 

}

try updated code.

private void button1_click(object sender, eventargs e)     {         int s = 4;         int x = 0;         int y = 0;         (int = 0; < s; i++)         {             if (i == 0)             {                 x = 38;                 y = 60;             }             else             {                 y += 50;             }              picturebox dynamicpicture1 = new picturebox();             dynamicpicture1.tag = i;             dynamicpicture1.location = new point(x, y);             dynamicpicture1.name = "picturebox" + i;             dynamicpicture1.size = new system.drawing.size(30, 27);             dynamicpicture1.imagelocation = @"c:\users\nxa00960\downloads\abc.jpg";             panel1.controls.add(dynamicpicture1);              dynamicpicture1.click += dynamicpicture1_click;         }     }      void dynamicpicture1_click(object sender, eventargs e)     {         var picturebox = sender picturebox;          switch (picturebox.name)         {             case "picturebox0":                 //do                 break;              case "picturebox1":                 //do                 break;              case "picturebox2":                 //do                 break;              case "picturebox3":                 //do                 break;              default:                 break;         }     } 

Comments