自定义控件 , 自定义控件里有一个 label ,我想调用的时候点击label就这行这个控件的click所绑定的事件,请问应该怎么实现啊?谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 编译成DLL 调用完全可以的呀,你是不是没写过自定义控件呀。访问我的博客 程序员日记 http://www.ideaext.com this.Load += new System.EventHandler(this.label_Click); 我自定义一个控件(MyControl),里面有个一label,我想单击这个MyControl里的label时,执行MyControl的Click绑定的事件,请问应该怎么实现啊?谢谢大侠! 自定义控件继承Control类,一般都要自己实现 OnPaint 方法来绘制外观的。按你的要求,MyControl 里边又放了一个Control:label,我想你应该用 用户控件,它继承UserControl类。新建一个类,复制下面代码,即可生成所需功能控件。using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Text;using System.Windows.Forms;namespace WindowsApplication3{ public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(20, 27); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(41, 12); this.label1.TabIndex = 0; this.label1.Text = "click this "; this.label1.Click += new System.EventHandler(this.label1_Click); // // UserControl1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.label1); this.Name = "UserControl1"; this.Click += new System.EventHandler(this.UserControl1_Click); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1; private void label1_Click(object sender, EventArgs e) { UserControl1_Click(sender, e); } private void UserControl1_Click(object sender, EventArgs e) { MessageBox.Show("control is click"); } }} 可能是我说的不是很清楚,我自定义一个控件(MyControl),里面有个一label!在用的时候,我把自定义控件(MyControl)拖到窗体上,但是我怎么才能绑定label的Click事件啊?谢谢! 初学者问题 关于DataGridVie问题? 问个数据库的简单问题 如何找出指定字符串中的特定字符串? 高分再次求助:COM+错误: 此事务已明地或暗地被确认或终止]”。在线等 ...... C#的timer事件怎么用? 请问如何在winform上划出一条直线 文本框为什么可以输入中文呢? 请问 动态 创建list,该怎么 写 根据不同用户类型登陆的问题,求教大神~~ C# 判断一个URL是否为文件下载 DataGridView添加ComboBox后,编辑时选择是没有问题,但检索数据时就显示为空白。何解?
按你的要求,MyControl 里边又放了一个Control:label,
我想你应该用 用户控件,它继承UserControl类。
新建一个类,复制下面代码,即可生成所需功能控件。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication3
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent(); } private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(20, 27);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 0;
this.label1.Text = "click this ";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// UserControl1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.label1);
this.Name = "UserControl1";
this.Click += new System.EventHandler(this.UserControl1_Click);
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1; private void label1_Click(object sender, EventArgs e)
{
UserControl1_Click(sender, e);
} private void UserControl1_Click(object sender, EventArgs e)
{
MessageBox.Show("control is click");
}
}
}