C# 方法从面板中删除文本框
我在windows窗体应用程序中有一个方法,尝试从面板中删除两个文本框 在该方法中,我循环遍历面板中的所有控件。总应该有2个面板被移除并添加在一起,但是当移除时,当我按下按钮时,它会随机移除1或2个容器 以下是删除文本框的代码:C# 方法从面板中删除文本框,c#,textbox,controls,panel,C#,Textbox,Controls,Panel,我在windows窗体应用程序中有一个方法,尝试从面板中删除两个文本框 在该方法中,我循环遍历面板中的所有控件。总应该有2个面板被移除并添加在一起,但是当移除时,当我按下按钮时,它会随机移除1或2个容器 以下是删除文本框的代码: private void removeRows() { string descName = "Desc" + (textBoxCounter - 1).ToString(); string costName
private void removeRows()
{
string descName = "Desc" + (textBoxCounter - 1).ToString();
string costName = "Cost" + (textBoxCounter - 1).ToString();
if (textBoxCounter >= 0)
{
foreach (Control c in costItems.Controls)
{
if (c.Name == descName)
{
// Remove the control from the panel and dispose of it
panel.Controls.Remove(c);
c.Dispose();
}
if(c.Name == costName)
{
// Remove the control from the panel and dispose of it
panel.Controls.Remove(c);
c.Dispose();
}
}
// Decrement the counter
// This happens only once since two controls need to be removed
if (textBoxCounter == 0)
textBoxCounter = 0;
else
textBoxCounter--;
}
else
MessageBox.Show("There are no more rows to remove", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
testlabel1.Text = textBoxCounter.ToString();
testlabel2.Text = panel.Controls.Count.ToString();
}
以下是添加按钮的代码:
private void addRows(string desc, string cost)
{
if (textBoxCounter >= maxExpenses)
{
MessageBox.Show("Maximum number of expenses entered", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
TextBox Desc = new TextBox();
TextBox Cost = new TextBox();
// Give the text boxes names
Desc.Name = "Desc" + textBoxCounter.ToString();
Cost.Name = "Cost" + textBoxCounter.ToString();
// Format the text boxes
Desc.Width = panel.Width / 2;
Cost.Width = panel.Width / 4;
// Add the items to the costItems panel
panel.Controls.Add(expenseDesc);
panel.Controls.Add(expenseCost);
// Add the items to the expenses dictionary
panel.Add(Desc, Cost);
// Increment the text box counter variable
textBoxCounter++;
testlabel1.Text = textBoxCounter.ToString();
testlabel2.Text = costItems.Controls.Count.ToString();
}
}
一些需要知道的信息。
始终会添加和删除两个文本框,它们相互关联。
textBoxCounter初始化为0,因此前两个框名称将为“Desc0”和“Cost0”
当我第一次按下按钮删除行时,一个文本框被删除,然后如果我再次按下它,它可能会删除2,它可能只删除1
我试着调试,我注意到迭代面板中所有控件的foreach循环似乎比完整的控件数量少了一次
对我的代码有任何帮助都会很好。您的问题是由
foreach
引起的,在foreach
中修改集合可能会导致一些意外行为。您只想删除名称事先已知的文本框
,那么为什么不使用ControlCollection.RemoveByKey方法呢
如果要删除最后添加的文本框(Desc..
和成本…
),请执行以下操作:
如果要删除所有添加的文本框(假设您有其他类型的文本框
,否则我们可以使用一点LINQ
轻松删除所有文本框):
for(int i=0;i
您的代码有两个问题:您正在处理无法处理的内容,并且您正在以错误的方式对集合(正在修改的集合)进行迭代。您可以通过执行以下操作删除所有控件
:
panel.Controls.Clear();
或者依靠索引向后迭代:
for (int i = panel.Controls.Count - 1; i >= 0; i--)
{
panel.Controls.RemoveAt(i);
}
关于Dispose,如果愿意,您可以使用它,但不需要使用Remove:
for (int i = panel.Controls.Count - 1; i >= 0; i--)
{
panel.Controls[i].Dispose();
}
附言:我问了一些与此相同的问题,结果得到-6分。保留此问题的原因之一正是为了帮助他人(我看到了您用来删除internet中控件的代码,我知道有很多人在使用它)。确实很讽刺。试着让它隐形,不要移除它。
for (int i = panel.Controls.Count - 1; i >= 0; i--)
{
panel.Controls.RemoveAt(i);
}
for (int i = panel.Controls.Count - 1; i >= 0; i--)
{
panel.Controls[i].Dispose();
}