首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c# form编程TextBox怎么具有MouseEnter事件

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

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 ")]
public class ActivetorDesigner : System.ComponentModel.Design.ComponentDesigner


{
private DesignerActionListCollection actionLists;

public override DesignerActionListCollection ActionLists
{
get
{
if (actionLists == null)
{
actionLists = new DesignerActionListCollection();
actionLists.Add(new ActivetorDesignerActionList(this.Component));
}

return actionLists;
}
}
}
/// <summary>
/// Designer ActionList derived class defines smart tag entries and resultant actions
/// </summary>
public class ActivetorDesignerActionList : System.ComponentModel.Design.DesignerActionList
{
private Activetor _component;
private DesignerActionUIService _desingerActionUISvc = null;
/// <summary>
/// The Constructor associates the Control with the smart tag list
/// </summary>
/// <param name= "component "> </param>
public ActivetorDesignerActionList(IComponent component)
: base(component)
{
this._component = component as Activetor;

//Cache a reference to DesignerActionUIService, so the DesignerActionList can be refreshed
this._desingerActionUISvc = GetService(typeof(DesignerActionUIService)) as DesignerActionUIService;
}

private PropertyDescriptor GetPropertyByName(string proName)
{
PropertyDescriptor prod = TypeDescriptor.GetProperties(_component)[proName];
if (prod == null)
{
throw new ArgumentException( "Matching Activetor property not found! ", proName);
}
else
return prod;
}

public Color NullColor
{
get { return _component.NullColor; }
set
{
GetPropertyByName( "NullColor ").SetValue(_component, value);
}
}

public Color DefaultColor
{
get { return _component.DefaultColor; }
set
{
GetPropertyByName( "DefaultColor ").SetValue(_component, value);
}
}
/// <summary>
/// Just a Test Method Item for DesignerActionItemCollection
/// </summary>
public void ShowMessage()
{
System.Windows.Forms.MessageBox.Show( "Test... ");
}
/// <summary>
/// Implementation of this abstract method creates smart tag items, associates their targets,
/// and collects into list
/// </summary>
/// <returns> </returns>
public override DesignerActionItemCollection GetSortedActionItems()
{
DesignerActionItemCollection items = new DesignerActionItemCollection();

//Define static section header entries
items.Add(new DesignerActionHeaderItem( "Appearance Category ", "Appearance "));
items.Add(new DesignerActionHeaderItem( "Information Category ", "Information "));

if (_component != null)
{
items.Add(new DesignerActionPropertyItem( "NullColor ", "Null Color ", "Appearance ",
"BackColor of the Control if its Text is Null "));


items.Add(new DesignerActionPropertyItem( "DefaultColor ", "Default Color ", "Appearance ",
"Default BackColor of the Control if its Text isn 't Null "));
}

items.Add(new DesignerActionMethodItem(this, "ShowMessage ", "Show Message ", "Information "));

return items;
}
}
[解决办法]
可以根据鼠标相对位置来判断是在内部还是其他地方,设置一个BOOL值,在内部时设置为1,那么在mouseleave事件中判断这个BOOL值是否为1,为1则不触发,你明白了吗?

热点排行