Home > Mobile >  How to change properties just one button
How to change properties just one button

Time:02-27

Hey, I have multiple buttons.I want to reduce the code so that I can set the properties of a button, but only the one I click on will change.So that I don't click on button_1 and change all the others

 public static void SetProp()
        {
            for (int i = 0; i < buttons.Count; i  )
            {
                buttons[i].Image = Properties.Resources.test;
                buttons[i].Width = buttons[i].Image.Width;
                buttons[i].Height = buttons[i].Image.Height;
                buttons[i].ImageAlign = ContentAlignment.MiddleCenter;
                buttons[i].Text = null;
                GraphicsPath gp = new GraphicsPath();
                gp.AddEllipse(0, 0, buttons[i].Width, buttons[i].Height);
                buttons[i].Region = new Region(gp);
                gp.Dispose();
            }
        }
   private void button1_Click(object sender, EventArgs e)
        {
            SetProp();
          

        }

CodePudding user response:

Like you did, i expect you have a Click-Event for each single Button so why you don´t give the "SetProp" Method an Parameter for the Button?

CodePudding user response:

 public static void SetProp(Button button)
        {
            for (int i = 0; i < buttons.Count; i  )
            {
                button.Image = Properties.Resources.test;
                button.Width = buttons[i].Image.Width;
                button.Height = buttons[i].Image.Height;
                button.ImageAlign = ContentAlignment.MiddleCenter;
                button.Text = null;
                GraphicsPath gp = new GraphicsPath();
                gp.AddEllipse(0, 0, button.Width, button.Height);
                button.Region = new Region(gp);
                gp.Dispose();
            }
        }
 private void button1_Click(object sender, EventArgs e)
        {
            SetProp(sender as Button);
           
          
        }
  • Related