0

I have problem like this: Have some function where I dynamically create TabPage object and textBox control on it.

   private void Create()
   {
        TabPage zakladkaTabControl = createTabPage();
        TextBox TB = new TextBox();

        TB.TextChanged += new EventHandler(TB_TextChanged);
    }

Now I need to change TabPage name dynamically when I write something in my TextBox control. I have function which supports changing content of the TextBox control:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        ((TabPage)sender).Text = ((TextBox)sender).Text;
    }

It doesn't work because function calling only to the TextBox object and not the TextBox and TabPage. I know a solution for objects created statically, but dynamically? For several hours I can not find a solution.

Any help would be most appreciated.

1
  • you are registering TB_TextChanged and showing code for textBox1_TextChanged method. Is there a mismatch? Soln of Hans should work but I'm not sure exactly what is the question? Dec 28, 2010 at 19:13

2 Answers 2

2
var box = (TextBox)sender;
var page = (TabPage)sender.Parent;
page.Text = box.Text;
0
1

To get the parent TabPage, you can walk up the control hierarchy until you find it:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    var tb = (TextBox)sender;
    Control ctl = tb.Parent;
    while (ctl != null && !(ctl is TabPage))
    {
        ctl = ctl.Parent;
    }

    if (parent != null)
    {
        var tp = (TabPage)parent;
        // Change the TabPage name here
    }
}

Alternatively, you could make zakladkaTabControl a property of the class rather than a local variable so that you can refer to it from the textBox1_TextChanged method.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.