0

I'm pretty new here on Stack Overflow and this is my first time asking a question so please be kind to me if you find this question stupid or anything.

Does anyone know how can I make these codes short, I mean I would like to put all these codes into one line.. Please see the code below.

Private Sub PB_SearchP_MouseHover(sender As Object, e As EventArgs) Handles PB_SearchP.MouseHover PB_SearchP.Image = My.Resources.search1 End Sub Private Sub PB_SearchP_MouseLeave(sender As Object, e As EventArgs) Handles PB_SearchP.MouseLeave PB_SearchP.Image = My.Resources.search End Sub Private Sub PB_AddP_MouseHover(sender As Object, e As EventArgs) Handles PB_AddP.MouseHover PB_AddP.Image = My.Resources.add_1_iconhover End Sub Private Sub PB_AddP_MouseLeave(sender As Object, e As EventArgs) Handles PB_AddP.MouseLeave PB_AddP.Image = My.Resources.add_1_icon End Sub Private Sub PB_New_MouseHover(sender As Object, e As EventArgs) Handles PB_New.MouseHover PB_New.Image = My.Resources.newhover End Sub Private Sub PB_New_MouseLeave(sender As Object, e As EventArgs) Handles PB_New.MouseLeave PB_New.Image = My.Resources.neww End Sub Private Sub PB_Save_MouseHover(sender As Object, e As EventArgs) Handles Btn_Save.MouseHover Btn_Save.Image = My.Resources.savehover End Sub Private Sub PB_Save_MouseLeave(sender As Object, e As EventArgs) Handles Btn_Save.MouseLeave Btn_Save.Image = My.Resources.save End Sub Private Sub PB_Update_MouseHover(sender As Object, e As EventArgs) Handles BTN_QUpdate.MouseHover BTN_QUpdate.Image = My.Resources.edithover End Sub Private Sub PB_Update_MouseLeave(sender As Object, e As EventArgs) Handles BTN_QUpdate.MouseLeave BTN_QUpdate.Image = My.Resources.edit End Sub 
6
  • Derive your own class from Button and override the OnMouseXxx() methods. So you don't need any of these event handlers. Commented Apr 6, 2016 at 9:25
  • @HansPassant Deriving from Button seems overkill - OP only seems interested in automating Image property. Commented Apr 6, 2016 at 9:44
  • Strange that programmers find this 'scary' or overkill. Inheritance is crucial in winforms to use the class library effectively. Not having to write an event handler is a feature, not overkill, and very well supported. If you don't like inheritance then WPF is the better alternative. Commented Apr 6, 2016 at 9:50
  • @HansPassant Even by subclassing Button to add image-swap-on-hover, each button subclass instance still needs to have the button images manually set on it - granted, the winforms designer could speed this up, but if you have to manually mutate each button you might as well just use event-handlers - especially because subclassing GUI components generally comes with caveats regarding correct override implementation (e.g. exactly when to call the parent implementation) which might break future-compatibility (e.g. OwnerDraw hell in WinForms) Commented Apr 6, 2016 at 9:53
  • Assigning the Image property requires code, key is that you only write it once. Writing a bunch of AddHandler statements is not once. There are no caveats, just a need to think about it a little bit. It will only ever be a caveat if you never had to think about it before. Commented Apr 6, 2016 at 10:03

1 Answer 1

1

A simpler solution is to use a function that sets-up lamba functions as event-handlers:

Public Shared Sub SetUpButton(btn As Button, normalImage As Image, hoverImage as Image) AddHandler btn.MouseLeave, Sub(o, e) btn.Image = normalImage AddHandler btn.MouseHover, Sub(o, e) btn.Image = hoverImage End Sub 

Called like so:

SetUpButton( Btn_Save, My.Resources.save, My.Resources.savehover ) SetUpButton( Btn_Update, My.Resources.update, My.Resources.updatehover ) SetUpButton( Btn_New, My.Resources.new, My.Resources.newhover ) 

and so on...

In C#, for a syntax comparison:

public static void SetUpButton(Button btn, Image normalImage, Image hoverImage) { btn.MouseLeave += (o,e) => btn.Image = normalImage; btn.MouseHover += (o,e) => btn.Image = hoverImage; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.