2010-08-26 87 views
0

我正在开发一个项目在C#Windows应用程序(赢得窗体),因为我需要创建一个函数来更改所有按钮的背景颜色使用按钮鼠标在单个Win窗体事件C#Windows应用程序

+0

您正在使用的WinForms或WPF?什么是“lidos”? – Tokk 2010-08-26 13:35:03

+2

你将需要更多地解释这个问题。按钮是否仅在您的应用中更改?或者你是否在更换其他应用程序的按钮? – 2010-08-26 13:37:36

+0

自己明确提及这是Windows应用程序,如果这是WPF控件意味着我需要编写WPF控件 – 2010-08-26 13:46:37

回答

1

可更换式按钮的所有控制:钩

for (int i = 0; i < Controls.Count; i++) 
      if (Controls[i] is Button) Controls[i].BackColor = Color.Blue; 

例子:

MouseEnter += new EventHandler(delegate(object sender, EventArgs e) 
    { 
     SetButtonColour(Color.Blue); 
    }); 

MouseLeave += new EventHandler(delegate(object sender, EventArgs e) 
    { 
     SetButtonColour(Color.Red); 
    }); 

public void SetButtonColour(Color colour) 
    { 
     for (int i = 0; i < Controls.Count; i++) 
      if (Controls[i] is Button) Controls[i].BackColor = Color.Blue; 
    } 
+0

罚款,这是非常有用的 – 2010-11-09 11:35:42

0

假设你只是改变你自己的应用程序,这并不难。

在鼠标事件,在窗体控件属性和那些按钮所有项目只是循环,改变背景色。您需要编写递归函数来查找所有按钮,因为Panel(或GroupBox等)包含其所有控件的Controls属性。

+0

一样,只有我在做,所有的按钮都放在组框里面,我已经tryed,对于文本框下面的代码,但使用改变背景色鼠标悬停事件是对我有些临界 公共静态无效Fungbstyle(分组框中gbsty){ gbsty.BackColor = Color.LightSteelBlue; gbsty.Cursor = Cursors.AppStarting; 的foreach(控制CNN在gbsty.Controls){ 如果(CNN是文本框){ cnn.BackColor = Color.LightCyan; cnn.Cursor = Cursors.PanNW; cnn.Font = new Font(cnn.Font,FontStyle.Italic); cnn.Width = 156; }}} – 2010-08-26 13:59:20

0

事情是这样的:

public partial class Form1 : Form 
{ 
    Color defaultColor; 
    Color hoverColor = Color.Orange; 

    public Form1() 
    { 
     InitializeComponent(); 
     defaultColor = button1.BackColor; 
    } 

    private void Form1_MouseHover(object sender, EventArgs e) 
    { 
     foreach (Control ctrl in this.Controls) 
     { 
      if (ctrl is Button) 
      { 
       ctrl.BackColor = hoverColor; 
      } 
     } 
    } 

    private void Form1_MouseLeave(object sender, EventArgs e) 
    { 
     foreach (Control ctrl in this.Controls) 
     { 
      if (ctrl is Button) 
      { 
       ctrl.BackColor = defaultColor; 
      } 
     } 
    } 
}