Search code examples
c#classcontrolsvisible

How to make a class to hide your controls in your form in c#


I have a button named btnBeverage, 3 numericupdown, 3 combobox and 3 textboxes. Now, I made a class to make the controls visible or not. e.g., If i click btnBeverage, the 3 numericupdown, 3 combobox and 3 textboxes should be shown/hidden. But when i put my codes in class like, numericupdown.Show(), it does not recognize any control from my form. how do i connect it?

CLASS :

    public void visibile()
    {
         numericupdown.Show(); //this has error because it does not recognize any numericupdown control.
    }

Solution

  • You're creating a whole class just to show/hide some controls? You can use these methods instead:

        public static void ShowControl(Control c)
        {
            c.Show();
        }
    
        public static void HideControl(Control c)
        {
            c.Hide();
        }
    

    Or, if you want to put it in a whole new class (which I don't recommend)

    public static class ControlsUtility
    {
        public static void ShowControl(Control c)
        {
            c.Show();
        }
    
        public static void HideControl(Control c)
        {
            c.Hide();
        }
    }
    

    Hope that helped.