Answered by:
TextBox array color

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();
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!
- Marked as answer by normara Monday, February 10, 2014 9:25 PM
- Unmarked as answer by normara Monday, February 10, 2014 9:27 PM
- Marked as answer by Herro wongMicrosoft contingent staff Monday, February 17, 2014 7:41 AM
All replies
-
-
-
-
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
-
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?
-
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!
- Marked as answer by normara Monday, February 10, 2014 9:25 PM
- Unmarked as answer by normara Monday, February 10, 2014 9:27 PM
- Marked as answer by Herro wongMicrosoft contingent staff Monday, February 17, 2014 7:41 AM
-