Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

trap up/down arrow click event from updowncontrol

Posted on 2013-01-30
6
Medium Priority
?
369 Views
Last Modified: 2013-01-31
I have a VisualStudio 2008 / VisualBasic 2008 (.Net) application with a form that includes a numeric up/down control.  I need to trap the up/down arrow click so that I can programmatically increment the value:
When the user uses the KBD (or cut-n-paste etc.) to enter/change the value the resulting value can be anything between min and max.
When the up/down arrow is used the resulting value must be the next higher/lower multiple of a specific increment - as long as it does not violate min or max ( if it does value is to remain unchanged ).
Increment = 12, Current value is 24, user can change value to 31
Increment = 12, Current value is 31, user clicks down arrow; resulting value is 24
Increment = 12, Current value is 31, user clicks up arrow; resulting value is 36
0
Comment
Question by:Swamp_Thing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1050 total points
ID: 38837352
Inherit from NumericUpDown() and override the UpButton() and DownButton() methods, which fire when the user clicks the arrows OR the Up/Down arrow keys are used:
Public Class MyNumericUpDown
    Inherits NumericUpDown

    Public Overrides Sub UpButton()
        If Me.Value Mod Me.Increment = 0 Then
            MyBase.UpButton()
        Else
            Me.Value = Math.Min((CInt(Me.Value) \ CInt(Me.Increment) + 1) * Me.Increment, Me.Maximum) ' <-- INTEGER division!
        End If
    End Sub

    Public Overrides Sub DownButton()
        If Me.Value Mod Me.Increment = 0 Then
            MyBase.DownButton()
        Else
            Me.Value = Math.Max((CInt(Me.Value) \ CInt(Me.Increment)) * Me.Increment, Me.Minimum) ' <-- INTEGER division!
        End If
    End Sub

End Class

Open in new window


When you rebuild or run, the new control will appear at the TOP of the ToolBox.  Use that instead of the normal NumericUpDown.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38837542
wow!!!!!!! ...You Dont exist!! :)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38837564
Lol...you figured me out jtoutou!  I'm a manifestation of the accumulated knowledge on the web.  I exist solely as a sentient digital entity in the bowels of the interwebs.  ;)
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 2

Author Comment

by:Swamp_Thing
ID: 38839758
It took a couple of tries to figure out where this code goes...   Initially I put it into one of my modules but no control showed up on the tools in design mode and the event didn't fire.  What worked was to paste the code into one of my forms .Designer.vb code at the very top - prior to the "<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _" and "Partial Class <formname>" etc. etc.
I did not need to drag-n-drop the new control onto my form:  I changed the .Designer.vb code from defining my existing control as “New System.Windows.Forms.NumericUpDown” to “New <MyProjectName>.MyNumericUpDown”
0
 
LVL 2

Author Closing Comment

by:Swamp_Thing
ID: 38839772
I'm not a novice but something along the lines of "cut-n-paste this code into the very beginning of your main forms .Designer.vb file" would have helped.

Crystal clear answer - 100% complete and accurate sample code!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38839789
Ah...there's more than one way to add the control to your project.  Just ask next time.  Glad it worked for you.  =)
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question