7o 7

7o 7

  • NA
  • 2
  • 529

C# Logic failure in dynamic Controls Code

Jun 12 2017 8:12 PM

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:

  1. |ADD ROW BUTTON| //Clicked 4 Times already  
  2.   
  3. PANEL  
  4. {  
  5. |Function 1| --- |Delete 1|  
  6. |Function 2| --- |Delete 2|  
  7. |Function 3| --- |Delete 3|  
  8. |Function 4| --- |Delete 4|  
  9. }  
  10.   
  11.   
  12.   
  13. // Clicking |Delte 2|  
  14. // Removed Row 2 and rename those which have bigger Row number as the deleted row with decreased value for the Text, Tag & Name  
  15. // So row 2 gets removed, Function 3 becomes Function 2 and Function 4 becomes Function 3  
  16.   
  17. PANEL  
  18. {  
  19. |Function 1| --- |Delete 1|  
  20. |Function 2| --- |Delete 2|   
  21. |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):

  1. public partial class Form1 : Form  
  2. {  
  3.     int rowCount = 0;  
  4.     int lastY;  
  5.   
  6.     public Form1()  
  7.     {  
  8.         InitializeComponent();  
  9.     }  
  10.   
  11.     private void btnAdd_Click(object sender, EventArgs e)  
  12.     {  
  13.         AddRow();  
  14.     }  
  15.   
  16.     public void AddRow()  
  17.     {  
  18.         rowCount++;  
  19.         label1.Text = rowCount.ToString();  
  20.         #region Create the Function Button in a row  
  21.         Button btnFunction = new Button();  
  22.         if (rowCount == 1)  
  23.         {  
  24.             btnFunction.Location = new Point(10, 5);  
  25.         }  
  26.         else if (rowCount > 1)  
  27.         {  
  28.             Control[] letztesY = pnl.Controls.Find($"btnFunction{rowCount - 1}"true);  
  29.   
  30.             foreach (var button in letztesY)  
  31.             {  
  32.                 lastY = button.Location.Y;  
  33.   
  34.             }  
  35.             btnFunction.Location = new Point(10, lastY + 25);  
  36.         }  
  37.         else  
  38.         {  
  39.             MessageBox.Show("Unerwarteter Fehler");  
  40.         }  
  41.   
  42.         btnFunction.Name = "btnFunction" + rowCount;  
  43.         btnFunction.Tag = rowCount;  
  44.         btnFunction.Text = "Function " + rowCount.ToString();  
  45.         pnl.Controls.Add(btnFunction);  
  46.         #endregion  
  47.  
  48.         #region Create the Delete Row Button in a row  
  49.         Button btnDelete = new Button();  
  50.         if (rowCount == 1)  
  51.         {  
  52.             btnDelete.Location = new Point(85, 5);  
  53.         }  
  54.         else if (rowCount > 1)  
  55.         {  
  56.             Control[] letztesY = pnl.Controls.Find($"btnDelete{rowCount - 1}"true);  
  57.   
  58.             foreach (var button in letztesY)  
  59.             {  
  60.                 lastY = button.Location.Y;  
  61.   
  62.             }  
  63.             btnDelete.Location = new Point(85, lastY + 25);  
  64.         }  
  65.         else  
  66.         {  
  67.             MessageBox.Show("Fail");  
  68.         }  
  69.   
  70.         btnDelete.Name = "btnDelete" + rowCount;  
  71.         btnDelete.Tag = rowCount;  
  72.         btnDelete.Text = "Delete " + rowCount.ToString();  
  73.         btnDelete.Click += new EventHandler(deleteCallerRow);  
  74.         pnl.Controls.Add(btnDelete);  
  75.         #endregion  
  76.   
  77.     }  
  78.   
  79.     private void deleteCallerRow(object sender, EventArgs e)  
  80.     {  
  81.         rowCount--;  
  82.         label1.Text = rowCount.ToString();  
  83.   
  84.         Button delCaller = sender as Button;  
  85.  
  86.         #region DeltePressedRowDelButton  
  87.         Control[] toBeRemovedDelButtons = pnl.Controls.Find($"btnDelete{delCaller.Tag}"true);                     
  88.         foreach (var delBtn in toBeRemovedDelButtons)  
  89.         {  
  90.             pnl.Controls.Remove(delBtn);  
  91.             delBtn.Dispose();  
  92.   
  93.         }  
  94.   
  95.         Control[] toBeRemovedFunctionButtons = pnl.Controls.Find($"btnFunction{delCaller.Tag}"true);  
  96.         foreach (var funcBtn in toBeRemovedFunctionButtons)  
  97.         {  
  98.             pnl.Controls.Remove(funcBtn);  
  99.             funcBtn.Dispose();  
  100.   
  101.         }  
  102.         #endregion  
  103.  
  104.         #region Decrease Numbers in Buttons Texts, Tags and Names  
  105.         Control[] toBeRenamedDeleteButtons = pnl.Controls.Find($"btnDelete"true);  
  106.         foreach (var db in toBeRenamedDeleteButtons)  
  107.         {  
  108.             if (Convert.ToInt32(db.Tag) > Convert.ToInt32(delCaller.Tag) )  
  109.             {  
  110.                 db.Name = "btnDelete" + Convert.ToString(Convert.ToInt32(db.Tag) - 1);  
  111.                 db.Tag = Convert.ToInt32(db.Tag) - 1;  
  112.                 db.Text = "Delete " + Convert.ToString(Convert.ToInt32(db.Tag) - 1);  
  113.             }  
  114.         }  
  115.   
  116.         Control[] toBeRenamedFunctionButtons = pnl.Controls.Find($"btnFunction"true);  
  117.         foreach (var fb in toBeRenamedFunctionButtons)  
  118.         {  
  119.             if (Convert.ToInt32(fb.Tag) > Convert.ToInt32(delCaller.Tag))  
  120.             {  
  121.                 fb.Name = "btnFunction" + Convert.ToString(Convert.ToInt32(fb.Tag) - 1);  
  122.                 fb.Tag = Convert.ToInt32(fb.Tag) - 1;  
  123.                 fb.Text = "Function " + Convert.ToString(Convert.ToInt32(fb.Tag) - 1);  
  124.             }  
  125.   
  126.         }  
  127.         #endregion  
  128.   
  129.   
  130.     }  

 

 If i alwas remove the last row, also multiple times, the rowCount works also when adding and loop this procedere. But if i delete some rows between and addin some new ones, the rowCounter is not true anymore.

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.


Answers (1)