Alex Kibler
Alex Kibler

Reputation: 4954

How can I return text from a form to a textbox C#?

In my C# program, I've got a bunch of automatically generated textboxes. On those textboxes, I add a click event that launches a form with three dropdown boxes. After the user selects a value for each box, they click OK and the value is to be placed in the textbox.

As of right now, the click event fires just fine and the dialog comes up. After I click OK, however, the data is never put in the textbox. Here is my click event.

private void clickTextBox(object sender, EventArgs e)
        {
            //MessageBox.Show(connectString + " " + unit);
            frmPickOven f = new frmPickOven(connectString, unit);
            if (f.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            else
            {
                this.Text = "";
                Console.WriteLine("Before: " + this.Text);
                this.Text = f.Oven;
                Console.WriteLine("After: " + this.Text);
            }
        }

The Before: is always blank.

The After: shows the new value, but the value isn't written to the textbox.

I just realized that the value is being written to the title of the form. I thought this would refer to whatever called the function, but apparently not. How can I set the value of the textbox that called the function?

Upvotes: 0

Views: 732

Answers (2)

Alireza
Alireza

Reputation: 5056

this.Text here refers to title of the Form containing the TextBox. You should change it to

((TextBox) sender).Text = f.Oven;

Upvotes: 2

FallenAvatar
FallenAvatar

Reputation: 4079

The sender argument to the function is the control that raised the event.

((TextBox)sender).Text = ...

Upvotes: 3

Related Questions