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);
    }
        // }
    // }
}
3
  • 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. Commented Jan 26, 2016 at 6:22
  • I have done it. All the textbox name property is set to " Textbox00,Textbox01......" Commented Jan 26, 2016 at 6: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(); Commented Jan 26, 2016 at 6:41

5 Answers 5

1

Control.ControlCollection.Find Method

TextBox t = this.Controls.Find(textboxname, true).FirstOrDefault() as TextBox;
Sign up to request clarification or add additional context in comments.

Comments

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");

Comments

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";
  }
}

Comments

0

Are you accessing textbox in separate thread? Try this:

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

Hope helps.

Comments

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.