locked
Dynamically Create "Remove" WinForm Buttons RRS feed

  • Question

  • I created a topic (http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/40b20051-0b27-4fe0-b0b7-a56b919d03bc) needing assistance adding buttons dynamically.  That part works great but when I remove the buttons to create a new set some of the buttons have text values that were assigned to previous buttons that I tried to remove.  I guess its safe to say that my buttons have not been removed properly. I tried to debug the app and the values seem correct.  I think the problem is how I am attempting to remove my buttons from the group box.  Does this look correct for removing the bottons?

    	    foreach(Control p in phraseBox.Controls)
                {
                    phraseBox.Controls.Remove(p);
                }


    • Edited by LluvianBlack Friday, May 17, 2013 12:07 PM
    • Moved by CoolDadTx Friday, May 17, 2013 2:07 PM Winforms related
    Friday, May 17, 2013 12:06 PM

Answers

  • 
    

    You are changing the collection itself by removing the items from it and it may create a problem.

    If you have RemoveAll the use it or otherwise you should use something like this

    int i = phraseBox.COntrols.count;
    
    for(int j =0; j < i; j++)
    {
       phraseBox.Controls.Remove(phraseBox.Controls[0]);
    }
    


    ViBi

    • Marked as answer by LluvianBlack Friday, May 17, 2013 6:03 PM
    Friday, May 17, 2013 12:15 PM
  • I think see the problem:

    You used foreach.

    Foreach is for "readonly" access only. I can savely asume that Rempve(p) is not removing the control at all. If it would, foreach would throw an exception next loop! Under no circumstances is the element iterated over allowed to change between foreach loops. (though this might not be an issue if "Controls" is inmutable). Have you checked if the elements are ever removed?

    Also could it be that you add the elements again? (you never clear the list of items to add).

    Overall we need a lot more code. Ideally a ready-to-compile example that demostrates your issue.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    • Marked as answer by LluvianBlack Friday, May 17, 2013 6:03 PM
    Friday, May 17, 2013 12:17 PM

All replies

  •  Does this look correct for removing the bottons?

    Yes, except for the fact that you are removing all controls and not only buttons.
    Friday, May 17, 2013 12:14 PM
  • 
    

    You are changing the collection itself by removing the items from it and it may create a problem.

    If you have RemoveAll the use it or otherwise you should use something like this

    int i = phraseBox.COntrols.count;
    
    for(int j =0; j < i; j++)
    {
       phraseBox.Controls.Remove(phraseBox.Controls[0]);
    }
    


    ViBi

    • Marked as answer by LluvianBlack Friday, May 17, 2013 6:03 PM
    Friday, May 17, 2013 12:15 PM
  • I think see the problem:

    You used foreach.

    Foreach is for "readonly" access only. I can savely asume that Rempve(p) is not removing the control at all. If it would, foreach would throw an exception next loop! Under no circumstances is the element iterated over allowed to change between foreach loops. (though this might not be an issue if "Controls" is inmutable). Have you checked if the elements are ever removed?

    Also could it be that you add the elements again? (you never clear the list of items to add).

    Overall we need a lot more code. Ideally a ready-to-compile example that demostrates your issue.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    • Marked as answer by LluvianBlack Friday, May 17, 2013 6:03 PM
    Friday, May 17, 2013 12:17 PM
  • Here is my over all objective.  I want to create a wheel of fortune type random buttons that show up in a pattern based on a string that is randomly selected by a class. That is why that groupbox is named phrase.  I have the program right now recognizing the correct input from user when they select a letter and places the text in the button.  When I need to throw up a new string of words that are reflected by blank buttons the words no longer appear as the problem since changing the for loop; however, the pattern of the buttons is not consistent and works 50% of the time at best.
    Friday, May 17, 2013 2:16 PM