I have a TabControl
in my app. Based on the selected checkboxes, I create a TabItem
at runtime. When a checkbox is unchecked, that TabItem
must be removed. Now, my problem is, I need just to remove only that TabItem
.
I was implementing this, and created every TabItem
again. But as I said, I lose all the info of all tabs when a checkbox is checked or unchecked:
tc_Descuento.TabPages.Clear();
I was a reading some posts and tried this:
TabPage tb = new TabPage();
tb = tc_Descuento.Controls.Find("Cuota-" + Convert.ToString(cuota.num_cuota));
tc_Descuento.TabPages.Remove(tb);
The name of the TabItem is : "Cuota-" + Convert.ToString(cuota.num_cuota)
What I tried is to create a TabItem
, then find it in the TabControl
by its name. And then remove it from the list of TabPages
in the TabControl
.
I´m getting a compilation error. Surely, its because I cannot convert a Control item into a TabPage. But then, how can I get that TabItem control as a TabItem and not a control?
Thanks..
EDIT:
tb = Convert.ChangeType(tc_Descuento.Controls.Find("Cuota-" + Convert.ToString(cuota.num_cuota), false).First(), TabItem);
I tried this now, I need a way to convert the control to a TabItem.. But this is not compilating either :/
Already found my answer!
This is the way to do it:
tb = (TabPage)(tc_Descuento.Controls.Find("Cuota-" + Convert.ToString(cuota.num_cuota), false).First());
You have to cast the type at the beginning.
Hope this can help somebody out!