11

After a ton of googling, I couldn't come up with anything..

Is there any way to get a numericUpDown control that does not have the spin box?

I need a textbox that only accepts integers, and a numericUpDown has the perfect behavior that I am looking for. However, I need to hide the numeric spinbox for space constraints.

When I try to do something like numericUpDown.Controls[0].Hide() or numericUpDown.Controls.RemoveAt(0), the spinbox disappears but leaves an unusuable void where the spinbox used to be. Thus the numbers scroll at that point, meaning the space is wasted..

Is there any other solution to this?

Thanks..

krebstar
  • 3,956
  • 8
  • 46
  • 64

2 Answers2

11

You can inherit from NumericUpDown. The trick is to hide control when the control is created.

public class NumericUpDownWitoutButtons : NumericUpDown
{
    public NumericUpDownWitoutButtons()
    {
        Controls[0].Visible = false;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.Clear(SystemColors.Window);
        base.OnPaint(e);
    }
}

If the place were buttons should be looks weird, override OnPaint too.

Also, you probably don't need NumericUpDown. Would it be enough to validate that only digits can by typed in? C# Numbers Only Textbox

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
  • I just tried this, sorry, it does not work.. The right side of the text box has a space that is unusable.. The text starts scrolling when it hits the white space where the UpDown buttons used to be :( – krebstar Jun 03 '11 at 03:24
  • Hi, thanks for the help.. But it still doesn't address my problem.. Here's a picture to show you what I'm talking about: http://imgur.com/EP1OD – krebstar Jun 03 '11 at 03:37
  • I really wanted to use the numericUpDown control because I liked that you couldn't paste anything else in it as well.. But I guess if this doesn't work I will have to use the regular textbox.. :S – krebstar Jun 03 '11 at 03:39
  • Whoops.. Looks like you can paste into a numericUpDown control :( – krebstar Jun 03 '11 at 03:42
  • this is good for me too, indeed the space used for arrows is still there. Hope client will like the way it is :) – Alexa Adrian Nov 20 '13 at 08:58
0

I settled for disabling the arrow buttons, as less ugly than trying to hide or remove them.

Immediately after this boilerplate code:

internal MainWin()
{
    InitializeComponent();

, add this:

    myNumericUpDown.Controls[0].Enabled = false;  // Disable the arrow buttons.

Also: set InterceptArrowKeys to false in the control's properties, to prevent arrow keys (on the keyboard) from incrementing and decrementing myNumericUpDown.Value.

Bad ways:

    myNumericUpDown.Controls[0].Hide();  // Hide the arrow buttons.
    // This leaves a gray patch that turns white after minimize-and-restore.

    myNumericUpDown.Controls.RemoveAt(0);  // Remove the arrow buttons.
    // This leaves a gray patch that turns white after minimize-and-restore.

Half-bad way:

Forget the above. Only set Increment to 0 in the control's properties. The arrow buttons still operate, and the arrow keys (on the keyboard) are still intercepted, but now they do nothing. Sadly, no-action buttons are probably less intuitive than disabled arrow buttons. (I'm surprised that it even accepts Increment = 0. A smart version of this control would neatly hide the arrow buttons when the increment is 0.)

Other solution(s):

A number-box can be implemented as a TextBox with extra code that restricts input to only numbers, and returns or announces a numeric value (decimal or your choice). (Searching finds many diverse, complex examples.) Pasted data comes in differently from keystrokes, so additional code is needed to handle pastes. (Upside: You can make the number-box smarter than the built-in NumericUpDown: NumericUpDown stupidly accepts and displays . until the user presses Enter or leaves the field, even if you specify DecimalPlaces = 0; and it accepts and displays multiple decimal points, which is nonsense.)

An integer-box is simpler than a generalized number-box that accepts real numbers (optionally in floating-point notation) and returns a real-number type (float, double, or decimal).

A876
  • 471
  • 5
  • 8