Question

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.
    }
Was it helpful?

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.

OTHER TIPS

Why not just do it client-side using jQuery? Seems from your example that there is no reason to do this on the server side. Doing it client side allows you dynamic control and won't require posting back to the server just to show or hide controls. You can even get some nice effects easily. You can just modify a class that you attach to all the controls. I put together a sample that I think is pretty self-explanatory below. It allows you to show and hide any item you tag with the "beverage" class with just a short, simple function.
http://jsfiddle.net/dMJds/3/

and here's the code:

<script>
    function hideBeverages() {
        $('.beverage').fadeOut(2000);
    };

    function showBeverages() {
        $('.beverage').fadeIn(2000);
    };
</script>

<form>
    <button id="hideBevs" type="button" onclick="hideBeverages()">Hide Beverages</button>
    <button id="showBevs" type="button" onclick="showBeverages()">Show Beverages</button>

    <hr />
    <div class="beverage">
        <h4>Beverage Order</h4>
        <input id="qtyMilk" type="text" /><label for="qtyMilk">Milk</label><br />
        <input id="qtyCoffee" type="text" /><label for="qtyMilk">Coffee</label>
    </div>

    <div>
        <h4>Foods</h4>
        <ul>
            <li class="beverage">Tea</li>
            <li class="produce">Apples</li>
            <li class="beverage">Water</li>
            <li class="produce">Oranges</li>
        </ul>
    </div>
</form>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top