65.9K
CodeProject is changing. Read more.
Home

NumericUpDown with correct read-only behavior

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (7 votes)

Nov 10, 2006

CPOL
viewsIcon

50112

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:

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

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

Enjoy!