views:

451

answers:

3

Hi,

I have a "numeric textbox" in C# .NET which is nothing more than a derivation of Textbox, with some added logic to prevent the user entering anything non-numeric. As part of this, I have added a Value property of type double? (or Nullable<double>). It's nullable to support the case where the user doesn't enter anything.

The control works fine when run, but the Windows Forms designer doesn't seem to like dealing with it much. When the control is added to a form, the following line of code is generated in InitializeComponent():

this.numericTextBox1.Value = 1;

Remember 'Value' is of type Nullable<double>. This generates the following warning whenever I try to reopen the form in the Designer:

Object of type 'System.Int32' cannot be converted to type 'System.Nullable`1[System.Double]'.

As a result, the form cannot be viewed in the Designer until I manually remove that line and rebuild - after which it's regenerated as soon as I save any changes. Annoying.

Any suggestions?

A: 

Could it help to setting the DefaultValue attribute on that property to new Nullable(1)?

[DefaultValue(new Nullable<double>(1))]  
public double? Value ...
AlexDuggleby
+2  A: 

Or, if you don't want the designer adding any code at all... add this to the Property.

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
Shaun Austin
+1  A: 

@Shaun Austin, that seems like a decent workaround. Thanks.

Always a pleasure mate!
Shaun Austin