1

How can I get the text from a dynamicly created RichTextBox and a dynamicly created rtb_TextChanged Event?

e.g:

    private void button1_Click(object sender, EventArgs e)
    {
        RichTextBox rtb = new RichTextBox();
        rtb.Name = "rtb" + i;
        rtb.Dock = DockStyle.Fill;

        rtb.TextChanged += rtb_TextChanged;

        Controls.Add(rtb);

    }

    void rtb_TextChanged(object sender, EventArgs e)
    {
        //string s = rtb.Text;    //How can I get the rtb.Text?
    }
3

3 Answers 3

3

You need to use the sender argument of your event handler:

void rtb_TextChanged(object sender, EventArgs e)
{
    RichTextBox rtb = (RichTextBox)sender;
    string s = rtb.Text;
    //... etc
}
Sign up to request clarification or add additional context in comments.

Comments

2

You just need to use the event parameter : sender

private void richTextBox1_TextChanged(object sender, EventArgs e)
{  
       RichTextBox rtb = (RichTextBox)sender;
       var str = rtb .Text;
}

Comments

0

First rtb is not the name that you called the textbox. Since the textbox sent the message you could cast the sender to a textbox and look at its text property.

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.