Jump to content

mousewheel to change the button text


Recommended Posts

Using the following code:

Private Sub Button1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseWheel

Dim zDelta As Int16 = e.Delta / 120

Button1.Text = zDelta.ToString()

End Sub

I'm trying to get the text on the button to increment up and down when I hover over the button and move the mousewheel. All that happens is the button text goes from 0 to 1 to -1 depending upon which way I move the mousewheel. How do I get it to count up 1,2,3,4,5, etc or down -1,-2,-3,-4,-5,etc for each detent move of the mousewheel. It's probably right in front of me ..... just can't see it.

Thanks for any help.

Phil

Link to comment
Share on other sites


Phil,

You're correct you’re missing something right in front of you.

The Delta portion of the mouse event arguments only tells you the direction the mouse wheel is being turned (+) away from you (-) towards you The Mouse Wheel event fires for every preset fractional rotation of the mouse.

Here is your code updated to do what you want

	Private Sub Button1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseWheel
If Button1.Focused Then
If Not IsNumeric(Button1.Text) Then Button1.Text = "0"
If (e.Delta < 0) Then
Button1.Text = (CInt(Button1.Text) - 1).ToString()
Else
Button1.Text = (CInt(Button1.Text) + 1).ToString()
End If
End If
End Sub

Couple things to note here

  1. as your script stands no matter where you are in your form the wheel will change that button from -1 to 1 and so on. adding the
    If Button1.Focused Then

    line ensures it will only perform its intended function when the button is the actively selected element.

  2. The
    If Not IsNumeric(Button1.Text) Then Button1.Text = "0"

    line is only there as a safe guard to ensure the Button has a string that can be converted into a number without raising an exception

  3. Then it's just a matter of checking if e.Delta is (-) or (+) and converting the Button's Text to its integer equivalent subtracting or adding 1 respectively and converting the value back to a string for the Button's Text

Hope that helps

Cheers,

Ryan Strope

Distributed Services

(Software package development/testing)

Lockheed Martin Systems Integration Owego

(Cyber City Computers)

ryan.strope@lmco.com

Quid quid latine dictum sit, altum videtur

Using the following code:

Private Sub Button1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseWheel

Dim zDelta As Int16 = e.Delta / 120

Button1.Text = zDelta.ToString()

End Sub

I'm trying to get the text on the button to increment up and down when I hover over the button and move the mousewheel. All that happens is the button text goes from 0 to 1 to -1 depending upon which way I move the mousewheel. How do I get it to count up 1,2,3,4,5, etc or down -1,-2,-3,-4,-5,etc for each detent move of the mousewheel. It's probably right in front of me ..... just can't see it.

Thanks for any help.

Phil

Link to comment
Share on other sites

Hi Ryan,

I'm learning a lot from your code. The numbering works just great now, but unfortunately, the mousewheel can change the number no matter where the mouse is positioned on or off the form. I would think the 'if statement for the button1.focused' would limit changes to just when I am hovering over the button, but no deal.

Phil

Edited by phlsti
Link to comment
Share on other sites

Phil,

I'm sorry I didn't quite understand what you were wanting to do I thought you would have already had the button under focus.

If you would like to do that when the mouse hovers over the button, then this along with the other Event Handler I sent you will do the trick

	Private Sub Button1_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseHover
Button1.Select()
End Sub

Essentially all this does is whenever you hover your mouse over the button it selects the button as the active control which means the wheel Event Handler will now do its job.

The .Select() method can be kind of convoluted if you're not used to working with forms controls the .Select() method is related to .Focused Property of each form element.

While .Focused is a read-only Property that you can use programmatically to determine if a specific form element is the currently active element or not,

.Select() is the programmatic method to make an element the currently active element (I’ll never understand why they didn't do .Focused and .Focus() )

One side not is the MouseHover event handler has a preset timeout before it fires it's pretty short but not instantaneous when you move the mouse into the button (not sure what the timeout is don't use that event as much myself), but if you find the .MouseHover is not responding quick enough you can use the .MouseEnter Event Handler

	Private Sub Button1_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
Button1.Select()
End Sub

This Event fires as soon as the mouse cursor enters the Element's physical border.

Either way will accomplish what you need.

Feel free to let me know if you need some more direction but if you want that kind of mouse based control look a little deeper into the .Mouse??????? Event handlers they should be very useful to you once you understand what each does.

Cheers,

Ryan Strope

Distributed Services

(Software package development/testing)

Lockheed Martin Systems Integration Owego

(Cyber City Computers)

ryan.strope@lmco.com

Quid quid latine dictum sit, altum videtur

Hi Ryan,

I'm learning a lot from your code. The numbering works just great now, but unfortunately, the mousewheel can change the number no matter where the mouse is positioned on or off the form. I would think the 'if statement for the button1.focused' would limit changes to just when I am hovering over the button, but no deal.

Phil

Link to comment
Share on other sites

THIS IS NEAT!! Thanks Ryan

I used the 'button1.select' method to get the instant response. I had fooled with the mousehover method earlier and there is something like a 400msec delay that gets aggravating if you want to change things NOW hehe.

The 'button1.select method works great. At first, I was getting the same response, with the button value changing no matter where I was on (or off) the form, but that was because the form starts up with the button1 active to start with. When I make another button active by clicking on it or hovering over it (using the same logic), it then works only if I am actually hovering over the button1. So, I just have to add something that makes the button inactive when I am not hovering over it, or puts focus elsewhere when I startup or move off the button. I have tried using the MouseEnter method for the form itself, with a Me.Select command, but it doesn't get focus off the button. Will just keep looking. Actually, I want to have 4-6 buttons on the same form that all have this feature so as to be able to vary their values prior to clicking the button.

