2010-08-14 127 views
0

我的框架上有许多按钮,我想通过显示背景颜色来显示选择哪一个按钮。唯一的问题是,只有当鼠标悬停在按钮上时,才会显示此背景颜色,否则该按钮将变为白色。当鼠标没有悬停时,按钮背景颜色丢失

重写MouseEnter和MouseLeave事件并没有帮助。

按钮从标准报Windows窗体按钮,并具有以下方法来显示继承的,如果它的选择:

public void SetFocus(bool focused) 
{ 
    if (focused) 
     this.BackColor = SelectColor; 
    else this.BackColor = color; 
} 

的SelectColor是一个静态的黄色(指示按钮被选中),颜色为存储在类中的私有颜色保持按钮在未被选中时的颜色。

有谁知道如何显示背景颜色,即使没有悬停在按钮上?

回答

0

创建从Button派生新类:

class MyButton : Button 
{ 
    public MyButton() : base() 
    { 
     this.BackColor = System.Drawing.Color.AntiqueWhite; 
    } 

    protected override void OnMouseEnter(EventArgs e) 
    { 
     this.BackColor = System.Drawing.Color.Blue; 
     base.OnEnter(e); 
    } 

    protected override void OnMouseLeave(EventArgs e) 
    { 
     this.BackColor = System.Drawing.Color.AntiqueWhite; 
     base.OnLeave(e); 
    } 
} 

,然后使用上的形式对我的作品。您必须以编程方式添加按钮或编辑.designer.cs文件。

显然用你的值替换我的硬编码颜色。