0

I tried looking through a few questions on here already but none seemed to fit.

This is what i've tried:

listBox1.BackColor = Color.FromArgb(85, 200, 200, 200);

But at runtime, there's an error. It states that the component doesn't support transparency. I'm asking on here because there could be a workaround. If anyone could help, that'd be great. Thanks in advance!

fishe
  • 21
  • 5
  • 2
    Is this Winforms or WPF? Please tag accordingly. – Sach Aug 09 '19 at 17:47
  • @Sach Sorry i'm using WinForms. – fishe Aug 09 '19 at 17:52
  • This might help: https://stackoverflow.com/questions/10586322/why-am-i-getting-control-does-not-support-transparent-background-colors – Sach Aug 09 '19 at 18:00
  • No, this doesn't help. Nothing does. – TaW Aug 09 '19 at 18:11
  • @Sach I've tried all of the solutions there, none work. Using the SetStyles method and attempting to bypass it by using FromHtml both will give the same error. – fishe Aug 09 '19 at 18:19
  • try this value. adjust as needed Color.FromArgb(128, 0, 0, 255) – jmag Aug 09 '19 at 18:34
  • @jmag Still the same error. – fishe Aug 09 '19 at 18:39
  • @user11633402 Windows Forms controls do not support true transparency. The background of a transparent Windows Forms control is painted by its parent. If it does, it is worth to try listBox1.BackColor = Color.Transparent; – jmag Aug 09 '19 at 18:41
  • You can check out how this guy did it. https://www.codeproject.com/Articles/20758/Alpha-Blended-Windows-Forms – jmag Aug 09 '19 at 18:46
  • @jmag I just want it to show the background a little, like this: https://i.vgy.me/wFRo2j.png – fishe Aug 09 '19 at 18:52
  • @user11633402 then this is definitely what you need.https://www.codeproject.com/Articles/20758/Alpha-Blended-Windows-Forms – jmag Aug 09 '19 at 19:11
  • @jmag I really don't understand anything in the article :/ – fishe Aug 09 '19 at 19:33
  • You can workaround by changing to ListView and setting a suitable BackgroundImage. – TaW Aug 09 '19 at 20:09
  • @user11633402 I think he set the opacity lower. this.SetOpacity(0.75); – jmag Aug 09 '19 at 20:13

1 Answers1

2

I suggest going for a ListView in Details View instead.

This is a more modern control, much more powerful and also more supportive when it comes to adding some extra styling..

ListView has a BackgroundImage which alone may be good enough. It doesn't support transparency, though.

But with a few tricks you can make it fake it by copying the background that would shine through..:

void setLVBack(ListView lv)
{
    int alpha = 64;
    Point p1 = lv.Parent.PointToScreen(lv.Location);
    Point p2 = lv.PointToScreen(Point.Empty);
    p2.Offset(-p1.X, -p1.Y );
    if (lv.BackgroundImage != null) lv.BackgroundImage.Dispose();
    lv.Hide();
    Bitmap bmp = new Bitmap(lv.Parent.Width, lv.Parent.Height);
    lv.Parent.DrawToBitmap(bmp, lv.Parent.ClientRectangle);
    Rectangle r = lv.Bounds;
    r.Offset(p2.X, p2.Y);
    bmp = bmp.Clone(r, PixelFormat.Format32bppArgb);
    using (Graphics g = Graphics.FromImage(bmp))
    using (SolidBrush br = new SolidBrush(Color.FromArgb(alpha, lv.BackColor)))
    {
        g.FillRectangle(br, lv.ClientRectangle);
    }
    lv.BackgroundImage = bmp;
    lv.Show();
}

A few notes:

  • I hide the listview for a short moment while getting the background pixels
  • I calculate an offset to allow borders; one could (and maybe should?) also use SystemInformation.Border3DSize.Height etc..
  • I crop the right area using a bitmap.Clone overload
  • finally I paint over the image with the background color of the LV, green in my case
  • you can set the alpha to control how much I paint over the image
  • Also note that I dispose of any previous image, so you can repeat the call when necessary, e.g. when sizes or positions change or the background etc..
  • The ListView overlaps a PictureBox (left) but sits on a TabPage with an image of its own.

Result:

enter image description here

TaW
  • 53,122
  • 8
  • 69
  • 111
  • This is a very good solution. But I have a question though! When I use this solution I get a dragging issue, even though my `listview` item width is not long to drag!! Can you please help me with that? – StackUseR May 12 '20 at 14:52
  • Can you explain what you mean by 'dragging issue' ? – TaW May 12 '20 at 14:54
  • Hi TaW. Thanks for reply. What I meant was, when the `listview` is created, it creates a scroll bar at bottom. No matter even if the content of the columns fit in to the default column width, there is still a option to scroll. – StackUseR May 15 '20 at 08:39
  • Hm, I don't see how it would do that; also: My screenshot doesn't have that. So, I assume that your ListView has some other reason to show the scrollbar..? – TaW May 15 '20 at 08:46
  • 1
    Sorry my bad! I forgot to call the `autosizecolumn` method after populating the `listview`. thanks for the support anyway:) – StackUseR May 15 '20 at 10:55