public partial class Form1 : Form
{
private Button[] buttonsArray = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
GetButtonsArray();
if (buttonsArray != null)
{
foreach (Button button in buttonsArray)
{
button.MouseEnter += button_MouseEnter;
button.MouseLeave += button_MouseLeave;
}
}
}
private void GetButtonsArray()
{
// This is an example to fill array of buttons.
// You have to fill array in your way, according to your task.
foreach (Control control in this.Controls)
if (control is Button)
buttonsArray = buttonsArray.Append(control as Button);
}
private void button_MouseEnter(object sender, EventArgs e)
{
if (sender is Button)
(sender as Button).BackColor = Color.Yellow;
}
private void button_MouseLeave(object sender, EventArgs e)
{
if (sender is Button)
(sender as Button).BackColor = SystemColors.Control;
}
}
public static class Extensions
{
public static T[] Append<T>(this T[] array, T item)
{
if (array == null)
return new T[] { item };
T[] result = new T[array.Length + 1];
array.CopyTo(result, 0);
result[array.Length] = item;
return result;
}
}
1条答案
按热度按时间t2a7ltrp1#
我希望这个代码示例对您有所帮助