Again, thanks for the help ...... this logic is fascinating, and quite a change from the basic programming I did in the 70's.

Phil

Link to comment
Share on other sites

Phil,

Well if you have multiple buttons you want to behave the same way here's the easiest way you can implement it with minimal code

The original Button1_MouseWheel Event handler gets modified to look like the following

Private Sub Button_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseWheel, Button2.MouseWheel
If CType(sender, System.Windows.Forms.Button).Focused Then
If Not IsNumeric(CType(sender, System.Windows.Forms.Button).Text) Then CType(sender, System.Windows.Forms.Button).Text = "0"
If (e.Delta < 0) Then
CType(sender, System.Windows.Forms.Button).Text = (CInt(CType(sender, System.Windows.Forms.Button).Text) - 1).ToString()
Else
CType(sender, System.Windows.Forms.Button).Text = (CInt(CType(sender, System.Windows.Forms.Button).Text) + 1).ToString()
End If
End If
End Sub

Here's the original one so we can do some comparisons

Private Sub Button1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseWheel
If Button1.Focused Then
If Not IsNumeric(Button1.Text) Then Button1.Text = "0"
If (e.Delta < 0) Then
Button1.Text = (CInt(Button1.Text) - 1).ToString()
Else
Button1.Text = (CInt(Button1.Text) + 1).ToString()
End If
End If
End Sub

First of you will notice the

Button1_MouseWheel

has been changed to

Button_MouseWheel

, this is not required I find it just helps to keep someone from assuming something by glancing at the code.

Next you will notice all the

Button1.

instances have been replaced with

CType(sender,System.Windows.Forms.Button).

What we've done with the last change is change the event handler from a specific control id's event handler to a generic button event handler.

The sender object which you will notice in ALL event handlers is an Object. Is the control that raised the event (i.e. Button1) Since literally everything in .NET is inherited from the Object Object you can use the CType() Short for Convert Type method to Explicitly convert the sender back into its System.Windows.Forms.Button Object which means you can then access any of its methods and properties just like you would directly using

Button1.Text

The last item on the change list is at the end of the Handles keyword in the original event handler you had

Handles Button1.MouseWheel

but in the update event handler you have

Handles Button1.MouseWheel, Button2.MouseWheel

Essentially you now have 1 event handler attached to handle 2 buttons’ MouseWheel events.

As you might have guessed you can add as many Button's after the Handles keyword as you want.

So continuing with that logic our MouseEnter event handler becomes

Private Sub Button_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter, Button2.MouseEnter
CType(sender, System.Windows.Forms.Button).Select()
End Sub

And to handle when the mouse is not on the button we add

Private Sub Button_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave, Button2.MouseLeave
SomeOtherWindowFormControl.Select()
End Sub

The last event handler will select another element rendering the button (sender) that raised the event unselected (bear in mind that SomeOtherWindowFormControl is another Control in your form or it is a dummy control made hidden)

So let's recap

we changed our event handlers from what we will call explicit event handlers to be generic event handlers with multiple controls’ specific event being handles by 1 event handler.

We changed the name of the event handlers from Button1_Mouse????? to Button_Mouse????? in a nut shell this is to keep you and anyone else who reads through your code from thinking that Button1_Mouse???? is a generated event handler for Button1 only (which is not the case here) Again not needed but a little house cleaning now will save your sanity when you revisit the code after you haven't looked at it for months and have forgotten the little idiosyncrasies like Button1_MouseWheel is actually tied to multiple buttons not just Button1.

We added a Button_MouseLeave event handler to move focus off the current active button to another predefined control in the form as soon as the mouse cursor leaves the confines of the current active button.

Hope that makes things a little easier for you feel free to ask for clarification if you need it.

Please understand though my primary .NET language is C# so I cannot claim to know everything about how VB.NET handles events. Also don't just take my coding advice as the gospel truth these are just things I have learned over the years and methods I have adopted to keep me out of trouble and minimize my rework. advice is good and should always be welcomed as well as offered but in the end you need to find what works consistently for you.

Hope you have a happy holiday.

Cheers,

Ryan Strope

Distributed Services

(Software package development/testing)

Lockheed Martin Systems Integration Owego

(Cyber City Computers)

ryan.strope@lmco.com

Quid quid latine dictum sit, altum videtur

THIS IS NEAT!! Thanks Ryan

I used the 'button1.select' method to get the instant response. I had fooled with the mousehover method earlier and there is something like a 400msec delay that gets aggravating if you want to change things NOW hehe.

The 'button1.select method works great. At first, I was getting the same response, with the button value changing no matter where I was on (or off) the form, but that was because the form starts up with the button1 active to start with. When I make another button active by clicking on it or hovering over it (using the same logic), it then works only if I am actually hovering over the button1. So, I just have to add something that makes the button inactive when I am not hovering over it, or puts focus elsewhere when I startup or move off the button. I have tried using the MouseEnter method for the form itself, with a Me.Select command, but it doesn't get focus off the button. Will just keep looking. Actually, I want to have 4-6 buttons on the same form that all have this feature so as to be able to vary their values prior to clicking the button.

Again, thanks for the help ...... this logic is fascinating, and quite a change from the basic programming I did in the 70's.

Phil

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...