Change ownership of multiple buttons simultaneously

5
Hello, I'm developing a project for a dental clinic in C # which uses several buttons inside a picturebox to select the teeth, so I decided to put several buttons with a certain opacity and the result was very satisfactory, the problem and that the opacity properties of a button can only be set in the code itself and not in the VS properties menu. So for this I have to relate all the buttons to picturebox, set the location of each one and set its color and opacity, in that, is a very extensive code. I would like to know if I can at least set the color and opacity of the buttons of a only in a few lines of code instead of repeating the same for each button.

Here's a piece of what I did as an example, in total there would be 32 buttons, it would be a code I believe unnecessarily large.

 button1.BackColor = Color.Transparent;
        button1.FlatAppearance.MouseOverBackColor = Color.FromArgb(150, Color.LightBlue);
        button1.Parent = pictureBox1;
        button1.Location = new Point(28, 90);
        button2.BackColor = Color.Transparent;
        button2.FlatAppearance.MouseOverBackColor = Color.FromArgb(150, Color.LightBlue);
        button2.Parent = pictureBox1;
        button2.Location = new Point(55, 90);
        button3.BackColor = Color.Transparent;
        button3.FlatAppearance.MouseOverBackColor = Color.FromArgb(150, Color.LightBlue);
        button3.Parent = pictureBox1;
        button3.Location = new Point(80, 90);
        button4.BackColor = Color.Transparent;
        button4.FlatAppearance.MouseOverBackColor = Color.FromArgb(150, Color.LightBlue);
        button4.Parent = pictureBox1;
        button4.Location = new Point(112, 90);
    
asked by anonymous 26.10.2016 / 12:19

2 answers

2

Put your buttons in an array and scroll through the array. But you can only do this for properties with equal values:

var botoes = new[]{button1, button2, button3, button4 /*...*/};
foreach(var botao in botoes){
    botao.Parent = pictureBox1;
    botao.BackColor = Color.Transparent;
    botao.FlatAppearance.MouseOverBackColor = Color.FromArgb(150, Color.LightBlue);
    //...
}

Another way to do this would be by deriving from Button and set properties to default values.

public class BotaoTransparente : Button{
    public BotaoTransparente(Controll parent){
        Parent = parent;
        BackColor = Color.Transparent;
        FlatAppearance.MouseOverBackColor = Color.FromArgb(150, Color.LightBlue);
    }
}

Now instead of using the "normal" buttons, use your

button1 = new BotaoTransparente(pictureBox1);
    
26.10.2016 / 12:30
2

You can create a class that inherits from button , and implement another interface, and then only give foreach to this.Controls .. something like this:

public interface iDentes{}

public class ButtonDentes : Button, iDentes {}

Use ButtonDentes in your form instead of Button (to appear the toolbox component just build in your project)

Then in your form, just scroll through the this.Controls

foreach (Control c in this.Controls)
{
    if (c is ButtonDentes)
    {
        ((ButtonDentes)c).propriedade = xxxx;
    }
}
    
26.10.2016 / 12:32