首页 新闻 搜索 专区 学院

C# WinForm 自定义控件的自定义事件 实例方法的委托不能具有空“this”

0
悬赏园豆:10 [已关闭问题] 关闭于 2021-02-09 08:15

这里有个自定义的CheckBox,然后我想实现他的MouseClick事件,然后我自定义了个MouseClicked事件

        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.BackgroundImage = global::BingSkin.Properties.Resources.checkbox0;
            this.panel1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
            this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
            this.panel1.Location = new System.Drawing.Point(1, 1);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(18, 28);
            this.panel1.TabIndex = 0;
            this.panel1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.CheckBox_MouseClick);
            // 
            // label1
            // 
            this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.label1.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(226)))), ((int)(((byte)(226)))), ((int)(((byte)(226)))));
            this.label1.Location = new System.Drawing.Point(19, 1);
            this.label1.Name = "label1";
            this.label1.Padding = new System.Windows.Forms.Padding(5, 0, 0, 0);
            this.label1.Size = new System.Drawing.Size(213, 28);
            this.label1.TabIndex = 1;
            this.label1.Text = "复选框";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.label1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.CheckBox_MouseClick);
            // 
            // UCCheckBox
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.BackColor = System.Drawing.Color.Transparent;
            this.Controls.Add(this.label1);
            this.Controls.Add(this.panel1);
            this.Name = "UCCheckBox";
            this.Padding = new System.Windows.Forms.Padding(1);
            this.Size = new System.Drawing.Size(233, 30);
            this.ResumeLayout(false);

        }

        #endregion

        /// <summary>
        /// The panel1
        /// </summary>
        private System.Windows.Forms.Panel panel1;
        /// <summary>
        /// The label1
        /// </summary>
        private System.Windows.Forms.Label label1;
    }
}

        /// <summary>
        /// 鼠标点击事件
        /// </summary>
        [Description("鼠标点击事件"), Category("自定义")]
        public event MouseEventHandler MouseClicked;
        

        private void CheckBox_MouseClick(object sender, MouseEventArgs e)
        {
            if (MouseClicked!=null)
            {
                MouseClicked(this, e);
            }
        }

动态添加CheckBox,并绑定自定义事件MouseClicked事件


            foreach (var item in GlobalInstance._SendContent.Keys)
            {
                bool val = GlobalInstance._SendContent[item];
                UCCheckBox checkBox = new UCCheckBox();
                checkBox.BackColor = System.Drawing.Color.Transparent;
                checkBox.Checked = val;
                checkBox.ForeColor = System.Drawing.Color.FromArgb(226, 226, 226);
                checkBox.Name = item + "CheckBox";
                checkBox.Size = new System.Drawing.Size(315, 30);
                checkBox.TextValue = item;
                checkBox.MouseClicked += new MouseEventHandler(ControlCenter.CheckeBox_MouseClicked);
                panel.Controls.Add(checkBox);
            }

问题:第一次载入调用方法给panel中添加东西的时候没问题,当我第二次调用再添加就有问题了,实例方法的委托不能具有空“this”

但是第一次程序启动,正常载入正常

< >
分享
所有回答(1)
0

因为事件定义是在ControlCenter中的,然后我添加事件的时候,是在util类中加的,util类没有ControlCenter的实例,所以就空“this”

echo_lovely | 园豆:321 (菜鸟二级) | 2021-02-09 08:15
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册