Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am selecting my tests from the database table, i want to create a textbox in front every test, where the user will enter 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"

What I have tried:

C#
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);
                    }
}
Posted
Updated 16-Dec-20 2:18am
v2
Comments
The Other John Ingram 16-Dec-20 6:56am    
can you use a list<>

C#
int tindex=0;
TextBox[] rst = new TextBox[tindex];
That declares an array which can hold precisely zero elements.
C#
tindex++;
rst[tindex] = new TextBox();
On the first iteration, that increases the index to 1 and then tries to set the second element of your zero-element array to a value.

Unsurprisingly, a zero element array does not have a second element. It doesn't even have a first element. So when you try to update the second element, you get an "index out of range" exception.

As suggested in the comments, use a List<TextBox> instead:
C#
List<TextBox> rst = new List<TextBox>();
while (sub.Read())
{
    TextBox txt = new TextBox();
    ...
    rst.Add(txt);
    panel7.Controls.Add(txt);
}
 
Share this answer
 
Comments
CHill60 16-Dec-20 8:18am    
Snap! 5'd
Maciej Los 16-Dec-20 8:18am    
5ed!
TheRealSteveJudge 16-Dec-20 10:30am    
5*
BillWoodruff 16-Dec-20 10:46am    
+5 bullseye !
Debug your code ...
C#
int tindex=0;
TextBox[] rst = new TextBox[tindex];
rst has only one element at index 0. Edit: My bad see Solution 1 "That declares an array which can hold precisely zero elements".

Then you do
C#
while (sub.Read())
{
         tindex++;

         // ...
         try
         {
             rst[tindex] = new TextBox();
tindex now has a value of 1 but rst[1] does not exist.
Arrays are only good for when you know how big they are going to be, and often not even then. As @User-14699738 suggests, a List would be better in this case. See this article When to Use Generic Collections | Microsoft Docs[^]
e.g. (not tested or compiled and very simplistic)
C#
List<TextBox> rst = new List<TextBox>();
// ...
TextBox t = new TextBox();
t.Location = new Point(120, startingpoint);
t.Name = string.Format("textBoxf{0}", tindex);
//...
rst.Add(t);
// ...
panel7.Controls.Add(t);
In fact, given that you are adding each of the textboxes to the panel7 control, why not just use that Control collection. There appears to be no need for the array (or List) in the code you have shared
 
Share this answer
 
v2
Comments
Maciej Los 16-Dec-20 8:19am    
5ed!
Richard Deeming 16-Dec-20 8:20am    
"rst has only one element at index 0."
Actually, it doesn't have any elements. :)
CHill60 16-Dec-20 9:15am    
D'oh - I've been doing some VBA all day. Brain frazzled!
TheRealSteveJudge 16-Dec-20 10:30am    
5*
BillWoodruff 16-Dec-20 10:47am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900