Stephen Daly - ASP.net/C# developer & technology enthusiast

One method to enable/disable controls instead of using two

I always try to think about how to improve my code or other people's code. One thing I notice is some people using two methods for enable and disable controls/buttons. Wouldn't it be much better to just have one method? This would mean less code and only one location to update the code.
 

Here is the old way:

    private void EnableControls()

    {

        lvMovieList.Visible = true;

        btnSearch.Enabled = true;

        btnRescan.Enabled = true;

        txtSearch.Enabled = true;

        btnTV.Enabled = true;

        pnlAdvancedSearch.Enabled = true;

        tcFindMovies.Enabled = true;

    }

    private void DisableControls()

    {

        lvMovieList.Visible = false;

        btnSearch.Enabled = false;

        btnRescan.Enabled = false;

        txtSearch.Enabled = false;

        btnTV.Enabled = false;

        pnlAdvancedSearch.Enabled = false;

        tcFindMovies.Enabled = false;

    }

 

Here is the better improved way: 

    private void ChangeControls(bool onOff)

    {

        lvMovieList.Visible = onOff;

        btnSearch.Enabled = onOff;

        btnRescan.Enabled = onOff;

        txtSearch.Enabled = onOff;

        btnTV.Enabled = onOff;

        pnlAdvancedSearch.Enabled = onOff;

        tcFindMovies.Enabled = onOff;

    }

 

So now when you want to disable controls you pass in false and pass in true when you want to enable the controls.

E.G: ChangeControls(true);

 

If you need one control set to true when disabling all other controls.

Then you can do this:

btnCancel.Enabled = onOff ? false : true;

 

I know this is pretty basic stuff but im sure some people out there will gain from reading it.


Posted by: Stephen
Posted on: 28/11/2008 at 11:09 AM
Tags: , ,
Actions: E-mail | Digg it! | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (1) | Post RSSRSS comment feed

Comments

Busby SEO Test United States

Tuesday, January 20, 2009 3:38 AM

Busby SEO Test

This code that i want.. Great method..

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading