Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Creating a loop(?)

One word! Im Fawkin Pro!
Loyal Member
Joined
Jul 1, 2010
Messages
1,254
Reaction score
359
Hello!

In programming this is one of the difficult coding I have yet to master.

I have made a (mini) program that contains textboxes and a button.
c488175616ad62e328b4e39b179fc8d0 - Creating a loop(?) - RaGEZONE Forums


Works like when you write a number and then press the button it should change the color of that many textboxes.. example:
4ba3770fe0655a608eb355909e9eea6e - Creating a loop(?) - RaGEZONE Forums


The code is written to make this manually... But how would I code this into a loop or something like that?

My current code:

Code:
private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "1")
            {
                textBox2.BackColor = Color.DarkRed;
                textBox2.Visible = true;
                textBox3.Visible = false;
                textBox4.Visible = false;
                textBox5.Visible = false;
                textBox6.Visible = false;
                textBox7.Visible = false;
                textBox8.Visible = false;
            }
            else if (textBox1.Text == "2")
            {
                textBox2.BackColor = Color.DarkRed;
                textBox2.Visible = true;
                textBox3.BackColor = Color.DarkRed;
                textBox3.Visible = true;
                textBox4.Visible = false;
                textBox5.Visible = false;
                textBox6.Visible = false;
                textBox7.Visible = false;
                textBox8.Visible = false;
            }
            else if (textBox1.Text == "3")
            {
                textBox2.BackColor = Color.DarkRed;
                textBox2.Visible = true;
                textBox3.BackColor = Color.DarkRed;
                textBox3.Visible = true;
                textBox4.BackColor = Color.DarkRed;
                textBox4.Visible = true;
                textBox5.Visible = false;
                textBox6.Visible = false;
                textBox7.Visible = false;
                textBox8.Visible = false;
            }
            else if (textBox1.Text == "4")
            {
                textBox2.BackColor = Color.DarkRed;
                textBox2.Visible = true;
                textBox3.BackColor = Color.DarkRed;
                textBox3.Visible = true;
                textBox4.BackColor = Color.DarkRed;
                textBox4.Visible = true;
                textBox5.BackColor = Color.DarkRed;
                textBox5.Visible = true;
                textBox6.Visible = false;
                textBox7.Visible = false;
                textBox8.Visible = false;
            }
            else if (textBox1.Text == "5")
            {
                textBox2.BackColor = Color.DarkRed;
                textBox2.Visible = true;
                textBox3.BackColor = Color.DarkRed;
                textBox3.Visible = true;
                textBox4.BackColor = Color.DarkRed;
                textBox4.Visible = true;
                textBox5.BackColor = Color.DarkRed;
                textBox5.Visible = true;
                textBox6.BackColor = Color.DarkRed;
                textBox6.Visible = true;
                textBox7.Visible = false;
                textBox8.Visible = false;
            }
            else if (textBox1.Text == "6")
            {
                textBox2.BackColor = Color.DarkRed;
                textBox2.Visible = true;
                textBox3.BackColor = Color.DarkRed;
                textBox3.Visible = true;
                textBox4.BackColor = Color.DarkRed;
                textBox4.Visible = true;
                textBox5.BackColor = Color.DarkRed;
                textBox5.Visible = true;
                textBox6.BackColor = Color.DarkRed;
                textBox6.Visible = true;
                textBox7.BackColor = Color.DarkRed;
                textBox7.Visible = true;
                textBox8.Visible = false;
            }
            else if (textBox1.Text == "7")
            {
                textBox2.BackColor = Color.DarkRed;
                textBox2.Visible = true;
                textBox3.BackColor = Color.DarkRed;
                textBox3.Visible = true;
                textBox4.BackColor = Color.DarkRed;
                textBox4.Visible = true;
                textBox5.BackColor = Color.DarkRed;
                textBox5.Visible = true;
                textBox6.BackColor = Color.DarkRed;
                textBox6.Visible = true;
                textBox7.BackColor = Color.DarkRed;
                textBox7.Visible = true;
                textBox8.BackColor = Color.DarkRed;
                textBox8.Visible = true;
            }
            else
            {
                MessageBox.Show("Please choose a number between 1 to 7!!");
            }

Would really appriciate help for this one.. I have tried to look into loops & such.. Couldn't figure it out :/
And as I said at the top.. Loops and try's & do has been the hardest part of me to understand fully (Still learning to code :p)

~Xakzi
 

Attachments

You must be registered for see attachments list
Junior Spellweaver
Joined
Oct 18, 2009
Messages
143
Reaction score
24
Code:
TextBox[] textboxes = {textBox2, textBox3, textBox4, textBox5,textBox6, textBox7, textBox8};
int number = Convert.ToInt32(textBox1.Text);
if (number <= 7 && number >= 1)
{
    for (int i = 0; i < textboxes.length; i++)
    {
        if (i < number)
	{
	    textboxes[i].BackColor = Color.DarkRed;
	    textboxes[i].Visible = true;
	}
	else 
	{
	    textboxes[i].Visible = false;
	}
}
else
{
    MessageBox.Show("Please choose a number between 1 to 7!!");
}

Alright, and now for an explanation.

Firstly we will create an array of textboxes that holds all the text boxes that will be changed.
Then we get the users input and convert it into an integer.
Next we create a loop to loop through all the TextBoxes in the array.
If the number we entered is less than i, we know we need to show this TextBox and set its color to red and make it visible, otherwise we set its visibility to false.
 
Put Community First
Loyal Member
Joined
Oct 2, 2014
Messages
1,114
Reaction score
833
Also, to expand a little on the explanation, arrays start at 0 being the first element. So this section:

Code:
if (number <= 7 && number >= 1)
{
for (int i = 0; i < textboxes.length; i++)

is checking if the number is between 1 and 7 on the input, then for the loop it checks between 0 and 6 (textboxes array length), which make up the range of 7 numbers you could input. So if you had a max of 12 boxes that could appear and be coloured, it would check between 0 and 11.
 
Back
Top