Dynamic Control Event Handler
Hey guys. I'm relatively new to C#. I'm only using it because my company wants a User Interface with a program they are requesting.
My program is pretty basic. It generates a variable amount of Label controls depending on how many database entries there are. However, since I am dynamically generating these using the following piece of code:
I removed the code which determines x and y, its not important. But back to my problem, I don't know how to capture when each object is Clicked. I read online I could do something like this:
public Label lblMaster = new Label;
for (i = 0; i < total; i++)
lblMaster[i].Size = new System.Drawing.Size(100, 30);
lblMaster[i].Location = new System.Drawing.Point(x, y);
lblMaster[i].Text = _names[i];
lblMaster[i].TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
I'm not sure how to go about this. Essentially every object that will be clicked will do the same thing, but just return different information depending on its index.
lblALabel.Click += new EventHandler(something);
Is there a way I can write a catch all event handler that knows the index of the Label that was clicked?