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.
var box = (TextBox)sender;
var page = (TabPage)sender.Parent;
page.Text = box.Text;
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.