110,539
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 自定义控件事件传递
{
public partial class labFYID : UserControl
{
//自定义一个事件
public event EventHandler myhandler;
public labFYID()
{
InitializeComponent();
this.MouseHover += new EventHandler(UcSecond_MouseHover);
}
void UcSecond_MouseHover(object sender, EventArgs e)
{
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.BackColor = Color.Blue;
if (myhandler !=null )
myhandler (this,e );
}
}
}
自定义控件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 自定义控件事件传递
{
public partial class UcParent : UserControl
{
public UcParent()
{
InitializeComponent();
//先手动放置控件方法
//labFYID1.myhandler += new EventHandler(labFYID1_myhandler);
}
void labFYID1_myhandler(object sender, EventArgs e)
{
//this.BackColor = Color.Red;
//this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
}
private void UcMain_Load(object sender, EventArgs e)
{
//动态加载控件方法
labFYID mylab = new labFYID();
mylab.myhandler += new EventHandler(mylab_myhandler);
this.Controls.Add(mylab);
mylab.Show();
}
void mylab_myhandler(object sender, EventArgs e)
{
this.BackColor = Color.Red;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
}
}
}
我试了可以,你看看可以吗
this.MouseHover += (ss, ee) => { f(); };
...
private void f()
{
//你想做什么
}