Adding textbox input to array

photo123

Member
Joined
Feb 11, 2020
Messages
18
Programming Experience
Beginner
Inside my form I have this code:

int[] totalScoreArray = new int[20];
int intScoreCount = 0;

For my assignment I need to: Modify the Click event handler for the Add button so it adds the score that’s entered by the user to the next element in the array. To do that, you can use the score count variable to refer to the element.

What I'm having trouble with: All of the scores are entered into a textbox that I have on my form called txtScore. I know that to add items to an array you just put the array name and then write .Add(); But, here I'm not sure what to use since I'm taking the text boxes input and not an actual number. So I know I can't write totalScoreArray.Add(txtScore.Text); but that is what I want to do, meaning I want to take the scores entered in the txtScore text box and put them into the next element. I'm not sure how to do this and I'm not really sure what I'm supposed to search to figure this out. I tried array C# but the examples I found used List<TextBox> myTextboxList = new List<TextBox>();. I can't use List because my directions want me to write it using regular arrays. Can someone please help me out? *No I'm not trying to get the answer, I seriously want to understand this.
 
I know that to add items to an array you just put the array name and then write .Add();
You know no such thing. That's how you add to a collection, e.g. a List<T>, but you're using an array here, not a collection.

Think of an array like an egg carton. All the elements are already there but they are empty by default. You don't add an item. You set an element. You specify which element to set by providing an index. That's what the int variable is for, as the assignment says. It starts with a value of 0, so you will be setting the first element. Each time you set an element, increment that variable and you'll refer to the next element next time. Once you've set all 20 elements, the array is effectively full.

A TextBox contains text, which is represented as a string. You need to get that string, convert it to an int and then add that number to the array.
 
You know no such thing. That's how you add to a collection, e.g. a List<T>, but you're using an array here, not a collection.

Think of an array like an egg carton. All the elements are already there but they are empty by default. You don't add an item. You set an element. You specify which element to set by providing an index. That's what the int variable is for, as the assignment says. It starts with a value of 0, so you will be setting the first element. Each time you set an element, increment that variable and you'll refer to the next element next time. Once you've set all 20 elements, the array is effectively full.

A TextBox contains text, which is represented as a string. You need to get that string, convert it to an int and then add that number to the array.
First I converted the string from the text box to an int.
intScoreCount = Convert.ToInt32(txtScore.Text);

then I made a for loop

//I started at 0, made sure the length is less then 20 since you count from 0 then in the index 0 i added the variable that I used to convert the string to int, lastly I incremented the count variable
for(intScoreCount=0; totalScoreLength < 20)
totalScoreArray[0]=intScoreCount;
intScoreCount++;
 
Please remember to use code tags when posting code, no matter how small the amount of code is.

Add your variables:
        private int[] totalScoreArray = new int[20];
        private int intScoreCount = 0;
        private int i = 0;
Add your button click event:
private void button1_Click(object sender, EventArgs e)
        {
   
        }
In your button click event, check if i is less than 20:
if (i < 20)
                //Additional checks here
            else
                MessageBox.Show("Empty it first", "Scoreboard full", MessageBoxButtons.OK);
For additional checks, see if the user first typed a number by parsing the textbox:
                if (int.TryParse(textBox1.Text, out int isNumber))
                {
                    totalScoreArray[i] = isNumber;
                    i++;
                }
                else
                    MessageBox.Show($"{textBox1.Text} is not a number, check and try again.", "Wrong value provided", MessageBoxButtons.OK);
Finished result should look like this:
        private int[] totalScoreArray = new int[20];
        private int intScoreCount = 0;
        private int i = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            if (i < 20)
                if (int.TryParse(textBox1.Text, out int isNumber))
                {
                    totalScoreArray[i] = isNumber;
                    i++;
                }
                else
                    MessageBox.Show($"{textBox1.Text} is not a number, check and try again.", "Wrong value provided", MessageBoxButtons.OK);
            else
                MessageBox.Show("Empty it first", "Scoreboard full", MessageBoxButtons.OK);
        }
You could optionally merge your if statements for cleanness. I didn't and left them separate for simplicity to understand:
            if (i < 20 && int.TryParse(textBox1.Text, out int isNumber))
Additionally merging the if statements also means that you need to determine if the condition wasn't met because the parse from textbox failed or because the array was full already. If you have any questions, I'm sure the guys will advise you from here.
 
I think that @Sheepings is probably distracted by the ton of work he's got to deal with. So minor correction to the code above. Instead of using i, you'll actually want to use intScoreCount. Updated code below:
C#:
private int[] totalScoreArray = new int[20];
private int intScoreCount = 0;

private void button1_Click(object sender, EventArgs e)
{
    if (intScoreCount < 20)
        if (int.TryParse(textBox1.Text, out int isNumber))
        {
            totalScoreArray[intScoreCount] = isNumber;
            intScoreCount++;
        }
    else
        MessageBox.Show($"{textBox1.Text} is not a number, check and try again.", "Wrong value provided", MessageBoxButtons.OK);
    else
        MessageBox.Show("Empty it first", "Scoreboard full", MessageBoxButtons.OK);
}
 
Yay! Do I get a gold star?
Gold-640x400.jpg
 
I'm not surprised you didn't miss the unused variable. Eye of an eagle!

I used to do this a lot on DIC and leave the main variable out and people often overlooked it. Teaches them how to adapt the code also, and gives them something to do.

Our OP ought to be reading over the documentation for the methods/datatypes they are using so familiarise themselves better with how they work, doing so will result in you not being caught out on the second post.
 
First I converted the string from the text box to an int.
intScoreCount = Convert.ToInt32(txtScore.Text);

then I made a for loop

//I started at 0, made sure the length is less then 20 since you count from 0 then in the index 0 i added the variable that I used to convert the string to int, lastly I incremented the count variable
for(intScoreCount=0; totalScoreLength < 20)
totalScoreArray[0]=intScoreCount;
intScoreCount++;
Hey Skydiver, Sheepings, and jmcilhinney thanks for your help! I really do appreciate you guys correcting me when I say things wrong. jmcilhinney I made sure to write down your egg carton example, never thought of it like that, you made it really simple!
 
Back
Top Bottom