我对编程非常新颖,所以请不要讨厌我的脏代码。如果你有一些适合我的话,我很乐意为你提供初学者友好的提示。抱歉我的英语,我希望你能理解我,也许你自己尝试我的代码,看看我的问题在行动。动态控制中的C#逻辑故障代码
我有一个Windows窗体与一个组框,在组框中是一个面板。直接在窗体中有一个按钮,用一组控件添加一行(在我的情况下,1行有2个按钮,一个没有感觉,一个可以删除行)放到面板中。
在一排按钮应具有相同的标签(1-X)
因此增加一排我的2个按钮的集作品“精”。我的下一个计划是当我按下一行中的删除按钮时删除该行。
删除了一个我认同控制标签的行后,我想减少名称的数值(如果我删除第1行的功能2到功能1),每个按钮的标签和文本有一个标记值高于删除的行号。所以我认为这是失败的地方。
它的工作正常,添加和删除。但不是,如果我想再次添加一行。我必须多次点击才能添加新行。
为了解释什么,我想做的事:
|ADD ROW BUTTON| //Clicked 4 Times already PANEL { |Function 1| --- |Delete 1| |Function 2| --- |Delete 2| |Function 3| --- |Delete 3| |Function 4| --- |Delete 4| } // Clicking |Delte 2| // Removed Row 2 and rename those which have bigger Row number as the deleted row with decreased value for the Text, Tag & Name // So row 2 gets removed, Function 3 becomes Function 2 and Function 4 becomes Function 3 PANEL { |Function 1| --- |Delete 1| |Function 2| --- |Delete 2| |Function 3| --- |Delete 3| }
但他们大多是“从不”重命名就像我在我的代码/心中已经计划。只有有时,当“玩”它会重命名按钮
我的代码看起来是这样的(开始与面板PNL命名和行Add按钮一个WinForm):
public partial class Form1 : Form
{
int rowCount = 0;
int lastY;
public Form1()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
AddRow();
}
public void AddRow()
{
rowCount++;
label1.Text = rowCount.ToString();
#region Create the Function Button in a row
Button btnFunction = new Button();
if (rowCount == 1)
{
btnFunction.Location = new Point(10, 5);
}
else if (rowCount > 1)
{
Control[] letztesY = pnl.Controls.Find($"btnFunction{rowCount - 1}", true);
foreach (var button in letztesY)
{
lastY = button.Location.Y;
}
btnFunction.Location = new Point(10, lastY + 25);
}
else
{
MessageBox.Show("Unerwarteter Fehler");
}
btnFunction.Name = "btnFunction" + rowCount;
btnFunction.Tag = rowCount;
btnFunction.Text = "Function " + rowCount.ToString();
pnl.Controls.Add(btnFunction);
#endregion
#region Create the Delete Row Button in a row
Button btnDelete = new Button();
if (rowCount == 1)
{
btnDelete.Location = new Point(85, 5);
}
else if (rowCount > 1)
{
Control[] letztesY = pnl.Controls.Find($"btnDelete{rowCount - 1}", true);
foreach (var button in letztesY)
{
lastY = button.Location.Y;
}
btnDelete.Location = new Point(85, lastY + 25);
}
else
{
MessageBox.Show("Fail");
}
btnDelete.Name = "btnDelete" + rowCount;
btnDelete.Tag = rowCount;
btnDelete.Text = "Delete " + rowCount.ToString();
btnDelete.Click += new EventHandler(deleteCallerRow);
pnl.Controls.Add(btnDelete);
#endregion
}
private void deleteCallerRow(object sender, EventArgs e)
{
rowCount--;
label1.Text = rowCount.ToString();
Button delCaller = sender as Button;
#region DeltePressedRowDelButton
Control[] toBeRemovedDelButtons = pnl.Controls.Find($"btnDelete{delCaller.Tag}", true);
foreach (var delBtn in toBeRemovedDelButtons)
{
pnl.Controls.Remove(delBtn);
delBtn.Dispose();
}
Control[] toBeRemovedFunctionButtons = pnl.Controls.Find($"btnFunction{delCaller.Tag}", true);
foreach (var funcBtn in toBeRemovedFunctionButtons)
{
pnl.Controls.Remove(funcBtn);
funcBtn.Dispose();
}
#endregion
#region Decrease Numbers in Buttons Texts, Tags and Names
Control[] toBeRenamedDeleteButtons = pnl.Controls.Find($"btnDelete", true);
foreach (var db in toBeRenamedDeleteButtons)
{
if (Convert.ToInt32(db.Tag) > Convert.ToInt32(delCaller.Tag))
{
db.Name = "btnDelete" + Convert.ToString(Convert.ToInt32(db.Tag) - 1);
db.Tag = Convert.ToInt32(db.Tag) - 1;
db.Text = "Delete " + Convert.ToString(Convert.ToInt32(db.Tag) - 1);
}
}
Control[] toBeRenamedFunctionButtons = pnl.Controls.Find($"btnFunction", true);
foreach (var fb in toBeRenamedFunctionButtons)
{
if (Convert.ToInt32(fb.Tag) > Convert.ToInt32(delCaller.Tag))
{
fb.Name = "btnFunction" + Convert.ToString(Convert.ToInt32(fb.Tag) - 1);
fb.Tag = Convert.ToInt32(fb.Tag) - 1;
fb.Text = "Function " + Convert.ToString(Convert.ToInt32(fb.Tag) - 1);
}
}
#endregion
}
}
,所以我没有处理行之间的空闲空间时,现在删除一个,因为我在重命名问题与问题的预期不起作用。
感谢您的任何建议帮助。
希望你能理解我的问题。
那么,如果我alwas删除最后一行,也是多次,rowCount也适用于添加和循环此过程。但是,如果我删除一些行之间并添加一些新的行,rowCounter不再是真实的。 –