elvzhpqlrl
elvzhpqlrl

Reputation: 71

Get Text from Textbox in textbox_Leave Method

this is my Code for adding Textboxes dynamically. Now I need to get the text from the Textbox in the textbox_leave Function for a save function. Also I need other properties from textbox. How can I get those?

Label makeLabel = new Label();
            makeLabel.Width = 120;
            makeLabel.Height = 21;
            makeLabel.Location = new Point(20, 60 + 2 * z * makeLabel.Height);
            makeLabel.Name = e.Node.Text;
            makeLabel.Text = e.Node.Nodes[z].Text;
            this.Controls.Add(makeLabel);
            panel1.Controls.Add(makeLabel);

            TextBox textbox = new TextBox();
            textbox.Width = 400;
            textbox.Height = 15;
            textbox.Location = new Point(140, makeLabel.Location.Y-5);
            textbox.Name = e.Node.Text + "lbl";
            textbox.Text = service.oldDescription(e.Node.Text, e.Node.Nodes[z].Text);
            textbox.Leave += new System.EventHandler(this.textbox_Leave);
            this.Controls.Add(textbox);
            panel1.Controls.Add(textbox);


        }           
    }

    private void textbox_Leave(object sender, EventArgs e)
    {
        string textboxtext= 
        MessageBox.Show(textboxtext);
    }

Upvotes: 0

Views: 674

Answers (1)

Pawel Maga
Pawel Maga

Reputation: 5787

You can use the parameter: sender. Then cast it to the appropriate object. Then you have access to object which invoke this event.

private void textbox_Leave(object sender, EventArgs e)
{
    var textbox = sender as TextBox;

    if (textbox != null)
    {
        string textboxtext = textbox.Text;
        MessageBox.Show(textboxtext);
    }
}

Upvotes: 4

Related Questions