日期:2014-05-18  浏览次数:21142 次

c# form编程TextBox如何具有MouseEnter事件?
c#   form编程,我想在鼠标进入TextBox空间使TextBox背景色改变。如何实现?  

VC#的IDE默认TextBox没有MouseEnter事件,所以无法用下面此类代码:  
this.TextBox.MouseEnter+=new   System.EventHandler(TextBox_MouseEnter);  

可以自定义TextBox控件,重写MouseEnter,但是此MouseEnter只是指TextBox周围一圈很小的区域,如果鼠标进入TextBox内部,则系统自动调用了MouseLeave事件,使背景色又变回去了(我写了MouseLeave事件的方法)。  
有没有方法使鼠标进入TextBox内部,而不触发MouseLeave事件?  

我用的是VC#2005.  
有知道的高手还望指教,不用详细代码,点到即可。谢谢


------解决方案--------------------
控件变色的一个Extender

[Designer(typeof(TextableComboBox.ActivetorDesigner))]
[ProvideProperty( "ActiveEnabled ", typeof(Control))]
public class Activetor : System.ComponentModel.Component, System.ComponentModel.IExtenderProvider
{
protected Color nullColor;
protected Color defaultColor;

protected Dictionary <Control, bool> controls;

protected System.ComponentModel.Container components;

public Activetor()
{
InitializeComponent();
}

protected void InitializeComponent()
{
this.components = new Container();
this.nullColor = Color.LightYellow;
this.defaultColor = Color.White;
this.controls = new Dictionary <Control, bool> ();
}

[Browsable(true)]
public Color NullColor
{
get { return nullColor; }
set { nullColor = value; }
}

[Browsable(true)]
public Color DefaultColor
{
get { return defaultColor; }
set { defaultColor = value; }
}

#region IExtenderProvider Members

bool IExtenderProvider.CanExtend(object extendee)
{
if ((extendee is Control) && !(extendee is Activetor))
return true;
return false;
}

#endregion

[DefaultValue(false)]
public bool GetActiveEnabled(Control ctrl)
{
if (controls.ContainsKey(ctrl))
return controls[ctrl];
else
return false;
}

public void SetActiveEnabled(Control ctrl, bool value)
{
if (value)
{
controls[ctrl] = value;
ctrl.TextChanged += new EventHandler(OnTextChanged);
OnTextChanged(ctrl, new EventArgs());
}
else
{
controls.Remove(ctrl);
ctrl.TextChanged -= new EventHandler(OnTextChanged);
}
}

protected void OnTextChanged(object sender, EventArgs e)
{
Control activeCtrl = (Control)sender;
if (activeCtrl.Text == " ")
{
activeCtrl.BackColor = nullColor;
}
else
{
activeCtrl.BackColor = defaultColor;
}
}
}
/// <summary>
/// Designer for the Activetor with support for a smart tag panel.
/// Must add reference to System.Design.dll
/// </summary>
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,Name= "FullTrust &quo