-2

//Hi, I want to write a Software and am confused about "keeping the buttom selected (i mean the background color) when button is clicked, until i click on another one"

If someone could help me, i would appreciate that so much. Thanks in advance//

namespace SoftwareUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.ForeColor = Color.LightGray;
        }

        private void button1_Leave(object sender, EventArgs e)
        {
            button1.ForeColor = Color.GhostWhite;
        }

        private void button1_MouseEnter(object sender, EventArgs e)
        {
            button1.ForeColor = Color.LightSlateGray;
        }    
    }
}
Scott Perham
  • 2,410
  • 1
  • 10
  • 20
Call Me James
  • 27
  • 1
  • 5
  • I'm not sure there is enough information here to diagnose the problem. If you want a button background colour to be set on click (as in your Click handler) and keep that colour until something else happens then remove the other mouse event handlers. – Scott Perham Jan 28 '18 at 19:11
  • Are these what you want: [How to make a button appear as if it is pressed?](https://stackoverflow.com/q/4913/3744182) and [ToggleButton in C# WinForms](https://stackoverflow.com/q/282118/3744182). – dbc Jan 28 '18 at 20:40

1 Answers1

1

Add this code for click event of all buttons:

Button b = (Button)sender;
b.BackColor = Color.LightGray;

foreach (Button bt in b.Parent.Controls.OfType<Button>())
{
    if (bt != b)
        bt.BackColor = Color.White;
}
dbc
  • 104,963
  • 20
  • 228
  • 340
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171