删除在运行时CF C#上创建的标签

Ign*_*mez 2 c# label compact-framework winforms

我在方法中动态创建标签"label1".然后,当我单击一个按钮时,我想删除创建的标签,但是如果我编写Controls.Remove(label1),则表示该控件在上下文中不存在.我怎么能做到这一点?

编辑:按照Jon的建议,我实现了foreach循环,但它没有做任何事情.这是我的代码,我使用的面板是由设计创建的:

void GenerateControls() {
    Label labelOne = new Label();
    Button btnContinue = new Button();
    panel.SuspendLayout();
    SuspendLayout();
    //btnContinue
    btnContinue.BackColor = System.Drawing.Color.Black;
    btnContinue.ForeColor = System.Drawing.SystemColors.Menu;
    btnContinue.Location = new System.Drawing.Point(145, 272);
    btnContinue.Name = "btnContinue";
    btnContinue.Size = new System.Drawing.Size(95, 28);
    btnContinue.TabIndex = 13;
    btnContinue.Text = "Continue";
    btnContinue.Visible = true;
    Controls.Add(btnContinue);
    btnContinue.Click += new System.EventHandler(btnContinue_Click);            
    //labelOne            
    labelOne.Location = new Point(0,65); 
    labelOne.Size = new System.Drawing.Size(100,20);
    labelOne.Text = "labelOne";
    labelOne.Name = "labelOne";
    labelOne.Visible = true;
    labelOne.TextChanged += new System.EventHandler(this.lbl_TextChanged);
    labelOne.BackColor = System.Drawing.Color.PaleGreen;
    Controls.Add(labelOne);
    //panel
    panel.Controls.Add(labelOne);
    panel.Visible = true;
    panel.Location = new Point(0,0);
    panel.Size = new Size(240, 320);
    //
    Controls.Add(panel);
    panel.ResumeLayout();
    ResumeLayout();
}
Run Code Online (Sandbox Code Playgroud)

然后在我点击btnContinue时:

private void btnContinuar_Click(object sender, EventArgs e) {
    foreach (Control control in panel.Controls) {
        if (control.Name == "labelOne"){
            panel.Controls.Remove(control);
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我调试它并在面板中.控制它继续,好像它是空面板.谢谢你的帮助!

Jon*_*eet 5

我怀疑它说该变量在该上下文中不存在.您必须通过文本找到标签,或者了解其他相关信息.例如,当您创建它时,您可以设置Name属性并在您想要删除它时找到它:

panel.Controls.RemoveByKey("YourLabelName");
Run Code Online (Sandbox Code Playgroud)

编辑:如评论RemoveByKey中所述,紧凑框架中不存在.所以你要么必须自己记住引用(在这种情况下你不需要名字)或使用类似的东西:

foreach (Control control in panel.Controls)
{
    if (control.Name == "YourLabelName")
    {
        panel.Controls.Remove(control);
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

EDIT2:为了使它更加"通用"和桌面兼容,您可以保留RemoveByKey调用并将其添加到您的应用中:

public static class FormExtensions
{
    public static void RemoveByKey(this Control.ControlCollection collection,
                                   string key)
    {
        if(!RemoveChildByName(collection, key))
        {
            throw new ArgumentException("Key not found");
        }
    }

    private static bool RemoveChildByName(
        this Control.ControlCollection collection,
        string name)
    {
        foreach (Control child in collection)
        {
            if (child.Name == name)
            {
                collection.Remove(child);
                return true;
            }

            // Nothing found at this level: recurse down to children.
            if (RemoveChildByName(child.Controls, name))
            {
                return true;
            }
        }

        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @nachogsiri我认为这个答案应该标记为答案,我的答案根本不会反映这个问题,而后来出现的人会对为什么这是答案感到困惑.继续并将其标记为有用,但将答案更改为Jon的 (2认同)