tags:

views:

130

answers:

3

I am developing a project in C# Windows applications(win form) in that I need to create a function to change the back color for all the buttons that are in the single Win Form using button mouse over event

+1  A: 

Changing all controls of type Button:

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

Example of hooks:

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;
    }
Blam
A: 

Assuming you are just changing your own app, this isn't that difficult.

In the mouse over event, just loop over the Controls property of the form and for all items that are Button, change the back color. You will need to write a recursive function to find all of the buttons probably though, since a Panel (or GroupBox, etc) contains a Controls property for all of its controls.

davisoa
like that only am doing, all the buttons are placed inside the groupbox, i already tryed that for textbox as below code, but changing backcolor using mouse over event is somewhat critical to me public static void Fungbstyle(GroupBox gbsty){gbsty.BackColor = Color.LightSteelBlue;gbsty.Cursor = Cursors.AppStarting;foreach (Control cnn in gbsty.Controls){if (cnn is TextBox){cnn.BackColor = Color.LightCyan;cnn.Cursor = Cursors.PanNW;cnn.Font = new Font(cnn.Font, FontStyle.Italic);cnn.Width = 156;}}}
Lawrance Rozario
A: 

Something like this:

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;
            }
        }
    }
}
Radoslav Hristov