<p>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 <code>double?</code> (or <code>Nullable&lt;double&gt;</code>). It's nullable to support the case where the user doesn't enter anything.</p>

<p>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():</p>

<pre><code>this.numericTextBox1.Value = 1;
</code></pre>

<p>Remember 'Value' is of type <code>Nullable&lt;double&gt;</code>. This generates the following warning whenever I try to reopen the form in the Designer:</p>

<pre><code>Object of type 'System.Int32' cannot be converted to type 'System.Nullable`1[System.Double]'.
</code></pre>

<p>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.</p>

<p>Any suggestions?</p>

[{'answer_id': 56528, 'author': 'Alex Duggleby', 'author_id': 5790, 'author_profile': 'https://Stackoverflow.com/users/5790', 'pm_score': -1, 'selected': False, 'text': '<p>Could it help to setting the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute(VS.80).aspx" rel="nofollow noreferrer">DefaultValue attribute</a> on that property to new Nullable(1)?</p>\n\n<pre><code>[DefaultValue(new Nullable&lt;double&gt;(1))]  \npublic double? Value ...\n</code></pre>\n'}, {'answer_id': 56533, 'author': 'Shaun Austin', 'author_id': 1120, 'author_profile': 'https://Stackoverflow.com/users/1120', 'pm_score': 3, 'selected': True, 'text': "<p>Or, if you don't want the designer adding any code at all... add this to the Property.</p>\n\n<pre><code>[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\n</code></pre>\n"}, {'answer_id': 12729701, 'author': 'Svetlin Ralchev', 'author_id': 538886, 'author_profile': 'https://Stackoverflow.com/users/538886', 'pm_score': 2, 'selected': False, 'text': '<p>It seems that there is an issue in Visual Studio 2008. You should create custom CodeDomSerializer to work around it:</p>\n\n<pre><code>public class CategoricalDataPointCodeDomSerializer : CodeDomSerializer\n{\n    public override object Deserialize(IDesignerSerializationManager manager, object codeObject)\n    {\n        CodeStatementCollection collection = codeObject as CodeStatementCollection;\n\n        if (collection != null)\n        {\n            foreach (CodeStatement statement in collection)\n            {\n                CodeAssignStatement codeAssignment = statement as CodeAssignStatement;\n\n                if (codeAssignment != null)\n                {\n                    CodePropertyReferenceExpression properyRef = codeAssignment.Left as CodePropertyReferenceExpression;\n                    CodePrimitiveExpression primitiveExpression = codeAssignment.Right as CodePrimitiveExpression;\n\n                    if (properyRef != null &amp;&amp; properyRef.PropertyName == "Value" &amp;&amp; primitiveExpression != null &amp;&amp; primitiveExpression.Value != null)\n                    {\n                        primitiveExpression.Value = Convert.ToDouble(primitiveExpression.Value);\n                        break;\n                    }\n                }\n            }\n        }\n\n        return base.Deserialize(manager, codeObject);\n    }\n}\n</code></pre>\n\n<p>Then you should apply it by using the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.design.serialization.designerserializerattribute.aspx" rel="nofollow">DesignerSerializer</a> attribute on your class.</p>\n'}]
['https://Stackoverflow.com/questions/56521', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']