Search code examples
vb.netnumericupdown

Defining default values for subclassed NumericUpDown control


I am subclass my NumericUpDown controls to name xNumericUpDown which appears now on top of toolbox in my IDE.
I would like that my new control set default values differently from original control.
Most important will be DecimalPlaces=2, Minimal=Decimal.MinValue, Maximal=Decimal.MaxValue and Increment=0.

I suppose for that I should make right properties in subclass. So I try as such:

 <DefaultValue(Decimal.MinValue), _
  Browsable(True)> _
Shadows Property Minimum() As Decimal
    Get
        Return MyBase.Minimum
    End Get
    Set(ByVal value As Decimal)
        MyBase.Minimum = value
    End Set
End Property

But this don't work. When placed to form my control have properties of original NumericUpDown.
Minimum=0, Maximum=100, DecimalPlaces=0, Increment=1.

How can I get desired functionality?


Solution

  • I don't know Vb.Net very well but here is c# where you create your own control giving the properties your default values.

    public class MyNumericUpDown : NumericUpDown
    {
        public MyNumericUpDown():base()
        {
            DecimalPlaces = 2;
            Minimum = decimal.MinValue;
            Maximum = decimal.MaxValue;
            Increment = 1;
        }
    }
    

    As I said I don't know vb.Net but I think this is the translation...

    Public Class MyNumericUpDown Inherits NumericUpDown
    {
        Public Sub New()
        {
            MyBase.New()
            DecimalPlaces = 2
            Minimum = decimal.MinValue
            Maximum = decimal.MaxValue
            Increment = 1   
        }
    }
    

    If you don't have a need to use NumericUpDown with constant defaults then creating a custom control wouldn't be of value and you should simply create different objects for each need.

        numericUpDown1 = New NumericUpDown()
    
        ' Set the Minimum, Maximum, and other values as needed.
        numericUpDown1.DecimalPlaces = 2
        numericUpDown1.Maximum = decimal.MaxValue
        numericUpDown1.Minimum = decimal.MinValue
        numericUpDown1.Increment = 1
    

    You would only use the Shadow keyword to hide implementations in a base class for a class that you derived.