Reputation: 38180
I created a custom control with a max value. When adding if (DesignMode) Parent.Refresh(); it compiles but in client it crashes with the message error:
Object Reference not set to an instance of an object
Call stack:
at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value)
at Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkPropertyDescriptor.SetValue(Object component, Object value)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializePropertyAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement, CodePropertyReferenceExpression propertyReferenceEx, Boolean reportError)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)
Source code:
[Category("Main")]
[Description("Max Value")]
[DefaultValue(100)]
public int Max
{
get { return _max; }
set {
_max = value;
if (DesignMode)
{
Parent.Refresh();
}
}
}
Upvotes: 4
Views: 11923
Reputation: 441
I just run the code and open that form from appropriate menu. Then took a screen shot and save the image. Then went to solution explorer opened '.designer.cs' file, copy the code and save it in notepad. Now opened the designer again and click on 'Ignore and Continue' and confirmed it. It worked for me. But if you have some control missing you can create that control by drag and drop and look at the saved screen shot and '.designer.cs' file so that the control is as previous.
Upvotes: 0
Reputation: 39600
try this:
if (DesignMode && Parent != null)
{
Parent.Refresh();
}
Most probably the control has not yet been added to its Parent when the value is set for the first time.
If you look at your Form's *.designer.cs, you'll notice that the properties of your usercontrol get assigned before it gets added to the parent form. That's why you get the exception.
Upvotes: 6
Reputation: 1120
I had a similar problem. The component code caused this error. The simple solution in my case was encapsulating all code that works with graphical components:
if (this.Created)
{
// insert code with other components here
}
Unfortunately this solution do not work correctly with tab control (when component is not on first visible tab).
In my case the problem was caused by a property. The property has a active initialisation of child components. Final solution is:
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
[Category("Main")]
[Description("...")]
[DefaultValue(MyDataClass.None)]
public MyDataClass Data
{
get
{
return this.data;
}
set
{
if (object.ReferenceEquals(value, null))
value = MyDataClass.None; // not null - valid state of no data
if (this.Created)
{
this.textEdit.Text = value.ToString();
}
this.data = value;
}
}
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
if (!this.DesignMode)
this.Data = this.data;
}
To eliminate the error use either [Browsable(false)]
or override OnVisibleChanged
.
Upvotes: 4
Reputation: 5150
You have to check for null as well for Parent and also make sure that you are setting the Parent object or the Parent.Refresh() will never execute.
Upvotes: 0