0

I have 15 buttons created through design.

I want them to have a background picture whenever I click on any of them, for example:

  • If I click on button11 then its background will be "Hello.jpg"
  • If click button12 then its background will be become "Hello.jpg"

Is there a method to write a code instead of writing code for individual button?

The code should detect which button I clicked and then change its background.

    Private Sub e_11_Click(sender As Object, e As EventArgs) Handles e_11.Click
e_11.Image = Image.FromFile("E:\battleship\Explode.gif")
End Sub

Is there a way that handles every button click?

1 Answer 1

3

Yes, you can bind the same method to multiple controls:

Private Sub MyButtons_Click(sender As Object, e As EventArgs) _
    Handles e_1.Click, e_2.Click, e_3.Click, ...

    Dim myButton = DirectCast(sender, Button)
    myButton.Image = Image.FromFile("E:\battleship\Explode.gif")
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.