Search code examples
c#visual-studio-2022windows-forms-designer.net-4.8visual-studio-designer

Why does my Designer Generated Code (InitializeComponent()) Not Generate an Object Instantiation Line For my Custom Control?


Enviroment details:

  • .NET 4.8
  • Visual Studio 2022
  • Designer interface (if that's what it's actually called)

I've created a custom form called WeatherBox which inherits from another custom form MultiSelectComboBox. After building the project, I can select the WeatherBox item from the toolbox window and create one within the designer view.

However, when I build the project, after adding the WeatherBox to the form, it throws an error because the designer generated code didn't include a line instantiating the custom class (as it normally does, and it does with MultiSelectComboBox which WeatherBox inherits from). Obviously, I can manually fix the error by implementing the instantiation myself, but that would be a pain to do every time I wanted to create a new WeatherBox.

Here is the WeatherBox class:

using [redacted].Util;
using [redacted].GUI.CustomizedControls;
using [redacted].GUI.CustomizedControls.MultiSelectComboBoxControl;
using System;
using System.Windows.Forms;

namespace [redacted].GUI.CustomizedControls.MultiSelectComboBoxControl.CustomMultiSelectComboBoxes
{
    public class WeatherBox : MultiSelectComboBox
    {
        WeatherBox() : base()
        {
            ListItemSelected += new System.EventHandler(WeatherYearBox_ListItemSelected);
        }

        private static void WeatherYearBox_ListItemSelected(object sender, EventArgs e)
        {
            try
            {
                CheckedListView CLV = (CheckedListView)sender;
                MultiSelectComboBox CMB = (MultiSelectComboBox)CLV.Tag;

                if (CMB.List.FocusedItem != null && CMB.List.FocusedItem.Text != "")
                {
                    CMB.close();
                    CMB.Tag = CMB.List.FocusedItem.Tag;
                    CMB.Items.Insert(0, CMB.List.FocusedItem.Text);
                    CMB.SelectedIndex = 0;
                }
                else
                    CMB.Tag = null;
            }
            catch (Exception ex)
            {
                Logger.logInfo(ex);
            }
        }
    }
}

The MultiSelectComboBoxControl is quite a large class, so I've just copied the instantiation (I'm not sure how relevant that is to the problem):

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using [redacted].Util; 
using System.Text.RegularExpressions;
using System.Drawing;

namespace SERVM.MCFRED.GUI.CustomizedControls.MultiSelectComboBoxControl
{
    public class MultiSelectComboBox : ComboBox
    {
        public MultiSelectComboBox() : base()
        {
                contents.Owner = this;
                contents.searchbox.Tag = this;
                contents.searchBoxPic.Tag = true;
                popup = new Popup(contents);
                popup.Resizable = true;
                popup.Height = 500;
                popup.Tag = this;
                popup.LostFocus += new EventHandler(popup_LostFocus);
                popup.VisibleChanged += new EventHandler(popup_VisibleChanged);
                contents.resizeBox.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
                contents.resizeBox.MouseUp += new MouseEventHandler(pictureBox1_MouseUp);
                contents.resizeBox.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);

                if (!isMultiSelectEnable)
                {
                }

                contents.checkedListView.Tag = popup;

                List = contents.checkedListView;
                List.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                List.Click += new EventHandler(delegate { });

                SuspendLayout();
                Name = "MultiSelectComboBox";
                Size = new System.Drawing.Size(237, 23);
                DropDownHeight = 1;
                DropDownStyle = ComboBoxStyle.DropDownList;
                Click += new EventHandler(SortableDropDown_Click);
                MouseHover += new EventHandler(MultiSelectComboBox_MouseHover);
                MouseWheel += new MouseEventHandler(MultiSelectComboBox_MouseWheel);
                Layout += new LayoutEventHandler(MultiSelectComboBox_Layout);
                ResumeLayout(false);
            }
    }
}

Most everything here is legacy code, please forgive the mess.

And then as for designer generated code: it generates the following lines for both custom controls outside of the InitializeComponent():

private CustomizedControls.MultiSelectComboBoxControl.CustomMultiSelectComboBoxes.WeatherBox weatherBox1;
private CustomizedControls.MultiSelectComboBoxControl.MultiSelectComboBox multiSelectComboBox1;

But, inside the InitializeComponent(), at the top, it generates:

this.multiSelectComboBox1 = new [redacted].GUI.CustomizedControls.MultiSelectComboBoxControl.MultiSelectComboBox();

But does not generate, what I assume it should:

this._weatherYearBox = new [redacted].GUI.CustomizedControls.MultiSelectComboBoxControl.CustomMultiSelectComboBoxes.WeatherBox();

Thanks for taking a look at my issue!

I've tried building, re-building and cleaning the solution in combination with restarting Visual studio, as well as deleting/adding that : base() signature thing after the constructor for WeatherBox (I copied it because I saw it was present in the MultiSelectComboBox class which was working- but the presence or absence of : base() after the constructor doesn't seem to make a difference") - I should probably research what that means. Will do after finishing this post.

None of these solutions have helped. The designer code generator remains broken when I try to implement a WeatherBox.


Solution

  • I needed to add a public flag to the initializer method, otherwise the designer code generator couldn't access it.

    Adding:

    public WeatherYearBox()
    

    fixed the issue.