?
Solved

Two dimensional array of textboxes

Posted on 2006-06-27
7
Medium Priority
?
342 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

762 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