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.
Printable View
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.
Check this demo .
Thanks for your reply, but what do I open that file with? I opened it in VS.net and got garbage.
I've created it with VS.NET 2003 , if you don't have it then use the converter under my signature .Quote:
Originally posted by jordan23
Thanks for your reply, but what do I open that file with? I opened it in VS.net and got garbage.
I downloaded the tool but could not get it to work. Could you just send it to me in a text file. Thanks.
Sure . That tool is very important for VS.NET compatibilty issues . Make sure you how to use it properly ;)
Code: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);
}
}
jordan: maybe you had problems unraring? Do you have WinRAR?
Thanks Pirate, that worked perfectly. CornedBee, No I do not have that.
The file he uploaded is an archive similar to ZIP, but with better compression. WinRAR can unpack the file.
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 .Quote:
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.
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 .:DQuote:
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.
Casting is the keyword here . ;)
Code: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();
}
Thanks Pirate, that is exactly what I needed to know. I appreciate all your help. :wave: