Transparent TextBox

Lance James 371 Reputation points
2021-04-26T22:14:42.837+00:00

Using VS, I created a Windows form and added a textbox. I need the textbox to be transparent.

textbox.backcolor = Color.Transparent  

The Build works fine but a runtime error occurs indicating the control is not capable of this. I have seen another method using SetStyles but that also produces errors.

Does someone have a current implementation that works?

Here is MS Doc describing transparency:

Interesting to me is that in VS, the Backcolor property only shows colors from which one chooses. It does not have Transparent that I can see (no pun intended).

Regards,
Lance

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-04-26T22:22:58.09+00:00

    Try the following

    public class TransparentBackgroundTextBox : TextBox  
    {  
        public TransparentBackgroundTextBox()  
        {  
      
            SetStyle(ControlStyles.SupportsTransparentBackColor |  
                     ControlStyles.OptimizedDoubleBuffer |  
                     ControlStyles.AllPaintingInWmPaint |  
                     ControlStyles.ResizeRedraw |  
                     ControlStyles.UserPaint, true);  
            BackColor = Color.Transparent;  
        }  
      
        public sealed override Color BackColor  
        {  
            get => base.BackColor;  
            set => base.BackColor = value;  
        }  
    }  
    

    ---
    91414-kpmvp.png

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Lance James 371 Reputation points
    2021-04-26T23:00:30.533+00:00

    Thank you for the quick response. I will give this a try in the morning.

    Is this property not available to set using the basic drag & drop TextBox from the VS toolbox?

    Regards,
    Lance


  2. Lance James 371 Reputation points
    2021-04-27T01:11:37.303+00:00

    Fair enough and I guess I already knew the answer to that question. I meant no disrespect.

    I appreciate your help.

    Best Regards,
    Lance


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.