Asmo
Asmo

Reputation: 241

C# Windows Form Application Transparent button

I'm new to C#. I'd like to create an invisible button, but they are click-able in C# windows form application. Is there a way? I tried BackColor to Transparent, but that does not change the fact that it is transparent

Upvotes: 16

Views: 62875

Answers (6)

Baris
Baris

Reputation: 93

The answers given just make the background color of the control you want to make transparent the same as the background color of its parent. It's not true transparency and windows forms doesn't support true transparency.

Windows Forms controls do not support true transparency. The background of a transparent Windows Forms control is painted by its parent.

https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-give-your-control-a-transparent-background?view=netframeworkdesktop-4.8&redirectedfrom=MSDN

Upvotes: 2

Michael Kuczmarski
Michael Kuczmarski

Reputation: 61

Setting the background property of the button to transparent will still leave a border. If you want a completely transparent button, do one of 2 things:

Create a transparent panel and assign a method to the Click event

or preferably

Create a custom UserControl that is filled with only BackColor (set to transparent) and assign method to Click event.

public class Invisible_Button : UserControl
{
    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);
        this.Cursor = Cursors.Hand;
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.FillRectangle(new SolidBrush(this.BackColor), 0, 0, this.Width, this.Height);
    }
}

Upvotes: 0

user2230410
user2230410

Reputation: 71

buttonLink.FlatStyle = FlatStyle.Flat; 
buttonLink.BackColor = Color.Transparent;
buttonLink.FlatAppearance.MouseDownBackColor = Color.Transparent;
buttonLink.FlatAppearance.MouseOverBackColor = Color.Transparent;

Upvotes: 7

Pritam Routh
Pritam Routh

Reputation: 396

Its simple try this.

Click the button that you want to make transparent. Select FlatStyle from Properties and set it to popup Now change the BackColor property to Transparent.

This will make the button transparent.

However if you want to make it transparent over a PictureBox this method wont work..

It works only on normal backgrounds and background images. Hope it works....

Upvotes: 38

decyclone
decyclone

Reputation: 30840

Reference:

Original article and code can be found at:

Displaying a ToolTip when the Mouse Hovers Over a Disabled Control

@ CodeProject by tetsushmz

Code:

public class TransparentSheet : ContainerControl
{
    public TransparentSheet()
    {
        // Disable painting the background.
        this.SetStyle(ControlStyles.Opaque, true);
        this.UpdateStyles();

        // Make sure to set the AutoScaleMode property to None
        // so that the location and size property don't automatically change
        // when placed in a form that has different font than this.
        this.AutoScaleMode = AutoScaleMode.None;

        // Tab stop on a transparent sheet makes no sense.
        this.TabStop = false;
    }

    private const short WS_EX_TRANSPARENT = 0x20;

    protected override CreateParams CreateParams
    {
        [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
        get
        {
            CreateParams l_cp;
            l_cp = base.CreateParams;
            l_cp.ExStyle = (l_cp.ExStyle | WS_EX_TRANSPARENT);
            return l_cp;
        }
    }
}

Explanation:

What you need to do is use the given control as an overlay on your disabled TextBox (that you mentioned in one of your comments). Sibscribe to the overlay control's Click event and you have yourself a click on a disabled control.

I strongly recommend against this approach and feel it is kind of a hack. You really should look for an alternative approach instead of having to use a disabled control with an overlay control on top of it.

Maybe a different UI or atleast wrap it up in a UserControl to isolate this messy logic.

Upvotes: 1

nunespascal
nunespascal

Reputation: 17724

Did you try button.Visible = false? If all you want is to hide it, this will do the job.

Upvotes: -1

Related Questions