Is there any easy method to do this?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jlo6687
    New Member
    • Nov 2009
    • 2

    Is there any easy method to do this?

    there are 64 buttons(button1 ,button2....but ton64).
    Also a textbox and button "ok ".

    when 15 is entered into the textbox and clicked ok Backcolcor of button15 shauold change to red;

    my Approach is
    1.valve entered is passed to i
    2.then using while loop i change the back color to red
    while(i==15)
    {button15.Backc olor=Color.red;
    }

    Above approach works but needs 64 while loops and that makes the code lenghty.


    is there any method to reduce the code

    i want to know wheather i can use "button"+i.tost ring(); here to reduce the code
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    First, no, you cannot build a string and use it to reference a variable.

    Second, why would you use a loop if there is nothing to repeat? You're using "while" statements as if they were "if" statements.

    If I had to do something like this, I would create the buttons in code, in a proper "for loop", rather than in the designer. And as I created them, I would add them to a System.Collecti ons.Generic.Dic tionary<int,But ton>.

    Code:
    Dictionary<int, Buttons> buttonsDict = new Dictionary<int, Buttons>();
    for(int i=1; i<=64; i++)
    {
      Button b = new Button();
      //set all of button b's attributes
      buttonsDict.Add(i, b);
    }
    Now, later, when your user wants to change the background, let's say they typed in "15". You can get that button reference like this:
    Code:
    buttonsDict[15].BackColor = Colors.Red;

    Comment

    • jlo6687
      New Member
      • Nov 2009
      • 2

      #3
      thanks for .i think that solved my problem

      Comment

      Working...