2015-10-14 40 views
0

Im在WinForms中工作我在窗体上有4个按钮。我希望能够将鼠标悬停在其上并将FlatStyle从Flat更改为System。悬停:使用Flatyle属性更改按钮C#

当您将鼠标悬停在系统样式上时,我的代码将所有按钮转换为系统样式,这并非我所想的。

所有按钮应保持平直,直到您将鼠标悬停在它们上面。如果将鼠标悬停关闭按钮,它应该变回平按钮

private void All_Button_Hover_MouseHover(object sender, EventArgs e) 
    { 
     btn_Back.FlatStyle = FlatStyle.System; 
     Btn_Forward.FlatStyle = FlatStyle.System; 
     btn_Print.FlatStyle = FlatStyle.System; 
     btn_Open.FlatStyle = FlatStyle.System; 
    } 

enter image description here

回答

2

这里是你能怎么处理这个建议。

您已经在单个事件方法中设置所有按钮,这很好。由于触发事件的按钮存储在sender,你可以只使用:

private void All_Button_Hover_MouseHover(object sender, EventArgs e) 
{ 
    ((Button)sender).FlatStyle = FlatStyle.System; 
} 

要更改按钮回到原来的FlatStyle.Flat风格,你可能会想所有的MouseLeave事件的订阅方法:

private void All_Button_Hover_MouseLeave(object sender, EventArgs e) 
{ 
    ((Button)sender).FlatStyle = FlatStyle.Flat; 
} 
+0

我实际上没有其他方法将它们翻转回原始样式,我仍然是编程新手。 – taji01

+0

我正在考虑放置一个if-else语句,如'if(Back_button.Hover)'做一些'''Else((Button)sender).FlatStyle = FlatStyle.System; – taji01

+0

不客气。 :) –