Visual Studio WinForms Designer Does Not Create an Object

I created a class derived from the System.Windows.Forms.ContextMenuStrip class, and not as a user control, just a regular .cs class with a constructor and one event handler.

When I drag this class from Toolbox to the constructor, it creates a private element and a couple of properties for it, but does not instantiate the object.

Thus, at runtime, I get "Object reference not set to instance of object." Since the constructor never generates a string:

this.searchGridContextMenu1 = new SearchGridContextMenu ();

inside InitializeComponent.

He used to generate this line, in fact, I always return it from my Vault repository, but the designer just “eats it” again.

Update: Now I tried to create User Control using the same class, and it has the same problem as this.

+3
source share
4 answers

I crossed this comment on another issue, but since it is related to it again.

When a user control does not load into the Visual Studio constructor, this is what you need to do. These instructions are for the vb.net project, but C # should be similar. Also, before doing so, close all open windows (or at least the source and design files of the control you are working on.)

. , , , visual studio . , . , visual studio. , , , , .

:

  • .
  • .
  • "" " " Visual Studio.

. , Visual Studio . Visual Studion (INSTANCE_1) "" (INSTANCE_2) .

  1. . INSTANCE_2 .
  2. INSTANCE_1.
  3. INSTANCE_1 CTRL-ALT-E. "". THROWN Common Language.

. , INSTANCE_1 BREAK , try.

  1. INSTANCE_2. , .

, INSTANCE_1 Visual Studio , . ( IsNot Nothing ... .)

, , INSTANCE_2 , INSTANCE_1. ... INSTANCE_2. / INSTANCE_1, .

. / , . , , , , .

, .

+5

? Visual Studio , ?

? - - , , , - , , InitializeComponent.

, ... , Visual Studio. .

+3

. , , :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinForms
{
    class Foo : System.Windows.Forms.ContextMenuStrip
    {
        public Foo()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
        }

        #endregion
    }
}
+1
source

This problem occurs when InitializeComponent () intersects the base and legacy components. The solution calls the component constructor method. Otherwise, the base method is called.

public Form()
{
    this.InitializeComponent(); 
    // base.InitializeComponent <-- default one is thisone
}
+1
source

Source: https://habr.com/ru/post/1702724/


All Articles