I am pretty new to programming, so please do not hate me for dirty code. I would love to get beginner friendly tips if you have some for me. And sorry for my english I hope you can understand me and maybe try my code yourself to see my problem in action.
I have a Windows form with a group box, in the group box is a panel. Directly in the Form there is a Button to add a row with a set of Controls (in my case 1 row has 2 buttons. One without sense now and one to delete the row) into the panel.
The buttons in one row should have the same tags (1-X)
So adding a row with my 2 button-set works "fine". My next plan was to delete the row when i press the delete button from a row.
After Deletion of a row which i identify with Control Tags, I want to decrease the Number Values for Name (Function 2 to Function 1 if I delete the row 1 for example), Tag and Text from each button which has a higher Tag Value than the row number which got deleted. So theres the failure somewhere I guess.
It's working fine to Add and Remove. But not if i want to add a row again. I must click multiple times then to add a new row.
To explain what I want to do:
- |ADD ROW BUTTON|
-
- PANEL
- {
- |Function 1| --- |Delete 1|
- |Function 2| --- |Delete 2|
- |Function 3| --- |Delete 3|
- |Function 4| --- |Delete 4|
- }
-
-
-
-
-
-
-
- PANEL
- {
- |Function 1| --- |Delete 1|
- |Function 2| --- |Delete 2|
- |Function 3| --- |Delete 3|
- }
But they mostly "never" rename like i've planned in my code/mind. Only sometimes when "playing" with it it will rename a button
My Code looks like this (start with a Winform with a panel named pnl and the Row AddButton):
- 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
-
-
- }
- }
I didn't handle free spaces between rows when removing one now since I am stucking at the Problem with the Renaming/Count doesn't work as expected.
Thanks for any help in advice.
Hope you can understand my problem.