question

LanceJames-3930 avatar image
0 Votes"
LanceJames-3930 asked LanceJames-3930 commented

Transparent TextBox

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:

[1]: https://docs.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-give-your-control-a-transparent-background?view=netframeworkdesktop-4.8


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

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

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



kpmvp.png (2.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LanceJames-3930 avatar image
0 Votes"
LanceJames-3930 answered karenpayneoregon commented

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If it was available via drag-n-drop I would not have provided a custom TextBox.

0 Votes 0 ·
LanceJames-3930 avatar image
0 Votes"
LanceJames-3930 answered LanceJames-3930 commented

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

I appreciate your help.

Best Regards,
Lance

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Didn't think you were in anyway disrespecting me. I always look to what can be done well with in the box controls property-wise before digging into code like I presented.

0 Votes 0 ·

I appreciate your kind response.

Regards,
Lance

0 Votes 0 ·