Click here to Skip to main content
16,023,224 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok I am creating dynamic labels by drag and drop and here is the code:
/Drag a Picture
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
            
        }
        
        private void ImageBox_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        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_ = new Label();
            labelCount++;
            buttlbl_.Name = "labelt" + labelCount.ToString();
            buttlbl_.Location = new Point(p2.X, p2.Y);
            buttlbl_.Size = new System.Drawing.Size(37, 37);
            buttlbl_.BackColor = System.Drawing.Color.DarkGray;
            this.Controls.Add(buttlbl_);
            buttlbl_.BringToFront();
            ImageBox.Invalidate();
        }


and I want to manipulate the labels by sending unicode so I parse the unicode as follows:

if (Message.StartsWith("πlabelt1") == true)
{
   if (Message.StartsWith("πlabelt1_BackColor") == true)
   {
      Message = Message.Substring(19);
      labelt1.BackColor = System.Drawing.Color.FromName(Message.Replace("}", ""));
   }
}


This example I want to change the backcolor of the dynamically created label, however since I don't have the labelt1 created on load It is giving me an error as labelt1 doesnt exist in the current content!

How can i do this right, any ideas?
Posted
Updated 2-Jun-10 7:41am
v2

So, it looks like you're trying to give the user the ability to add multiple labels and change their properties, and you're using messages to communicate those changes.

You really shouldn't hardcode that. You can get the information from the message. You can do things like:

C#
//Get control name from message
string controlName = Message.SubString(1, Message.IndexOf('_') - 1);

//remove the control name from the message
Message = Message.SubString(Message.IndexOf('_') + 1);

//see if the control is part of the control collection
Control[] ctrlCollection = this.Controls.Find(controlName, true);

if (ctrlCollection.Count > 0)
{
  //Get the label
  Label messageLabel = (Label)ctrlCollection[0];
  
  //see if we're changing the back color
  if (Message.StartsWith("BackColor"))
  {
    //remove BackColor from the message
    Message = Message.SubString(10);
    
    //Set the BackColor
    messageLabel.BackColor = System.Drawing.Color.FromName(Message.Replace("}", ""));
  }
}


and we have told you this over and over

when a method returns true or false, you don't need to check its result against true or false in an if statement. it just adds an extra process in the code.

Instead of
C#
if (Message.StartsWith("πlabelt1") == true)

it should just be
C#
if (Message.StartsWith("πlabelt1"))
 
Share this answer
 
TolgaCiftci wrote:
labelt1.BackColor


You are trying to use a named resource that obviously doesn't exist because it was created dynamically. You need to use a method such as FindControl to get the instance you are looking for from the control hierarchy.
 
Share this answer
 
Where are you trying to use the second bit of code? Of course the label isn't available if it hasn't been created yet.
 
Share this answer
 
Comments
TolgaCiftci 2-Jun-10 13:49pm    
I use the second bit of code at the beginning as:

private void InputBox_TextChanged(object sender, EventArgs e)
{
if (InputBox.Text.StartsWith("p") == false)//Clear the box if the message does not start with p
{

InputBox.Text = "";

}

if ((InputBox.Text.StartsWith("p") == true) && (InputBox.Text.EndsWith("}") == true))// only process if the message starts with p and ends with }
{
string Message = InputBox.Text;
InputBox.Text = "";// Clear the box when done.

// Butt1 message line
if (Message.StartsWith("plabelt1") == true)
{
if (Message.StartsWith("plabelt1_BackColor") == true)
{
Message = Message.Substring(19);
labelt1.BackColor = System.Drawing.Color.FromName(Message.Replace("}", ""));
}
}

I know it doesnt exist therefore it can't but how am i going to overcome that?

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