C# WinForms numericUpDown control (removing the spin box)

17,662

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

Share:
17,662

Related videos on Youtube

krebstar
Author by

krebstar

Updated on June 04, 2022

Comments

  • krebstar
    krebstar over 1 year

    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
    krebstar over 12 years
    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
    krebstar over 12 years
    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: imgur.com/EP1OD
  • krebstar
    krebstar over 12 years
    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
    krebstar over 12 years
    Whoops.. Looks like you can paste into a numericUpDown control :(