2010-08-26 58 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; 
      } 
     } 
    } 
}