0

I am trying to access a Textbox in my form by casting a string equivalent to Textbox name in my form but i get error stating "Object reference not set to an instance of object"

private void writetotexboxarray()
{
    // for (int i = 0; i < 9; i++)
    //{
        //for (int j = 0; j < 4; j++)
        //{
    textboxname= "Textbox" + 0 + 0;
    MessageBox.Show(textboxname);
    TextBox t = new TextBox();

    t = (TextBox)(this.Controls[textboxname]);

    //readintakedata[0,0].Text = t.Text;
    try
    {
        string value = t.Text;
    }
    catch (Exception exp)
    {
        MessageBox.Show(exp.Message);
    }
        // }
    // }
}
Rob
  • 26,989
  • 16
  • 82
  • 98
KamalSubodh
  • 51
  • 2
  • 10
  • If you are trying to get the newly created TextBox in given code then you need to assign the name to its property and add it to Form object before you try to get it. – Adil Jan 26 '16 at 06:22
  • I have done it. All the textbox name property is set to " Textbox00,Textbox01......" – KamalSubodh Jan 26 '16 at 06:24
  • Just note: If you want the variable for setting TextBox type you should declare it like: TextBox t; instead of creating new object of TextBox(), TextBox t = new TextBox(); – Jamaxack Jan 26 '16 at 06:41

5 Answers5

1

It's because this.Controls only returns immediate children of the current control. If the textbox is within a panel, this.Controls (assuming this is the form) would not find the textbox. You need to do something like this:

(Modified version of this answer)

public IEnumerable<Control> GetAll(Control control)
{
    var controls = control.Controls.Cast<Control>();

    return controls.SelectMany(ctrl => GetAll(ctrl)).Concat(controls);
}

Then you would write:

var t = GetAll(this).OfType<TextBox>().FirstOrDefault(c => c.Name == "Textbox00");
Community
  • 1
  • 1
Rob
  • 26,989
  • 16
  • 82
  • 98
1

Control.ControlCollection.Find Method

TextBox t = this.Controls.Find(textboxname, true).FirstOrDefault() as TextBox;
Ghasem
  • 14,455
  • 21
  • 138
  • 171
0

This might help you to create the TextBox Array.

var arr = new TextBox[10];
for (var i = 0; i < 10; i++)
{
    var tbox = new TextBox();
    tbox.Text = i.ToString();
    // Other properties sets for tbox
    this.Controls.Add(tbox);
    arr[i] = tbox;
}

You can also get all the TextBox Controls like this

foreach (Control x in this.Controls)
{
  if (x is TextBox)
  {
    ((TextBox)x).Text = "Whatever you wanted here";
  }
}
Mohit S
  • 13,723
  • 6
  • 34
  • 69
0

Are you accessing textbox in separate thread? Try this:

this.Invoke((MethodInvoker)delegate() { this.Controls["Textbox00"] = "YourVal"; });

Hope helps.

Jamaxack
  • 2,400
  • 2
  • 24
  • 42
0

I would suggest using a recursive function to search all controls and their children for the one you need:

private TextBox GetTextBoxByName(string name)
{
    foreach (Control control in Controls)
    {
        if (control.Name == name)
        {
            if (control is TextBox)
            {
                return (TextBox)control;
            }
            else
            {
                return null;
            }
        }
        if (control.HasChildren)
        {
            return GetTextBoxByName(name);
        }
    }
    return null;
}

Then, you could do this:

TextBox t = GetTextBoxByName(textboxname);
if (t != null)
{
    string value = t.Text;

    // do stuff
}
Tim
  • 857
  • 6
  • 13