Search code examples
c#eventsbuttoncolorsflowlayout

FlowLayout Controls Events


There are many buttons in FlowLayout. When I click on the buttons, I want the normal colors to change. When I click the first time, the color of the button changes. There is no problem here but when I click on another button for the second time, the button I clicked before is not restored. how can i do ?

 int j =1;
    foreach (...)
                 {
                     BunifuFlatButton newButton = new BunifuFlatButton();
                     {
                         newButton.Name = string.Format("Button{0}", j);
                         flowLayoutPanel1.Controls.Add(newButton);
                         newButton.Click += NewButtonOnClick;
                         j++;
                     }

                 }




  private void NewButtonOnClick(object sender, EventArgs eventArgs)
        {
            BunifuFlatButton btn = (BunifuFlatButton)sender;
            btn.Normalcolor = Color.FromArgb(37, 66, 80);
        }

Solution

  •   List<BunifuFlatButton> btnss = new List<BunifuFlatButton>();
        private bool pressed = false;
        private void NewButtonOnClick(object sender, EventArgs eventArgs)
        {
            BunifuFlatButton btn = (BunifuFlatButton)sender;
            if (pressed)
            {
                btnss[0].Normalcolor = Color.FromArgb(37, 37, 37);
                btn.Normalcolor = Color.FromArgb(37, 66, 80);
                pressed = false;
                btnss.Clear();
                btnss.Add(btn);
            }
            else
            {
                if (btnss.Count != 0)
                {
                    btnss[0].Normalcolor = Color.FromArgb(80, 80, 80);
    
                }
                btn.Normalcolor = Color.FromArgb(37, 66, 80);
                btnss.Clear();
                btnss.Add(btn);
                pressed = true;
            }
    

    }