Winform Custom Control Why "Object Reference not set to an instance of an object"?

c# custom control designer attributes
c# custom controls download
c# custom user control
custom control vb net example
winforms control tutorial
how to create custom control in visual studio 2015
winforms custom controls library
winforms control library

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:

[Description("Max Value")]
public int Max
    get { return _max; }
    set { 
        _max = value;
        if (DesignMode)

try this:

if (DesignMode && Parent != null)

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.

Develop Custom Controls, How do I make custom controls in C#? [closed] � c# winforms controls. Closed. This question needs to be more focused. It is not currently accepting� Winform Custom Control Why “Object Reference not set to an instance of an object”? Ask Question Asked 9 years, 6 months ago. Active 1 year, 3 months ago.

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:

public MyDataClass Data
    if (object.ReferenceEquals(value, null))
      value = MyDataClass.None; // not null - valid state of no data
    if (this.Created)
      this.textEdit.Text = value.ToString();
    } = value;

protected override void OnVisibleChanged(EventArgs e)
  if (!this.DesignMode)
    this.Data =;

To eliminate the error use either [Browsable(false)] or override OnVisibleChanged.

Create a Control That Takes Advantage of Visual Studio Design , Create a custom picture box with scrollbars in c# .net windows forms using How do I implement Duration: 7:11 Posted: Jul 31, 2016 I have exported a MFC custom control as Regualer DLL using shared MFC DLL just like this. class AFX_EXT_CLASS CCustomBitmapViewer : public CWnd { DECLARE_DYNAMIC(CCustomBitmapViewer) public: CCustomBitmapViewer(); virtual ~CCustomBitmapViewer(); BOOL SetBitmap(UINT nIDResource); BOOL RegisterWindowClass(); virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const

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.

How do I make custom controls in C#?, In this tutorial i show you how to create a custom button control with the appearance of a Flat Duration: 12:37 Posted: Aug 27, 2017 namespace HZH_Controls.Controls { partial class UCStep { /// ///Required designer variables. /// private System.ComponentModel.IContainer components = null

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.

C# Tutorial - Create a Custom control, How to make a simple custom control in C#. The example uses a How to Create Custom Duration: 13:13 Posted: Jun 13, 2017 The ImageBox is a custom control for displaying images. It supports zooming, scrolling, panning, region selection and much more! c-sharp winforms-controls cyotek imagebox

C# Tutorial - Creating custom controls : Flat/Material button, Embedding user controls in a Windows form is just like adding a simple button or text box that are already provided with .NET. These basic controls were written� Running the page will display the message "Hello world" from the custom control as many times as specified in the property of the control. How to apply design-time attributes on the custom control Why design-time attributes are needed The custom control that you built in the previous example works as expected.

Custom Controls C#, NET framework's capability of creating custom controls. Here I'm going to make a custom control and then, test my control in a Windows� Building a container control for WinForms was easier than I first imagined. Why did I build one? Because I needed a custom solution for viewing controls that were created, but could not be placed into any other third-party control. It appears that Microsoft left out the Data Repeater that was in VB 6 for Windows, so now we have to make our own.

Custom Controls in Visual C# .NET, Custom controls are specialized controls in .NET derived from Control class.Here see the step by step process of creating a custom control in C# WinForms. MyControls. Winform And WPF Custom Controls for use, many controls are written by others but some are written by me ,all the controls are really used in project on work,hope that can help you!

  • Why do you refresh the parent? I've never seen this behaviour desired or needed.
  • +1 To Botz3000 - I know this was mentioned already but I just wanted to explicitly state that: if you have any public properties in your control's code, then the the parent designer file (where your custom control resides) will take the liberty of initializing your property to null (or Nothing for VB.Net). The worst part is that sometimes this won't occur until much later which is why it seems like this error comes out of no where sometimes. If you add your control to the parent, then add the public property to the control's code, the designer might pick it up later rather than sooner.
  • enclosing the code inside 'if (this.Created){ }' in the setter fixed the problem in my case. Thanks! great help!!