Solved

Two dimensional array of textboxes

Posted on 2006-06-27
7
323 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
  • 4
  • 2
7 Comments
 
LVL 13

Expert Comment

by:devsolns
Comment Utility
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
Comment Utility

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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 13

Expert Comment

by:devsolns
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
are you kidding me, he took my answer and pasted it in code.  wonderful.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now