0

I am developing a desktop app using vb.net in Visual Studio 2019. At some point I ask the user to choose the background color of the label they want to create. In the code everything works fine, but when the user chooses a semi-transparent background, say ARGB (116,255,255,000), it skips the alpha part so that I don't get the background transparency I want. How can I resolve this?

here is how the user chose the label properties

img1

and here is a part of the code that deal with it

Str5 = "116255255000"   
Label1.BackColor = Color.FromArgb(Str5.Substring(0, 3), Str5.Substring(3, 3), Str5.Substring(6, 3), Str5.Substring(9, 3))

and here is the result I get

img2

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • That code indicates that you have `Option Strict Off`, which you should remedy immediately. Set it `On` in the project properties and it will likely flag a number of places where you are playing fast and loose with data types, making your code less efficient and possibly masking errors that could cause a crash at run time. You should also set it `On` in the VS options, so that it is `On` by default for all future projects. – jmcilhinney Sep 07 '21 at 12:03
  • You can have a completely translucent / transparent Control, but you have to handle the transparency yourself. An example: [Translucent circular Control with text](https://stackoverflow.com/a/51435842/7444103) – Jimi Sep 07 '21 at 14:55

1 Answers1

0

It's working exactly as it is supposed to. Add a BackgroundImage to your form and you'll see that.

Your problem is not that the BackColor is not transparent but, rather, that Windows Forms only supports a fudged form of transparency. When a control is transparent, you don't actually see through it. What actually happens is that an image of it's parent behind it is created and that is shown in the background of the control. It's only the parent itself that is included in this image though, and not any other sibling controls. If you do as I suggested then you'll see that, because you will still see through to the parent where you expect see that other control.

There's no real solution to this issue using WinForms controls. The alternative is to not use a Label but, instead, draw the text and its background colour using GDI+. You can then draw on both the form and the other control(s) and you'll be able to see through to everything you expect.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46