phlsti Posted December 17, 2007 Share Posted December 17, 2007 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 SubI'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 More sharing options...
TofuBug Posted December 18, 2007 Share Posted December 18, 2007 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 SubCouple things to note here 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.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 exceptionThen 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 TextHope that helps Cheers,Ryan StropeDistributed Services(Software package development/testing)Lockheed Martin Systems Integration Owego(Cyber City Computers)ryan.strope@lmco.comQuid quid latine dictum sit, altum videturUsing 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 SubI'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 More sharing options...
phlsti Posted December 20, 2007 Author Share Posted December 20, 2007 (edited) 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 December 20, 2007 by phlsti Link to comment Share on other sites More sharing options...
TofuBug Posted December 20, 2007 Share Posted December 20, 2007 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 SubEssentially 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 SubThis 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 StropeDistributed Services(Software package development/testing)Lockheed Martin Systems Integration Owego(Cyber City Computers)ryan.strope@lmco.comQuid quid latine dictum sit, altum videturHi 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 More sharing options...
phlsti Posted December 23, 2007 Author Share Posted December 23, 2007 THIS IS NEAT!! Thanks RyanI 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 More sharing options...
TofuBug Posted December 24, 2007 Share Posted December 24, 2007 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 codeThe original Button1_MouseWheel Event handler gets modified to look like the followingPrivate 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 IfEnd SubHere's the original one so we can do some comparisonsPrivate 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 IfEnd SubFirst 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.TextThe 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.MouseWheelEssentially 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 becomesPrivate 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 SubAnd to handle when the mouse is not on the button we addPrivate Sub Button_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave, Button2.MouseLeave SomeOtherWindowFormControl.Select()End SubThe 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 StropeDistributed Services(Software package development/testing)Lockheed Martin Systems Integration Owego(Cyber City Computers)ryan.strope@lmco.comQuid quid latine dictum sit, altum videturTHIS IS NEAT!! Thanks RyanI 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 More sharing options...
phlsti Posted December 25, 2007 Author Share Posted December 25, 2007 Thanks again, Ryan, this made my holiday!! My code works now, and I have learned alot; appreciate all of your advice. Have a great holiday.Phil Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now