Can someone please tell me what is wrong with the following code or if it is even legal? I am trying to access the text in a multitude of textboxes without having to go to each textbox manually. so i named them all similar enough. their names are all variations of
txtSG1D1
with only the numbers varying. the problem here is that s2 cannot be equal to the text of curText. I get an error message that seems to imply that curText is not a real object. I must create an instance by using keyword new.

Or is there some other way of creating an array of controls. I really do not want to resign myself to the fate of having to code 30 textboxes by hand.

Thanks.

TextBox curText = newTextBox();
curText = (TextBox) this.Controls["txtSG" + x.ToString() + "D" + y.ToString()];
 
s2 = curText.Text;

Yes you can create an array of textboxes.Look at the code below.

public partial class Form1 : Form
    {
        TextBox[] txt = new TextBox[3]; //Creating an array of textbox //references NOT an array of textboxes.
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            

            for(int i = 0; i < txt.Length; i++)
            {
                txt[i] = new TextBox(); //Each textbox reference must show a //textbox...
                
                this.Controls.Add(txt[i]); //Add the current textbox to the //form.
                
            }

            
        }

Now why we wrote txt = new TextBox(); ???
Because when you initialize the array of textbox references , each reference shows "null".So when you write txt.Text = "something" without write txt = new TextBox(); it will cause an error(Null reference exception).

public partial class Form1 : Form
    {
        TextBox[] txt = new TextBox[3];
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txt[0].Text = "something"; //These assignments will cause Null //Reference Exception because each of reference shows "null";
            txt[1].Text = "something else";
            .......
        }
    }

Note that all of the textboxes have the same location on the form when they were added to the form.You must change their location with the code below :

txt[0].Location = new Point(45,89);
txt[1].Location = new Point(34,88);
......

But this will cause a little mess for 30 textboxes...:P
If the textbox positions are proportional you may solve this by using a loop...

If I understand you right, you have some text controls on the form, and you want to iterate through each to look at their text value.

Since all of them are already in the controls array, you can just iterate through them. You can rely on the name, or set the Tag value of those you want to search (from the designer IDE).

Your first line of TextBox curText = new TextBox() should really be set to null, unless you are really trying to create a new control on the form.

As a sample, I created a new project, dropped three TextBox controls and a button. In the second control I set the Text property to "Hello World" then used this code in the button handler:

foreach (Control ctrl in this.Controls)
{
if (ctrl is TextBox)
{
if (ctrl.Text == "Hello World")
{
MessageBox.Show(ctrl.Name + " has this value.");
break;
}
}
}

The results showed that TextBox2 contains the text I am looking for.

Hope this helps,
Jerry

Yes you can create an array of textboxes.Look at the code below.

public partial class Form1 : Form
    {
        TextBox[] txt = new TextBox[3]; //Creating an array of textbox //references NOT an array of textboxes.
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            

            for(int i = 0; i < txt.Length; i++)
            {
                txt[i] = new TextBox(); //Each textbox reference must show a //textbox...
                
                this.Controls.Add(txt[i]); //Add the current textbox to the //form.
                
            }

            
        }

Now why we wrote txt = new TextBox(); ???
Because when you initialize the array of textbox references , each reference shows "null".So when you write txt.Text = "something" without write txt = new TextBox(); it will cause an error(Null reference exception).

public partial class Form1 : Form
    {
        TextBox[] txt = new TextBox[3];
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txt[0].Text = "something"; //These assignments will cause Null //Reference Exception because each of reference shows "null";
            txt[1].Text = "something else";
            .......
        }
    }

Note that all of the textboxes have the same location on the form when they were added to the form.You must change their location with the code below :

txt[0].Location = new Point(45,89);
txt[1].Location = new Point(34,88);
......

But this will cause a little mess for 30 textboxes...:P
If the textbox positions are proportional you may solve this by using a loop...

thank you. it was most helpfull. there still is some issues but they involve logic and i shal be able to deal with them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.