Hi friends,

I am trying to add multiple LABEL controls in my program using a GROUP.I am using the folllowing code

Label newlabel = new Label(); 

void functionname(..... , ....)
{
int i = 0;
            foreach (string s1 in s2)
            {
                newlabel = new Label();
                newlabel.AutoSize = true;
                newlabel.Text = s1;
                newlabel.Location = new System.Drawing.Point(50, 20+30*i);
                newlabel.Name = s1;
                newlabel.Size=new System.Drawing.Size(60,25);
              
                newlabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                groupBox1.Controls.Add(newlabel);
                i++;
            }
}

where s2 is an array consisting of various names which are to be applied to the labels.
I want to associate a click event which will show the name of the label in Messagebox when clicked.
Please help!

foreach (string s1 in s2)
            {
                newlabel = new Label();
                newlabel.AutoSize = true;
                newlabel.Text = s1;
                newlabel.Location = new System.Drawing.Point(50, 20+30*i);
                newlabel.Name = s1;
                newlabel.Size=new System.Drawing.Size(60,25);
              
                newlabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                groupBox1.Controls.Add(newlabel);

//event handler
newlabel.Click+=new EventHandler(LabelClick);
                i++;
            }
void LabelClick(object sender, EventArgs)
{
MessageBox.Show( ((Label)sender).Text);
}

thnks for reply but above code shows
System.Windows.Form.Label,Text:NameofLabel

and output tht i need is
NameofLabel

i think the code provided above is enough.
the complete code contains unnecessary trivia.

Wat i actually need to know is how can we identify a separate label at the runtime whose name we have specified at the runtime only.

I think you just need to change the ShowMessage() to show the .Name property instead of .Text, try this:

foreach (string s1 in s2)
            {
                newlabel = new Label();
                newlabel.AutoSize = true;
                newlabel.Text = s1;
                newlabel.Location = new System.Drawing.Point(50, 20+30*i);
                newlabel.Name = s1;
                newlabel.Size=new System.Drawing.Size(60,25);
              
                newlabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                groupBox1.Controls.Add(newlabel);

//event handler
newlabel.Click+=new EventHandler(LabelClick);
                i++;
            }
void LabelClick(object sender, EventArgs)
{
MessageBox.Show( ((Label)sender).Name);
}

Look, I'll give you simple code
Simply when button clicked It add new label and once the created label clicked; message box shown with its Name and its Text

Label newlabel;
void BtbAddLabel_Click(object sender, EventArgs)
{
newlabel = new Label();
newlabel.AutoSize = true;
newlabel.Text = s1;
newlabel.Location = new System.Drawing.Point(50, 20+30*i);
newlabel.Name = s1;
newlabel.Size=new System.Drawing.Size(60,25);
newlabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
newlabel.Click+=new EventHandler(LabelClick);
groupBox1.Controls.Add(newlabel);
}
void LabelClick(object sender, EventArgs)
{
MessageBox.Show( ((Label)sender).Name+ " " + ((Label)sender).Text);
}

@sknake
i tried tht too ,but it also yields the same result

I think you just need to change the ShowMessage() to show the .Name property instead of .Text, try this:

WHAT :) I think they should overcome this!!!

Comments
you would think but thats obviously not the case ;)

i think the code provided above is enough.
the complete code contains unnecessary trivia.

Wat i actually need to know is how can we identify a separate label at the runtime whose name we have specified at the runtime only.

You can distinguish labels created at runtime with the name assigned at runtime. What is the problem here? Ramy has given you the code...

@ramy
r u sure this shud work coz i am trying it on my system and its yielding d same result as earlier

You can distinguish labels created at runtime with the name assigned at runtime. What is the problem here? Ramy has given you the code...

the code ramy gave does not gives the required output .Wat i need to show in Messagebox() is just the name of the label clicked but it shows
System.Windows.Forms.Label,Text:NameofLabel
and the required output is:NameofLabel

Thanks, Scott.
Scholar, no. It's my fault I didn't note that newlabel.Name = s1; let it newlabel.Name = "name" + i;

Look at the Code that YOU posted. You are setting the name of the label =s2, and the Text of the label=s2. In this case the name and text are identical so what is the problem?

Thanks, Scott.
Scholar, no. It's my fault I didn't note that newlabel.Name = s1; let it newlabel.Name = "name" + i;

but it is not going to affect the result.
when u make it

newlabel.Name = "name" + i

you are trying to assign a differnt name to each label i guess.
but the label name wud be differnet in previous case too as s1 changes due to foreach loop.

What is the desired name and text value for each label? From what I understand the .Name property should come from the s2[] array. Where should the text property come from?

Scott it should pop up empty message box, I tried that don't give Text or Name it works fine (I used BorderStyle) to see the label. Chrome crashes everytime I upload the proof :)

Hi Ramy! Hi Scott!
The OP sees the outcome of this :

void LabelClick(object sender, EventArgs e)
        {
            MessageBox.Show(((Label)sender).ToString());
        }

Probably as a result of his so called "trivia code". Code is seldom trivial perhaps he could show us a bit more?

Comments
He came to put our minds in rest!
good call. i didn't think about that

The code ramy posted was sound. It had a little left to chance, but I don't see how anyone could have a problem with it.

I have included an example project, just compile it and click the button, then go back and read the code.

all credit to this code goes to ramy, I just copy and pasted it in, then modified it slightly to be more obvious in what was what.

Comments
Thanks :)

@Danny: ooooooooooooooh I posted code, what else :)???!! and they didn't show what they wrote!

@Diamond: Thanks for your kind words.

Fully agree with Diamonddrake!
Copied Ramy's code in a project tested it and it worked well.
That's how I find out Scolar must be using a ToSring() somewhere.

This question has already been answered. Start a new discussion instead.