views:

186

answers:

5

Hi,

I just wanna find out if there's a way I could minimize code clutter in my application.

I have written code/s similar to this:

   private void btnNext_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnNext.Opacity = 1;
    }

    private void btnNext_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnNext.Opacity = 0.5;
    }

    private void btnShowAll_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnShowAll.Opacity = 1;
    }

    private void btnShowAll_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnShowAll.Opacity = 0.5;
    }

    private void btnPrev_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnPrev.Opacity = 1;
    }

    private void btnPrev_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnPrev.Opacity = 0.5;
    }

    private void btnSearch_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnSearch.Opacity = 1;
    }

    private void btnSearch_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnSearch.Opacity = 0.5;
    }

    private void btnSearchStore_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnSearchStore.Opacity = 1;
    }

    private void btnSearchStore_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnSearchStore.Opacity = 0.5;
    }

    private void btnCloseSearch_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnCloseSearch.Opacity = 1;
    }

    private void btnCloseSearch_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnCloseSearch.Opacity = 0.5;
    }

    private void btnHome_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnHome.Opacity = 1;
    }

    private void btnHome_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnHome.Opacity = 0.5;
    }

and so on and so forth...

Do I need to create a 'function' that will run initially? Or do I have to create another class just so I can 'organize' them?

Any suggestions?

+15  A: 

You could rewrite all those functions into 2:

private void FadeBtn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
    Button btn = (Button)sender;
    btn.Opacity = 1;
}

private void FadeBtn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
    Button btn = (Button)sender;
    btn.Opacity = 0.5;
}

And then point all of the buttons MouseEnter and MouseLeave events to those functions.

TJMonk15
+1 As long as his implementation stays this simple that would work.
amelvin
what if for example, at the same time, I've created a function that will generate buttons dynamically (let's say 5 new buttons), will they have their own MouseEnter and MouseLeave events?
eibhrum
@eibhrum If they have the same functionality then you can just register them with these two events, since they will do they same thing.
gmcalab
A: 

Create a Mouse Enter Event and register all the buttons with it. Inside the method you will notice I cast the sender object as a button. So what ever button calls it you can perform this opacity action on.

private void ButtonMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
  Button button = (Button) sender;
  button.Opacity = 1;
}
gmcalab
A: 

As far I can see, in your case you can shorten to:

private void btn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
    (sender as Button).Opacity = 1;
}

private void btn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
    (sender as Button).Opacity = 0.5;
}

In the designer, you can choose these event handlers then instead of creating new ones for each button.

herzmeister der welten
Nevermind that the original funcitonality is not preserved, the program is now much shorter ;)
Paul Ruane
Why use `as` if you're just going to fail on the `NullReferenceException` anyway? Use an explicit cast, and fail correctly on an `InvalidCastException` when `sender` isn't a `Button`.
Mark Brackett
lolz my first version was wrong due to copy-and-waste and I couldn't edit it for a while because StackOverflow is slow here today and I experience continuous time-outs :-/
herzmeister der welten
+2  A: 

You need to have ChangeButtonOpacity method:

private void ChangeButtonOpacity(Button button, double newOpacity)
{
    button.Opacity = newOpacity;
}

And you can implement your handlers as:

private void btn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
    ChangeButtonOpacity((Button)sender, 1);
}

private void btn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
    ChangeButtonOpacity((Button)sender, 0.5);
}

In this way you will need only two handlers.

Andrew Bezzub
A: 

Perhaps you can use the Tag property of the button if your not using it for anything else, Then you can do the following.

private void btn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e) 
{ 
    (sender as Button).Opacity = (double)((sender as Button).Tag); 
} 

private void btn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e) 
{ 
    (sender as Button).Opacity = 0.5;
} 

This would allow you to setup different values for different buttons using only two handlers.

galford13x
Good point, but be cautious about going down this path. I've seen it abused in Delphi - objects cast as pointers stored in tags, then cast back and used when the event fires. As yourself, "will a new developer (or yourself two years from now) be able to figure out what to set Tag to when modifying this code?"
TrueWill
That's very true. I normally use this as sort of a quick fix for quick simple applications.The better way to handle, and how I normally resolve the issue your talking about, is to inherit the object and add your own properties. This will still let you use the VS Designer in Designer mode if desired while giving customization to common components.
galford13x