简体   繁体   English

C#缩短重复代码的更好方法

[英]C# Better way to shorten repeated code

the following code allows me to have a hover over image on a button but requires a load of repeated code. 以下代码使我可以将鼠标悬停在按钮上的图像上,但需要加载大量重复代码。 I was wondering if there is a way to shorten down and allow the same piece of code to be used but with a different button name. 我想知道是否有一种方法可以缩短并允许使用相同的代码段,但使用不同的按钮名称。

I know the private void *NAME* can be the same, used by different buttons and allows me to shorten the following but then leaves me with the hover over being on all the buttons at once rather than individually. 我知道private void *NAME*可以相同,可以由不同的按钮使用,并允许我缩短以下内容,但随后将鼠标悬停在所有按钮上而不是单个上。

private void button1_Leave(object sender, EventArgs e)
    {
        this.button1.BackgroundImage = ((Image)(Properties.Resources.leave_img));
    }

    private void btnfb_Leave(object sender, EventArgs e)
    {
        this.btnfb.BackgroundImage = ((Image)(Properties.Resources.leave_img));
    }

    void btnfb_MouseMove(object sender, MouseEventArgs e)
    {
        this.btnfb.BackgroundImage = ((Image)(Properties.Resources.hover_img));
    }

    private void btndiscord_Leave(object sender, EventArgs e)
    {
        this.btndiscord.BackgroundImage = ((Image)(Properties.Resources.leave_img));
    }

    void btndiscord_MouseMove(object sender, MouseEventArgs e)
    {
        this.btndiscord.BackgroundImage = ((Image)(Properties.Resources.hover_img));
    }

    private void btn_pp_Leave(object sender, EventArgs e)
    {
        this.btn_pp.BackgroundImage = ((Image)(Properties.Resources.leave_img));
    }

    void btn_pp_MouseMove(object sender, MouseEventArgs e)
    {
        this.btn_pp.BackgroundImage = ((Image)(Properties.Resources.hover_img));
    }

    private void btnhelp_Leave(object sender, EventArgs e)
    {
        this.btnhelp.BackgroundImage = ((Image)(Properties.Resources.leave_img));
    }

    void btnhelp_MouseMove(object sender, MouseEventArgs e)
    {
        this.btnhelp.BackgroundImage = ((Image)(Properties.Resources.hover_img));
    }

    private void btnsave_Leave(object sender, EventArgs e)
    {
        this.btnsave.BackgroundImage = ((Image)(Properties.Resources.leave_img));
    }

    void btnsave_MouseMove(object sender, MouseEventArgs e)
    {
        this.btnsave.BackgroundImage = ((Image)(Properties.Resources.hover_img));
    }

    private void btnmini_Leave(object sender, EventArgs e)
    {
        this.btnmini.BackgroundImage = ((Image)(Properties.Resources.leave_img));
    }

You can wire up all your event to the same handler ... 您可以将所有事件连接到同一个处理程序...

private void MyButtonLeaveHandler(object sender, EventArgs e)
{
 Button button = sender as Button;
 if (button != null)
 { 
     button.BackgroundImage = ((Image)(Properties.Resources.leave_img));
 }
}

Saying

this.button1.Leave += MyButtonLeaveHandler;
this.btnfb.Leave += MyButtonLeaveHandler;

Similarly you can create abother common handler for MouseLeave event 同样,您可以为MouseLeave事件创建另一个通用处理程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM