简体   繁体   中英

overloading button in visual studio c#.net

I have a 54 buttons a flat style = flat, and I manually designed each one of them with an image on mouse hover and mouse leave w/c is all of them has the same design. What should I do to make it more easier than doing the code of mousehover and mouseleave each button.

Since you haven't specified, I'll assume winforms.

So you've got 54 buttons and you've manually code up presumably 108 methods (54 for mouse enter & 54 for mouse leave)

What you could do is create an object to hold button->image info, and store it all in a dictionary. Key it by button Id. ie

public class ButtonImageInfo 
{  
     public string ButtonId {get;set;}
     public string MouseEnterImage {get;set;}
     public string MouseLeaveImage {get;set;}
}

// ...
public Dictionary<string, ButtonImageInfo> _dict = new ...
_dict.Add("Button1", new ButtonImageInfo ("Button1", "Image1Enter", "Image1Leave"));
///    ... etc...

That would allow you to code up one OnMouseEnter & one OnMouseLeave methods and apply them to every button.

You would then just grab the correct image based on the sender Id.

private void MouseLeaveEvent(object sender, EventArgs e)
  {
     Button b = sender as Button;
     if(b != null)
     { 
          string id = b.Id;
          //Do Something with _dict[id].MouseLeaveImage
     }
  }

  private void MouseEnterEvent(object sender, EventArgs e)
  {
     Button b = sender as Button;
     if(b != null)
     { 
          string id = b.Id;
          //Do Something with _dict[id].MouseEnterImage
     }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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