71601

Variable increment step in WinForm NumericUpDown control depending on whether Shift/Ctrl/etc. are pr

Question:

What I want is somewhat similar to what happens in a WinForm designer in Visual Studio, say VS2010. If I place a button and select it and use arrow keys, it will move around by 5 pixels in whatever direction I chose by pressing the right key. Now, if I hold either a Shift or a Ctrl modifier as I do that (forgot which one, sorry), then the button will move only by 1 pixel at a time.

I want this to happen with my NumericUpDown control in a C# WinForm app. Let's say a default increment is 100.0, and a smaller increment is 10.0. An even smaller increment (if possible) can be 1.0. Any hints on how can I do that?

Hopefully I do not need to ask this as a separate question: I am also toying with the idea of having the increment be dependent on the current value entered. Say, I can enter a dollar amount anywhere between 1 and 100 billion. I then want the default, small, and smaller increment values be dependent on the value entered. I can figure out the exact formula myself.

Answer1:

Derive your own class from NumericUpDown and override the UpButton() and DownButton() methods:

using System; using System.Windows.Forms; public class MyUpDown : NumericUpDown { public override void UpButton() { decimal inc = this.Increment; if ((Control.ModifierKeys & Keys.Control) == Keys.Control) this.Increment *= 10; base.UpButton(); this.Increment = inc; } // TODO: DownButton }

Tweak as necessary to give other keys different effects.

Answer2:

This is a bit crude but it's a little simpler than the other answer (not better though) and for the 2nd part of the question you just need to replace the 100/10/1 with a calculation based on the current value.

Set the default incrememnt to 100 (or whatever) and in the keydown event of the NumericUpDown (nUpDown)

private void nUpDown_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.Up) nUpDown.Value += 10; else if (e.Control && e.KeyCode == Keys.Down) nUpDown.Value -= 10; else if (e.Shift && e.KeyCode == Keys.Up) nUpDown.Value += 1; else if (e.Shift && e.KeyCode == Keys.Down) nUpDown.Value -= 1; }

Recommend

  • How can I create a Textbox which only accepts certain integer values?
  • What is UserPreferenceChangedEventHandler in C# winform applications?
  • Mongo DB duplication issue while using sorting with limit and skip in aggregation
  • .NET WinForms combobox bindingsource and databinding question
  • Setting the cursor position in a NumericUpDown Control
  • Progress Bar in Python Console on Windows
  • Spark dataframes: Extract a column based on the value of another column
  • double precision error when converting to scientific notation
  • Inno Setup Search for specifc file on a CD, retrieve exact filepath and return value to [Files]-Sect
  • python & Mysql: unsupported operand type(s) for -: 'int' and 'tuple'
  • ProgressBar Paint Method?
  • Get or convert Week of year to ISO week
  • C: Custom strlen() library function
  • Is it possible to “shrink” a PdfPtable?
  • Efficient & Pythonic way of finding all possible sublists of a list in given range and the minim
  • Private IP address in reserved subnet range
  • How to make R's read_csv2() recognise the text characters properly
  • Specify the x-axis values in ggplot in R
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • Bad request using file_get_contents for PUT request in PHP
  • Ajax Loaded meta Tags
  • CSS Linear-gradient formatting issue accross different browsers
  • req.body is undefined - nodejs
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Modifying destination and filename of gulp-svg-sprite
  • SSO with signing and signature validation doesn't work
  • Deserializing XML into class C#
  • Symfony2: How to get request parameter
  • Akka Routing: Reply's send to router ends up as dead letters
  • GridView Sorting works once only
  • Is there a mandatory requirement to switch app.yaml?
  • File upload with ng-file-upload throwing error
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • costura.fody for a dll that references another dll