VB.Net button problem... (RESOLVED)

ferrari_chris

fcOnTheWeb.com
Premium
4,728
ferrari_chris
I've run into a little problem with an app that I'm building that I can't find a fix for. It's not a problem that will stop it from working in any way, just a design thing I want to get cleared up.

Here's the problem:

I've put an image on my button. When you click the button, the button moves down and to the right. To make the image move too, I replace the original with a new image 1px lower and 1px to the right. This works well, and looks like factory. 👍

The problem starts when the user holds the mouse button down, but rolls off the button. The button rises back to it's unclicked position, but the image stays down. 👎

Is there any way to get the image to revert to the original and move with the button?

I've attached both the code (ButtonTestCode) and a simple exe file (ButtonTestExe) so you can see exactly what is happening.

Try first clicking the button to see the image move, then while holding the mouse button move the pointer off the button and see how the image stays down. :grumpy:

Any help is greatly appreciated. :)
 

Attachments

  • ButtonTestExe.zip
    5.8 KB · Views: 7
  • ButtonTestCode.zip
    26.4 KB · Views: 7
That is because the MouseUp event, in which you set back the image won't fire until you release the mouse button, even if the button itself reacts to the mouse leaving its area, depressing it.

You won't have luck with the MouseLeave event either, since in that case it would be fired after your MouseUp event too. If that really bothers you, you could always use the MouseMove event for the button, which will be fired regardless of the other Mouse events, using its MouseEventArgs variable to check if the mouse leaves the area of the button before the MouseUp event is fired.

Should look like that:

Code:
Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove

   If e.Button = Windows.Forms.MouseButtons.Left Then
      If e.Location.X < 0 Or e.Location.X > Button1.Width Or e.Location.Y < 0 Or e.Location.Y > Button1.Height Then
         Button1.Image = Image.FromFile(Application.StartupPath & "\button-up.jpg")
      Else
         Button1.Image = Image.FromFile(Application.StartupPath & "\button-down.jpg")
      End If
   End If
End Sub

For better modularity you should also cast the Object variable sender into a Button variable instead of using a direct referrence to Button1, ideally after checking e.Button so it's only done when required.

Hope that helps.
 
Carl.
That is because the MouseUp event, in which you set back the image won't fire until you release the mouse button, even if the button itself reacts to the mouse leaving its area, depressing it.

You won't have luck with the MouseLeave event either, since in that case it would be fired after your MouseUp event too. If that really bothers you, you could always use the MouseMove event for the button, which will be fired regardless of the other Mouse events, using its MouseEventArgs variable to check if the mouse leaves the area of the button before the MouseUp event is fired.

Should look like that:

Code:
Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove

   If e.Button = Windows.Forms.MouseButtons.Left Then
      If e.Location.X < 0 Or e.Location.X > Button1.Width Or e.Location.Y < 0 Or e.Location.Y > Button1.Height Then
         Button1.Image = Image.FromFile(Application.StartupPath & "\button-up.jpg")
      Else
         Button1.Image = Image.FromFile(Application.StartupPath & "\button-down.jpg")
      End If
   End If
End Sub

For better modularity you should also cast the Object variable sender into a Button variable instead of using a direct referrence to Button1, ideally after checking e.Button so it's only done when required.

Hope that helps.
:bowdown:

Thankyou very much - it works like a charm. 👍

I'd tried all the other Mouse Events (MouseLeave, MouseHover etc.) but hadn't tried MouseMove. I'd tried to use MouseMove on another control some months ago and couldn't get it to do anything other than generate an error. :dunce:

I'd sort of taken a set against it I guess. :indiff:

I'm liking it a lot more now though.

Thanks. :)
 

Latest Posts

Back