Solved

Two dimensional array of textboxes

Posted on 2006-06-27
7
335 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:pai_prasad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 13

Expert Comment

by:devsolns
ID: 16994675
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][];
0
 
LVL 13

Expert Comment

by:devsolns
ID: 16994703

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.
0
 
LVL 10

Author Comment

by:pai_prasad
ID: 16994713
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 = "";
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 13

Expert Comment

by:devsolns
ID: 16994963

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()}}

0
 
LVL 5

Accepted Solution

by:
AdGroot earned 500 total points
ID: 16994977
try this

            System.Windows.Forms.TextBox[][] tb = new TextBox[8][];
            for (int x = 0; x < 8; x++)
            {
                tb[x] = new TextBox[8];
                for (int i = 0; i < 8; i++)
                {

                    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]);
                }
            }


Ad
0
 
LVL 10

Author Comment

by:pai_prasad
ID: 16995740
thanks for the answer,,

but how do i add a text_changed event listener and retrieve the value
any links for that?
0
 
LVL 13

Expert Comment

by:devsolns
ID: 16996218
are you kidding me, he took my answer and pasted it in code.  wonderful.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question