Click here to Skip to main content
15,901,666 members
Articles / Programming Languages / C#

NumericUpDown with correct read-only behavior

Rate me:
Please Sign up or sign in to vote.
4.00/5 (7 votes)
10 Nov 2006CPOL 49.2K   9   12
A simple article showing another bug in .NET Framework 2.0.

Introduction

When writing a simple application, I needed one of the numeric boxes to be read only, so I just changed the ReadOnly property of NumericUpDown to true. But after a while, I noticed that I can change the value in it, not by typing, but by using the up and down buttons.

Solution

To save people time, I posted this article presenting a simple and fast solution to the problem:

C#
public class FixedNumericUpDown : NumericUpDown
{
    public override void DownButton()
    {
        if (ReadOnly)
            return;
        base.DownButton();
    }

    public override void UpButton()
    {
        if (ReadOnly)
            return;
        base.UpButton();
    }
}

Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Poland Poland
Hi to all.

I’m a software developer who works in a small company. I started programming when I was 10 (or less, can't remember) on not mine C64 – those were the good times :P Now I’m coding mainly in C# / C++, but my favorite language is C.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 864580517-Jul-13 5:05
Member 864580517-Jul-13 5:05 
GeneralMy vote of 4 Pin
Toli Cuturicu19-Aug-10 5:14
Toli Cuturicu19-Aug-10 5:14 
GeneralMight be even better Pin
MillionsterNutzer21-May-10 10:05
MillionsterNutzer21-May-10 10:05 
GeneralNumericUpDown.Increment = 0; Pin
Eugene Goryachev11-Apr-08 0:10
Eugene Goryachev11-Apr-08 0:10 
GeneralFix without replacing the control Pin
los.12-Jun-07 22:32
los.12-Jun-07 22:32 
In case you want to fix NumericUpDown without replacing it with FixedNumericUpDown control, you may use a code like this:
/// <summary><br />
/// Fixes behavior of NumericUpDown control by disabling or enabling up and down buttons<br />
/// according to its ReadOnly property.<br />
/// </summary><br />
/// <param name="numericUpDown">NumericUpDown control to fix.</param><br />
/// <exception cref="ArgumentNullException">Thrown when numericUpDown is set to null.</exception><br />
static public void Fix(NumericUpDown numericUpDown)<br />
{<br />
    if (numericUpDown == null)<br />
        throw new ArgumentNullException("numericUpDown");<br />
<br />
    foreach (Control control in numericUpDown.Controls)<br />
    {<br />
        TextBox textBox = control as TextBox;<br />
<br />
        if (textBox != null)<br />
        {<br />
            textBox.ReadOnlyChanged -= numericUpDown_ReadOnlyChanged; // in case it was already fixed<br />
            textBox.ReadOnlyChanged += numericUpDown_ReadOnlyChanged;<br />
            numericUpDown_ReadOnlyChanged(textBox, EventArgs.Empty);<br />
        }<br />
    }<br />
}<br />
<br />
static void numericUpDown_ReadOnlyChanged(object sender, EventArgs e)<br />
{<br />
    TextBox textBox = (TextBox)sender;<br />
    NumericUpDown numericUpDown = (NumericUpDown)textBox.Parent;<br />
<br />
    foreach (Control control in numericUpDown.Controls)<br />
        if (control.GetType().Name == "UpDownButtons")<br />
            control.Enabled = !textBox.ReadOnly;<br />
}

GeneralRe: Fix without replacing the control Pin
lemueladane15-May-09 1:20
lemueladane15-May-09 1:20 
Generaldisable updown-control Pin
jls1716-Nov-06 7:41
jls1716-Nov-06 7:41 
AnswerRe: disable updown-control Pin
dr4cul416-Nov-06 21:27
dr4cul416-Nov-06 21:27 
Generalcode style opinion Pin
Tornacious14-Nov-06 8:17
Tornacious14-Nov-06 8:17 
QuestionMore readable? Pin
cw@renishaw10-Nov-06 6:04
cw@renishaw10-Nov-06 6:04 
AnswerRe: More readable? Pin
Marc Leger10-Nov-06 8:33
Marc Leger10-Nov-06 8:33 
AnswerRe: More readable? Pin
dr4cul411-Nov-06 2:14
dr4cul411-Nov-06 2:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.