0

I am selecting my tests from the database table, I want to create a textbox in front of every test, where the user will enter a result for each test, and be able to save results for each test . The error message i get is

"index was outsite the bounds of the array"

      int tindex=0;
                TextBox[] rst = new TextBox[tindex];

                try
                {
                    while (sub.Read())
                    {
                       
                        tindex++;
                        Label tst = new Label();
                        tst.Location = new Point(1, startingpoint);
                        tst.Name = "textBoxf";
                        tst.Size = new Size(200, 18);
                        tst.BorderStyle = BorderStyle.None;
                        tst.BackColor = SystemColors.Control;
                        tst.Text += sub.GetString("abbrev");

                       
                        try
                        {


                            rst[tindex] = new TextBox();
                            rst[tindex].Location = new Point(120, startingpoint);
                            rst[tindex].Name = "textBoxf";
                            rst[tindex].Size = new Size(70, 12);
                            rst[tindex].BorderStyle = BorderStyle.None;
                            rst[tindex].BackColor = SystemColors.Control;
                            rst[tindex].TabIndex = tindex;
                            rst[tindex].TextChanged += rst_textchanged;
                            rst[tindex].KeyDown += rst_KeyDown;
                            TextBox rsts = rst[tindex];
                           

                            panel7.Controls.Add(rst[tindex]);

                        }
                        catch(Exception er)
                        {
                            MessageBox.Show(er.Message);
                        }
}

I want a result like the one in the image. I can achieve this by just creating the textbox in a loop without the use of arrays, but it became hard to read from them the textbox since it's looping as one textbox

3
  • Hint: what's the first index of your new array that you believe you're trying to write to? Commented Dec 16, 2020 at 11:33
  • Hint 2: int tindex=0; TextBox[] rst = new TextBox[tindex];...have a look at this code. How big is the array it creates? Commented Dec 16, 2020 at 11:34
  • It isn't really clear why you need an array here anyway, since you add each textbox to the panel immediately after you've created it. Do you need the array for something else later? Commented Dec 16, 2020 at 11:37

1 Answer 1

1

You issue is here:

int tindex=0;
TextBox[] rst = new TextBox[tindex];

You created an empty array which can hold 0 entries, since tIndex is 0.

Then you even increase that number, making it 1 for the first loop

tindex++;

And then you access the array with your current index being 1. Remember, the array can still hold 0 items. That's where the exception is thrown. To translate the exception text: You array bounds are 0 and 1 is outside of these bounds.

rst[tindex] = new TextBox();

And it seems like you don't even need the rst array. At least not in your snipped. So I suggest to remove it.

And instead of:

                        rst[tindex] = new TextBox();
                        rst[tindex].Location = new Point(120, startingpoint);
                        rst[tindex].Name = "textBoxf";
                        rst[tindex].Size = new Size(70, 12);
                        rst[tindex].BorderStyle = BorderStyle.None;
                        rst[tindex].BackColor = SystemColors.Control;
                        rst[tindex].TabIndex = tindex;
                        rst[tindex].TextChanged += rst_textchanged;
                        rst[tindex].KeyDown += rst_KeyDown;
                        TextBox rsts = rst[tindex];
                       

                        panel7.Controls.Add(rst[tindex]);

you do something like:

TextBox newBox = new TextBox();
newBox.Location = new Point(120, startingpoint);
newBox.Name = "textBoxf";
newBox.Size = new Size(70, 12);
newBox.BorderStyle = BorderStyle.None;
newBox.BackColor = SystemColors.Control;
newBox.TabIndex = tindex;
newBox.TextChanged += rst_textchanged;
newBox.KeyDown += rst_KeyDown;

panel7.Controls.Add(newBox);

If you do need an array I suggest you use a List:

List<TextBox> textBoxes = new List<TextBox>();

try
{
    while (sub.Read())
    {
        Label tst = new Label();
        tst.Location = new Point(1, startingpoint);
        tst.Name = "textBoxf";
        tst.Size = new Size(200, 18);
        tst.BorderStyle = BorderStyle.None;
        tst.BackColor = SystemColors.Control;
        tst.Text += sub.GetString("abbrev");

       
        try
        {
            TextBox newBox = new TextBox();

            newBox.Location = new Point(120, startingpoint);
            newBox.Name = "textBoxf";
            newBox.Size = new Size(70, 12);
            newBox.BorderStyle = BorderStyle.None;
            newBox.BackColor = SystemColors.Control;
            newBox.TabIndex = tindex;
            newBox.TextChanged += rst_textchanged;
            newBox.KeyDown += rst_KeyDown;

            // Add the new textbox to list
            textBoxes.Add(newBox);
            
            // Also add it to the panel controls
            panel7.Controls.Add(newBox);

        }
        catch(Exception er)
        {
            MessageBox.Show(er.Message);
        }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks AdrAs. This has helped me Identify my error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.