steamrolla
steamrolla

Reputation: 2491

TextBox.Text returns nothing

I have a form with a tab control on it, and on one of these tabs, I have a ComboBox. Depending on the value the user selects in this ComboBox, different controls need to populate. This is working fine, however, when I attempt to retrieve the text the user has put into a TextBox control that I have populating, TextBox.Text returns nothing to me. TextBox.Text works fine when I add a TextBox to the same form, but include it in the form initialization (rather than populating it on the form later with the method below), which makes me think I am missing a property on the control.

Snippet from the method I'm using to populate the TextBox and other controls onto the tab control:

private System.Windows.Forms.TextBox filePathBox;

private void populateControls(string someText)
    {
        if (someText == "Something")
        {
            // 
            // TextBox
            //
            this.filePathBox.Location = new System.Drawing.Point(6, 61);
            this.filePathBox.Name = "filePathBox";
            this.filePathBox.Size = new System.Drawing.Size(220, 20);

            this.tabPage1.Controls.Add(this.filePathBox);
            this.filePathBox.Show();
        }
        else if (someText == "SomethingElse")
        {
            //populate other controls.
        }
    }

And, to test, I have a button that simply displays a MessageBox of the string that is in the TextBox, which results in nothing.

private void button2_Click(object sender, EventArgs e)
    {
        MessageBox.Show(filePathBox.Text);
    }

Again, it makes me think I am missing some properties from the TextBox, but anything would be appreciated at this point.

Upvotes: 1

Views: 3207

Answers (3)

rfresia
rfresia

Reputation: 556

I created a quick sample and saw no issues. Make sure your constructor calls InitializeComponents,hope this helps

 private System.Windows.Forms.TextBox filePathBox = new TextBox();

    public Form1()
    {
        InitializeComponent();
        PopulateControls("Something");
    }

    public void PopulateControls(string someText)
    {
        if (someText == "Something")
        {

            this.filePathBox.Location = new System.Drawing.Point(6, 61);
            this.filePathBox.Name = "filePathBox";
            this.filePathBox.Size = new System.Drawing.Size(220, 20);

            this.tabPage1.Controls.Add(this.filePathBox);
            this.filePathBox.Show();
        }

    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (filePathBox != null)
        {
            MessageBox.Show(filePathBox.Text);
        }
    }

Upvotes: 0

Bolu
Bolu

Reputation: 8786

change your:

this.filePathBox = new TextBox();

to:

if(this.filePathBox==null)
{
    this.filePathBox = new TextBox();  
}

Upvotes: 1

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23675

I suppose you correctly initialized filePathBox in your InitializeComponents() (form designer content) so... the filePathBox.Text will be initially empty. You have to fill it with content before it shows something... like this:

filePathBox.Text = "something";
MessageBox.Show(filePathBox.Text);

Upvotes: 0

Related Questions