这里有个自定义的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);
}
}
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);
}
但是第一次程序启动,正常载入正常
因为事件定义是在ControlCenter
中的,然后我添加事件的时候,是在util类中加的,util类没有ControlCenter
的实例,所以就空“this”