Search code examples
c#winformsbuttontransparentflowlayoutpanel

I want to make a my own button which has 2 other button inside and I want to make them transparent but transparency doesn't work


I have a button, I put 2 other buttons inside it. I want those 2 other buttons to only appear when I enter the main button with my mouse. When I enter it, I want the 2 other buttons to be half opaque and only be fully opaque when I enter one of those 2 buttons.

These buttons are inside a FlowLayoutPanel with a background image on it. This is how they look like:

enter image description here

The Buttons have a picture inside them and a text.

Here is my code:

public class MyButton : Button
{
    public MyButton()
    {
        SetStyle(ControlStyles.StandardClick | 
                 ControlStyles.StandardDoubleClick, true);

        Text = component.ProductsName;
        TextAlign = ContentAlignment.TopCenter;
        ImageAlign = ContentAlignment.TopLeft;
        Size = new Size(178, 75);

        foreach (Button item in CustomButtons())
        {
            Controls.Add(item);
        }
    }

    static Button[] CustomButtons()
    {
        Button delete = new Button();
        delete.Location = new Point(157, 1);
        delete.Size = new Size(20, 20);
        delete.MouseEnter += OnMouseEnter;
        delete.MouseLeave += DeleteOnMouseLeave;

        Button customize = new Button();
        customize.Location = new Point(delete.Left - 20, 1);
        customize.Size = new Size(20, 20);

        Button[] buttons = {delete, customize};
        return buttons;
    }

    private static void DeleteOnMouseLeave(object sender, EventArgs e)
    {
        Button btn = (Button) sender;
        btn.UseVisualStyleBackColor = true;
        btn.BackColor = Color.Transparent;
    }

    private static void OnMouseEnter(object sender, EventArgs e)
    {
        Button btn = (Button) sender;
        btn.UseVisualStyleBackColor = false;
        btn.FlatAppearance.MouseOverBackColor = Color.FromArgb(100, 
                                                       Color.Black);
    }
 }

I think I tried everything that came to my mind, I tried events and everything and the buttons never worked as I intended them to work. Any help would be appreciated! Thanks! :D


Solution

  • it seems I solved it! I only had to set Flatstyle = FlatStyle.Flat and backColor = Color.Transparent! :D

    Here is the result: exsample of output