如何以编程方式删除窗体控件

时间:2016-07-23 00:50:28

标签: c# winforms

我试图创建一个winForms应用程序,其中,numericUpDown的每次增加最终都应该使面板控件可见,并创建一个文本框控件,一个picturebox,一个numericUpDown控件和三个标签。并且每次减少,都应该删除这组控件。我设法编写了创建控件的代码,但我不知道如何删除它们。我唯一的猜测是,为每个控件分配一个名称,然后使用colorPanel.Controls.RemoveByKey()。不太清楚如何处理nameTextBoxPositionYnewLabelPositionY,在他们目前的状态下,他们可能会搞砸一切。或者我应该放弃,并使用switch(regionNumber),手动创建控件,并根据numericUpDown值让它们可见?考虑到numericUpDown的最大值是10,这将是一件很麻烦的事。

    private Label newLabel;
    private TextBox nameTextBox;
    private NumericUpDown heightNumericUpDown;
    private PictureBox colorPictureBox;
    private string[] newLabelText = {"Name", "Height", "Color"};

    private int newLabelPositionX = -3;
    private int newLabelPositionY = 5;

    private int nameTextBoxPositionX = 74;
    private int nameTextBoxPositionY = 2;
    private void numberOfRegions_ValueChanged(object sender, EventArgs e)
    {            
        int regionNumber = Convert.ToInt32(numberOfRegions.Value);
        int numberOfLabels = 3;            

        if (regionNumber > 0)
        {
            colorPanel.Visible = true;                                                          
            for (int i = 0; i < regionNumber; i++)
            {
                nameTextBox = new TextBox();                                       
                nameTextBox.Size = new System.Drawing.Size(81, 20);
                nameTextBox.Location = new System.Drawing.Point(nameTextBoxPositionX, nameTextBoxPositionY);
                colorPanel.Controls.Add(nameTextBox);
                nameTextBoxPositionY += 78;                                                    
                for (int a = 0; a < numberOfLabels; a++)
                {
                    newLabel = new Label();
                    newLabel.Location = new System.Drawing.Point(newLabelPositionX, newLabelPositionY);
                    newLabel.Text = newLabelText[a];
                    colorPanel.Controls.Add(newLabel);
                    newLabelPositionY += 26;                        
                }                                     
            }
            newLabelPositionY = 5;
            nameTextBoxPositionY = 2;               
        }
        else
        {
            colorPanel.Visible = false;
        }            
    }

3 个答案:

答案 0 :(得分:0)

您可以简单地遍历控件并按名称删除不需要的项目(假设您将其名称存储在数组中,甚至标记,假设您为每个创建的控件分配了一个数字标记(例如regionNumber)

foreach (Control item in colorPanel.Controls.OfType<Control>())
{
    foreach(var name in myItemNames) 
    {
        if (item.Name == name)
        {
            colorPanel.Controls.Remove(item);
            item.Dispose();
            //...

或者您可以清除控件,如果这是您实际想要实现的目标

colorPanel.Controls.Clear();

myItemNames这里是您在生成控件时分配的数组。 e.g。

//before loop
myItemNames = new List<string>();

//...in loop
newLabel = new Label();
newLabel.name = "label"+i;
myItemNames.Add(newLabel.name);

答案 1 :(得分:0)

使用Control.Controls.Remove方法:

this.Controls.Remove(Control);

可能更改您的代码以使用数组,然后您可以通过索引删除控件:

TextBox[] MyTextBoxes = new TextBox[50];

foreach (TextBox textBox in MyTextBoxes)
    this.Controls.Add(textBox);

foreach (TextBox textBox in MyTextBoxes)
    this.Controls.Remove(textBox);

答案 2 :(得分:0)

感谢您的回答,但我已经摆弄了Controls.RemoveByKey()并找到了解决方案:

struct DynamicFormControls
{
    public TextBox textBox;
    public Label label;
}

DynamicFormControls dynamicControls = new DynamicFormControls();
Stack<Control> controlStack = new Stack<Control>();
private string[] newLabelText = {"Name", "Height", "Color"};
private int newLabelPositionX = -3;
private int newLabelPositionY = 5;
private int nameTextBoxPositionX = 74;
private int nameTextBoxPositionY = 2;
private int numberOfLabels = 3;
private int currentValue = 0;
private int previousValue = 0;
private int difference = 0;
private int nameSuffix1 = 1;
private int nameSuffix2 = 1;

private void numberOfRegions_ValueChanged(object sender, EventArgs e)
{
    currentValue = Convert.ToInt32(numberOfRegions.Value);
    if (currentValue == 0)
    {
        colorPanel.Visible = false;
        colorPanel.Size = new System.Drawing.Size(161, 10);
    }
    if (!ValueIncreased())
    {
        RemoveControls();
    }
    else
    {
        colorPanel.Visible = true;            
        CreateControls();
    }
}

private bool ValueIncreased()
{
    if (currentValue > previousValue)
    {
        difference = currentValue - previousValue;
        previousValue = currentValue;
        return true;
    }
    else
    {
        difference = previousValue - currentValue;
        previousValue = currentValue;
        return false;
    }
}

private void CreateControls()
{
    for (int i = 0; i < difference; i++)
    {
        dynamicControls.textBox = new TextBox();
        dynamicControls.textBox.Name = "textBox" + nameSuffix1;
        dynamicControls.textBox.Size = new System.Drawing.Size(81, 20);
        dynamicControls.textBox.Location = new System.Drawing.Point(nameTextBoxPositionX, nameTextBoxPositionY);                
        colorPanel.Controls.Add(dynamicControls.textBox);
        controlStack.Push(dynamicControls.textBox);
        nameTextBoxPositionY += 78;
        nameSuffix1++;                
        for (int a = 0; a < numberOfLabels; a++)
        {
            dynamicControls.label = new Label();
            dynamicControls.label.Name = "label" + nameSuffix2;
            dynamicControls.label.Location = new System.Drawing.Point(newLabelPositionX, newLabelPositionY);
            dynamicControls.label.Text = newLabelText[a];
            colorPanel.Controls.Add(dynamicControls.label);
            controlStack.Push(dynamicControls.label);
            newLabelPositionY += 26;
            nameSuffix2++;
        }
    }
}

private void RemoveControls()
{
    for (int d = 0; d < difference; d++)
    {                
        for (int b = 0; b < 6; b++)
        {
            controlStack.Pop().Dispose();
        }
        nameSuffix1--;            
        if (nameTextBoxPositionY > 2)
        {
            nameTextBoxPositionY -= 78;
        }
        for (int t = 0; t < numberOfLabels; t++)
        {
            nameSuffix2--;
            if (newLabelPositionY > 5)
            {
                newLabelPositionY -= 26;
            }
        }
    }
}