Can someone show me how you would have one label on your window at design time and then once your window is run you would have 10, one stacked up on the other. I am used to control arrays in VB6. Thanks.
Last edited by jordan23; Dec 30th, 2003 at 07:33 AM.
Ok, I understand that part of the code. Now, what if I want to know which one the user clicks on. How do I detect that? Sorry for all the simple questions. New to the C# syntax.
Originally posted by jordan23 Ok, I understand that part of the code. Now, what if I want to know which one the user clicks on. How do I detect that? Sorry for all the simple questions. New to the C# syntax.
Umm , by creating and event and attaching it one event handler . Let me see if I can figure it out .
Well, I understand that part. My problem is where to you generate the event code for the labels? How do you detect which on was clicked? Thanks for all your help.
Originally posted by jordan23 Well, I understand that part. My problem is where to you generate the event code for the labels? How do you detect which on was clicked? Thanks for all your help.
You don't create any event but you have to create event handler . I believe you need only one handler , I'll show you how in minutes .
private void CreateLabels()
{
for (int i=0;i<10;i++)
{
Label lbl=new Label();
lbl.Location=new Point(80,20*i);
lbl.Size=new Size(120,20);
lbl.Text="LabelRuntime"+i.ToString();
lbl.Name="Label"+i.ToString();
lbl.BackColor=Color.FromArgb(230,130,30);
lbl.BorderStyle=BorderStyle.Fixed3D;
lbl.TextAlign=ContentAlignment.MiddleCenter;
lbl.ForeColor=Color.White;
this.Controls.Add(lbl);
//Here subscribed with click events with
//each label is created .
lbl.Click+=new EventHandler(lbl_Click);
}
}
//This is the handler that dectects which label
//was clicked
private void lbl_Click(object sender, EventArgs e)
{
Label l=(Label)sender;
MessageBox.Show(l.Name);
//Here you can various implementations
//switch cases or handle them by name .
}
private void button1_Click(object sender, System.EventArgs e)
{
CreateLabels();
}