Change property of multiple Uttons simultaneously

Asked

Viewed 690 times

5

Hello, I am developing a project of a dental clinic in c# in which uses several buttons inside a picturebox to select the teeth, so I decided to put several Buttons with certain opacity and the result was very satisfactory, the problem is that the opacity properties of a button can only be set in the code itself and not in the VS properties menu. So for that I have to relate all Buttons to picturebox, set the Location of each and set its color and opacity, I wonder if I can at least set the color and opacity of the Buttons at once in just a few lines of code instead of repeating the same for each button.

Follow a piece of what I did as an example, in total would be 32 Buttons, would be a code I believe I 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);

2 answers

2


Place 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 the same would be derived from Button and set properties with 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);
  • That’s what I’m looking for, thank you very much!!!!

  • in this last part of the second suggestion, where do I put button1 = etc? where do I put it from the error "Inaccessible due to its Protection level"

  • @Gabrielarruda Make sure your class/field is public and the builder is also

  • I have a question, should it create a new button in the form instead of using the existing button1 in the form? when executing button properties are not applied and it creates a new random button.

  • You have to find your boot on toolbox. Usually this is only possible if you have a constructor with no arguments, so the button appears on toolbox have to build the project. This is why I gave the first solution, I consider it a little simpler.

  • Um, but this is something I don’t quite understand how this works, is there any place that shows it? the friend from the other post said the same thing but I didn’t understand.

  • @Gabrielarruda I hope my question helps and response

  • helped a lot yes Bruno, thanks a lot for the help and patience!!!! the same question and that it did not appear in my Toolbox, if not, would not be having all this doubt, but for that if I do not find out I open another post, vlw!!!

Show 3 more comments

2

You can create a class that you inherit from button, and implement another interface, and then just give foreach us this.Controls.. something like that:

public interface iDentes{}

public class ButtonDentes : Button, iDentes {}

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

Then in your form just go through the this.Controls

foreach (Control c in this.Controls)
{
    if (c is ButtonDentes)
    {
        ((ButtonDentes)c).propriedade = xxxx;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.