0

Recently I was working on .NET Framework 4.8 and decided to change it to .NET 6

At .NET Framework 4.8 I was using the code below to draw a rectangle around the Listbox and to make the event DrawItem:

using (Pen penBorder = new Pen(Color.FromArgb(66, 66, 66), 2))
            {
                penBorder.Alignment = PenAlignment.Inset;
                graph.DrawRectangle(penBorder, ModifiersLB.Location.X - 2, ModifiersLB.Location.Y - 2, ModifiersLB.Width + 4F, ModifiersLB.Height + 4F);
            }

private void LB_DrawItem(DrawItemEventArgs e, ListBox LB)
        {
            Color backgroundColor = Color.FromArgb(50, 50, 50);
            Color horizontalColor = Color.FromArgb(100, 100, 100);

            if (e.Index >= 0)
            {
                SolidBrush sb = new SolidBrush(((e.State & DrawItemState.Selected) == DrawItemState.Selected) ? horizontalColor : backgroundColor);
                e.Graphics.FillRectangle(sb, e.Bounds);
                string text = LB.Items[e.Index].ToString();
                SolidBrush tb = new SolidBrush(e.ForeColor);
                e.Graphics.DrawString(text, e.Font, tb, LB.GetItemRectangle(e.Index).Location);
            }
        }

The problem is this code is only supported by Windows.

I decided to make this question because I was receiving a warning. I never really intended to make my app supported on all platforms. I just wanted to ask if there is a better way to do what I was doing so that I don't get the warning.

Is there a better method to do this code or is there a ListBox replacement that I could use?

Filipe
  • 146
  • 10
  • 6
    _"The problem is this code is only supported by Windows."_ - If you want to go out of Windows you have a whole different level of problem: You might not be able to use WinForms _at all_. So, you may be forced to switch to a [completely different UI Framework](https://dotnet.microsoft.com/en-us/apps/maui). – Fildor Jan 05 '23 at 09:18
  • Doesn't ListBox have a Border that can be used instead of actually drawing a Box around it manually? – Fildor Jan 05 '23 at 09:26
  • you can't change its color @Fildor – Filipe Jan 05 '23 at 09:27
  • Have a look at this ("Solution 2"): https://stackoverflow.com/a/69632935/982149 – Fildor Jan 05 '23 at 09:29
  • @Fildor, I think that's for 4.7 and it's more or less what I'm currently doing. The code in the description works well, I'm just receiving a warning. – Filipe Jan 05 '23 at 09:36
  • 2
    To get rid of the warning you just need to adjust the TFM in csproj like `net6.0-windows`. Btw. `System.Drawing.Common` still [supports](https://stackoverflow.com/a/71541052/5114784) Unix OSes if you really want, though this will not work anymore in .NET 7 and above. But as this seems to be a WinForms app it doesn't matter anyway as WinForms is practically supported only on Windows. Actually WinForms apps targeting the .NET Framework can be executed on Linux by Mono but it has quite a few limitations and compatibility issues. – György Kőszeg Jan 05 '23 at 11:26

0 Answers0