You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 4.8自定义控件WeatherBox在Designer中未生成实例化代码问题

问题:WinForms设计器无法自动生成自定义继承控件的实例化代码

环境信息

  • .NET 4.8
  • Visual Studio 2022
  • WinForms设计器界面

问题描述

基于自定义控件MultiSelectComboBox继承实现了WeatherBox控件,编译后可从工具箱拖入设计器,但构建项目时报错:设计器生成的InitializeComponent()方法中未包含WeatherBox的实例化语句(父类MultiSelectComboBox能正常生成)。手动添加实例化代码可解决问题,但每次新增控件都需手动操作,十分繁琐。

相关代码

WeatherBox类

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);
            }
        }
    }
}

MultiSelectComboBox构造函数片段

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);
            }
    }
}

设计器生成代码情况

  • 控件声明(InitializeComponent()外部):
private CustomizedControls.MultiSelectComboBoxControl.CustomMultiSelectComboBoxes.WeatherBox weatherBox1;
private CustomizedControls.MultiSelectComboBoxControl.MultiSelectComboBox multiSelectComboBox1;
  • InitializeComponent()内部仅生成父类控件的实例化:
this.multiSelectComboBox1 = new [redacted].GUI.CustomizedControls.MultiSelectComboBoxControl.MultiSelectComboBox();
  • 缺失WeatherBox的实例化语句:
this._weatherYearBox = new [redacted].GUI.CustomizedControls.MultiSelectComboBoxControl.CustomMultiSelectComboBoxes.WeatherBox();

已尝试的解决方案

  • 清理、重建解决方案并重启Visual Studio
  • 调整WeatherBox构造函数的: base()语句

以上操作均未解决问题,设计器仍无法自动生成WeatherBox的实例化代码。


内容的提问来源于stack exchange,提问作者when-cowz-fly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 22:32:02