Click here to Skip to main content
15,074,464 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating dynamic labels by dragging and dropping. My question is how would I know the name of the generated labels so that I can manipulate them by sending Unicodes?

Here is my code:
private void ImageBox_DragDrop(object sender, DragEventArgs e)
        {
            Graphics g = ImageBox.CreateGraphics();
            g.DrawImage((Image)e.Data.GetData(DataFormats.Bitmap),
                new Point(e.X - this.Left, e.Y - this.Top - 150));
            Point p2 = PointToClient(Cursor.Position);
            Label buttlbl_0 = new Label();
            buttlbl_0.Location = new Point(p2.X, p2.Y);
            buttlbl_0.Size = new System.Drawing.Size(37, 37);
            buttlbl_0.BackColor = System.Drawing.Color.DarkGray;
            this.Controls.Add(buttlbl_0);
            buttlbl_0.BringToFront();
            ImageBox.Invalidate();
        }    
    }
Posted
Updated 1-Jun-10 9:59am
v2
Comments
Henry Minute 1-Jun-10 15:14pm
   
In reply to your comment on John's Answer: No you have to take care of creating a different name for each of your labels. One way is to have a member global to your Form and increment it as each label is added. e.g. private int labelCount = 0; then in ImageBox_DragDrop() buttlbl_0.Name = "labelt_" + ++labelCount.ToString();

Or similar.

1 solution

You could - ummm - set the Name property...

At that point, you can look for them after they're added to the Controls list.
   
Comments
TolgaCiftci 1-Jun-10 13:32pm
   
When I add the name property.. lets say I add the name labelt, will it go as labelt1, labelt2 as I create new labels?
#realJSOP 1-Jun-10 16:09pm
   
Yeah, use label.Name = string.Format("label{0:000}", counter); and you'll get "label001", "label002", etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900