0

I have a TabControl MyTabs contains some labels and textboxes , I am trying to hide/show controls in MyTabs and show/hide pages based on RadioButton check :

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    foreach (var Ctrl in MyTabs.Controls.OfType<TextBox>())
    {
        if (Ctrl.Tag.ToString() == "Products")

        {
            Ctrl.Visible = false;
            PgBom.Hide();
        }
        else
        {
            Ctrl.Visible = true;
            PgBom.Show();
        }
    }

    foreach (var Ctrl in MyTabs.Controls.OfType<Label>())
    {
        if (Ctrl.Tag.ToString() == "Products")

        {
            Ctrl.Visible = false;
            PgBom.Hide();
        }
        else
        {
            Ctrl.Visible = true;
            PgBom.Show();
        }
    }


}

PgBom is a page in MyTabs , What is wrong with my code ? . Thanks in advance.

Csharp Newbie
  • 303
  • 1
  • 10
  • _PgBom is a page in MyTabs...._. if you mean `TabPage`, then you can't `.Show()` & `.Hide()` one. You need to add/remove it into and from the `TabPages` collection to show/hide it. See [How to hide TabPage from TabControl](https://stackoverflow.com/questions/552579/how-to-hide-tabpage-from-tabcontrol) and the duplicate post. – dr.null Apr 17 '22 at 02:59

1 Answers1

1

I am pretty sure that the Controls in a TabControl will be TabPages. Controls like labels and text boxes would go into a specific TabPage. Therefore, to get the controls, then you need to loop through all the different TabPages then look at each TabPages Controls collection as shown below.

Also, setting the variable to… PgBom.Show(); and PgBom.Hide(); is questionable in sense that as you loop through the controls it may get hid and un-hid many times over. What ends up being displayed will depend on what the LAST checked control’s state was.

foreach (TabPage tp in MyTabs.TabPages) {
  foreach (var Ctrl in tp.Controls.OfType<TextBox>()) {
    if (Ctrl.Tag != null) {
      if (Ctrl.Tag.ToString() == "Products") {
        Ctrl.Visible = false;
        PgBom.Hide();
      }
      else {
        Ctrl.Visible = true;
        PgBom.Show();
      }
    }
  }
  foreach (var Ctrl in tp.Controls.OfType<Label>()) {
    if (Ctrl.Tag != null) {
      if (Ctrl.Tag.ToString() == "Products") {
        Ctrl.Visible = false;
        PgBom.Hide();
      }
      else {
        Ctrl.Visible = true;
        PgBom.Show();
      }
    }
  }
}
JohnG
  • 9,259
  • 2
  • 20
  • 29
  • Thanks for reply, I will try your way in hiding controls in tabpages, About hiding the page PgBom could we do it outside the loop? When the radio button clicked it hides the PgBom. – Csharp Newbie Apr 17 '22 at 10:56
  • Well it is somewhat unclear what exactly you are trying to do with… `PgBom.Hide()` … ? … Are you trying to hide the “whole” tab page or are you wanting to hide all the Controls on that tab page. I am pretty sure you can NOT “Hide” a `TabPage`, if you want to hide a tab page then you will have to remove it from the `TabControls` `TabPages` collection. – JohnG Apr 17 '22 at 22:02
  • In addition… if you are wanting to hide all the controls on the tab page by calling `PgBom.Hide()`, then, in that case you should be aware that in order for the controls to get hidden… the tab Page MUST be displayed. If the tab page `PgBom` is NOT displayed when the code calls `PgBom.Hide()`… then, the controls will not get hidden. – JohnG Apr 17 '22 at 22:03