Link to home
Start Free TrialLog in
Avatar of pai_prasad
pai_prasadFlag for United States of America

asked on

Two dimensional array of textboxes

Hi,

I want to have a 2dimensional array of textboxes ...
I wrote this code..( learning C# so pls be patient)


                                                for (int x = 0; x<8; x++)
                  for (int i = 0; i<8; i++)
                  {
                         
                        System.Windows.Forms.TextBox[][]tb ;
----------------->            tb[x][i]=  new System.Windows.Forms.TextBox();
                        tb[x][i].Text = "";
                        tb[x][i].TabIndex = i;
                        tb[x][i].Size = new System.Drawing.Size(20,20);
                        tb[x][i].Location = new System.Drawing.Point(x*70, i*25);
                        tb[x][i].Text = i.ToString();
                        this.Controls.Add(tb[x][i]);
                  }

ERROR: Use of unassigned local variable 'tb'

How do i solve this
Avatar of devsolns
devsolns

System.Windows.Forms.TextBox[][]tb ;   <---just a reference to nothing.

need to make,
int size = 50;
System.Windows.Forms.TextBox[][]tb  = new System.Windows.Forms.TextBox[size][];

Alternatives as well,

                //Framework 1.1
                ArrayList list = new ArrayList();
                list.Add(new System.Windows.Forms.TextBox());

                //Framework 2.0
                System.Collections.Generic.List<System.Windows.Forms.TextBox> = new List<System.Windows.Forms.TextBox>();
                list.Add(new System.Windows.Forms.TextBox());


I would still learn to use arrays though.  They are a critical element in comptuer science.
Avatar of pai_prasad

ASKER

i tried that ...
System.Windows.Forms.TextBox[][]tb = new TextBox[8][];


it gives error: Object reference not set to an instance of an object. on

tb[x][i].Text = "";

see your not using a 2 dimensional array, your using an array of an array

int size = 50;
System.Windows.Forms.TextBox[][]tb  = new System.Windows.Forms.TextBox[size][];

int size2 = 2;
tb[0] = new System.Windows.Forms.TextBox[size2];


2d array looks like System.Windows.Forms.TextBox[,] tb = new System.Windows.Forms.TextBox[,] {{new System.Windows.Forms.TextBox(), new System.Windows.Forms.TextBox()}, {new System.Windows.Forms.TextBox(), new System.Windows.Forms.TextBox()}}

ASKER CERTIFIED SOLUTION
Avatar of AdGroot
AdGroot

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for the answer,,

but how do i add a text_changed event listener and retrieve the value
any links for that?
are you kidding me, he took my answer and pasted it in code.  wonderful.