Solved

Two dimensional array of textboxes

Posted on 2006-06-27
7
326 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
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exit the loop 4 42
Media.Imaging 1 18
Set value of dynamically created checkboxlist 2 33
Following an example - Why do I need to click Save button twice? 3 25
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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