无法操纵动态创建的标签 [英] can't manipulate dynamically created label

查看:100
本文介绍了无法操纵动态创建的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,我正在通过拖放创建动态标签,这是代码:

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();
        }



而且我想通过发送unicode来操纵标签,因此我按如下方式解析unicode:



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("}", ""));
   }
}



此示例我想更改动态创建的标签的背景色,但是由于我没有在加载时创建labelt1,这给我一个错误,因为当前内容中不存在labelt1!



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?

推荐答案

您在哪里尝试使用第二部分代码?当然,如果尚未创建标签,该标签将不可用.
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.


TolgaCiftci写道:
TolgaCiftci wrote:

labelt1.BackColor

labelt1.BackColor



您正在尝试使用显然不存在的命名资源,因为它是动态创建的.您需要使用诸如FindControl之类的方法来从控件层次结构中获取所需的实例.



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.


因此,看起来您正在尝试使用户能够添加多个标签并更改其属性,您正在使用消息传达这些更改.

您真的不应该对此进行硬编码.您可以从消息中获取信息.您可以执行以下操作:

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:

//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("}", ""));
  }
}



我们已经一遍又一遍地告诉你了

当一个方法返回true或false时,您无需在if语句中检查其结果是否为true或false.它只是在代码中添加了一个额外的过程.

代替



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

if (Message.StartsWith("πlabelt1") == true)


它应该只是


it should just be

if (Message.StartsWith("πlabelt1"))


这篇关于无法操纵动态创建的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