using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace WindowsApplication26 { public partial class Form1 : Form { public Form1() { InitializeComponent(); SetEnterEvent(this.Controls); } void SetEnterEvent(Control.ControlCollection CC) { foreach (Control C in CC) { C.Enter += new EventHandler(C_Enter); SetEnterEvent(C.Controls); } } void C_Enter(object sender, EventArgs e) { MessageBox.Show(((Control)sender).Name + "被激活"); } } }
重写过这个属性,没有用,好像.NET调用,这个属性只是用到时候,会执行Get,SET不执行的!
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication26
{
public partial class Form1 : Form
{ public Form1()
{
InitializeComponent(); SetEnterEvent(this.Controls);
} void SetEnterEvent(Control.ControlCollection CC)
{
foreach (Control C in CC)
{
C.Enter += new EventHandler(C_Enter);
SetEnterEvent(C.Controls);
}
} void C_Enter(object sender, EventArgs e)
{
MessageBox.Show(((Control)sender).Name + "被激活");
}
}
}
完全可以实现!不过是为窗体所有控件添加了Enter事件,看还有没有别的办法!