none
TextBox array color RRS feed

  • Question

  • hie I have an array of text boxes which are populated by a random number generator (1,49). I would like to have the each Textbox to change color according to a range of numbers. for example if the number is less than 10, the Textbox should be blue. If number is between 10 and 20 textbox should be green, etc. Ant one with an idea? here is the code

    Random RandomClass = new Random();
    TextBox [] textboxes = new Control[]{TextBox1,TextBox2,TextBox3,TextBox4,TextBox5,TextBox6};
    
    foreach(TextBox c in textboxes) 
        c.Text = RandomClass.Next(1,49).ToString();
    Sunday, February 9, 2014 12:31 AM

Answers

  • You won't be able to sort the textboxes, as the sort won't be based on the textbox text. Here is a modified version of Noam's example, where you can get the random numbers first, sort them, then put the numbers into the textboxes and set the colors.

    Random rnd = new Random();
    Control[] textboxes = new Control[] { textBox1, textBox2, };
    Color[] colors = new Color[] { Color.Blue, Color.Red }; // Add as mush color as you need
    int[] nums = new int[2];
    
    for (int i = 0; i <= 1; i++) //change to loop for number of textboxes
    {
        nums[i] = rnd.Next(1, 49);
    }
    Array.Sort(nums);
    
    for (int i = 0; i <= textboxes.Length - 1; i++)
    {
        textboxes[i].Text = nums[i].ToString();
        if ((nums[i] > 1) && (nums[i] < 25))
            textboxes[i].BackColor = colors[0];
        else
            textboxes[i].BackColor = colors[1];
        // Put mor if's to have more choices for colors
    }


    - Brady My posts are kept as simple as possible for easier understanding. In many cases you can probably optimize or spruce up what I present. Have fun coding!

    Monday, February 10, 2014 12:25 AM

All replies

  • Hello,

    Do you want to change the color of the text or the color of the control? in this case what part of the control needs to change color? the border?


    Regards, Eyal Shilony

    Sunday, February 9, 2014 12:44 AM
  • I need to change the color of the control...

    and to change the control and not the border

    thanks


    • Edited by normara Sunday, February 9, 2014 12:49 AM
    Sunday, February 9, 2014 12:48 AM
  • You can use the BackColor property of the Textbox.

    this.textBox1.BackColor = Color.Blue;


    - Brady My posts are kept as simple as possible for easier understanding. In many cases you can probably optimize or spruce up what I present. Have fun coding!

    Sunday, February 9, 2014 1:16 AM
  • Here you go:

                Random rnd = new Random();
                Control[] textboxes = new Control[] { textBox1, textBox2, };
                Color[] colors = new Color[]{Color.Blue, Color.Red}; // Add as mush color as you need
                foreach (TextBox c in textboxes)
                {
                    int randomNumber = rnd.Next(1, 49);
                    c.Text = randomNumber.ToString();
    
                    if ((randomNumber > 1) && (randomNumber < 25))
                        c.BackColor = colors[0];
                    else
                        c.BackColor = colors[1];
                    // Put mor if's to have more choices for colors
                }
    

     

    Noam B.


    <hr> Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Proposed as answer by Noam B Sunday, February 9, 2014 12:25 PM
    Sunday, February 9, 2014 12:25 PM
  • cool thanx that works fine.

    I have six text boxes. Now  want to sort then so that they read from smallest generated number to the highest.

    so I have a sort button

    protected void sortBtn_Click(object sender, EventArgs e)
        {
            Array.Sort(textboxes);
        }

    And this doesnt seem to work right either. I tried to include OrderBy and this gets rejected. how can i achieve this?

    Sunday, February 9, 2014 2:30 PM
  • You won't be able to sort the textboxes, as the sort won't be based on the textbox text. Here is a modified version of Noam's example, where you can get the random numbers first, sort them, then put the numbers into the textboxes and set the colors.

    Random rnd = new Random();
    Control[] textboxes = new Control[] { textBox1, textBox2, };
    Color[] colors = new Color[] { Color.Blue, Color.Red }; // Add as mush color as you need
    int[] nums = new int[2];
    
    for (int i = 0; i <= 1; i++) //change to loop for number of textboxes
    {
        nums[i] = rnd.Next(1, 49);
    }
    Array.Sort(nums);
    
    for (int i = 0; i <= textboxes.Length - 1; i++)
    {
        textboxes[i].Text = nums[i].ToString();
        if ((nums[i] > 1) && (nums[i] < 25))
            textboxes[i].BackColor = colors[0];
        else
            textboxes[i].BackColor = colors[1];
        // Put mor if's to have more choices for colors
    }


    - Brady My posts are kept as simple as possible for easier understanding. In many cases you can probably optimize or spruce up what I present. Have fun coding!

    Monday, February 10, 2014 12:25 AM
  • thanks Brady
    Monday, February 10, 2014 9:25 PM