0

I have noticed that all Controls have a Text property. However, the Intellisense doesn't suggest it for NumericUpDown objects. When manually writing it down, it does work and returns the value of the NumericUpDown as a string. Why is that?

Michael Haddad
  • 4,085
  • 7
  • 42
  • 82

1 Answers1

3

The docs show the property defined as:

[BrowsableAttribute(false)]
[BindableAttribute(false)]
public override string Text { get; set; }

The BrowsableAttribute(false) bit (or more likely EditorBrowsableAttribute) is what 'hides' it from Intellisense.

Why does it hide it?

This API supports the product infrastructure and is not intended to be used directly from your code.

mjwills
  • 23,389
  • 6
  • 40
  • 63
  • Thanks! But what if I have a method that gets a `Control` and uses its text? I would have to check if it is a `TextBox` and then use the `Text` property, if it is a `NumericUpDown` and then use the `Value` property, and who knows what other `Control`s. Having one common property to get the value of a `Control` makes much more sense! – Michael Haddad Nov 24 '17 at 10:21
  • You'll have to take that up with Microsoft. They wrote the control and the docs. – mjwills Nov 24 '17 at 10:22
  • 2
    Here's the source-code which also mentions that they have overridden it just to make it non-browsable: http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/NumericUpDown.cs,0aaedcc47a6cf725,references – Tim Schmelter Nov 24 '17 at 10:23
  • @Sipo: just because you don't see it in intellisense doesn't mean you can't use it ' – Tim Schmelter Nov 24 '17 at 10:25
  • @TimSchmelter - even though the whole reason they hide it is for you to not use it? – Michael Haddad Nov 24 '17 at 10:26
  • 1
    @Sipo: the whole reason is that you don't use it when you are not 100% sure what you're doing. You have to read documentation and you'll learn there is a `Value` property. That should be used. Otherwise people would use the inherited `Text` property because intellisense suggests it. Note that `Text` is normally the label of a control. They could extend this control in future to provide a label and `Text` returns just this text whereas the `Value` is independend. Then that would break a lot of code if people would have used `Text` as `Value`. Note that it's also a `decimal` and not a `string`. – Tim Schmelter Nov 24 '17 at 10:33
  • @TimSchmelter So if really want to avoid endless if-else statements to find out which property exactly give me the value of the `Controls` - it is fine to use the `Text` property? – Michael Haddad Nov 24 '17 at 10:39
  • @Sipo: Depends on what your method which takes a control want to do.Either use the `Text`, go the "endless if-else statements"-approach or use a completely different(if possible). What you are trying to do? – Tim Schmelter Nov 24 '17 at 10:51
  • @TimSchmelter, The method is supposed to get a control as a parameter and validate its value. – Michael Haddad Nov 24 '17 at 11:18
  • 1
    @Sipo - What kind of validation? By its very nature, the control ensures its input is numeric. By some of its properties, it enforces min and max values. What possible validation is possible that only depends on the textual version of its actual value, and isn't available through more specific controls? – Damien_The_Unbeliever Nov 24 '17 at 19:10
  • @Damien_The_Unbeliever - Let's say I want the `NumericUpDown` to have a minimum of 10. If the user types 8, and then click "Save", the value will automatically change to 10, and the user will not be notified unless he actually sees the automatic change. The method is supposed to let him know there is a problem (using an `ErrorProvider` etc.). Thanks! – Michael Haddad Nov 28 '17 at 14:09