سؤال

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?

هل كانت مفيدة؟

المحلول 2

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.

نصائح أخرى

DefaultValue is only an attribute used by the designer to determine whether or not to serialize the data (and make it bold in the PropertyGrid). In your code, you would still have to "set" the default value yourself.

Public Class xNumericUpDown
  Inherits NumericUpDown

  Public Sub New()
    MyBase.DecimalPlaces = 3
  End Sub

  <DefaultValue(3)> _
  Public Shadows Property DecimalPlaces As Integer
    Get
      Return MyBase.DecimalPlaces
    End Get
    Set(value As Integer)
      MyBase.DecimalPlaces = value
    End Set
  End Property
End Class
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top