Problem with Listview C # Winform Background

I have a little problem with the list.

I can load it with list items perfectly, but when I set the background color, it does not draw the color to the end on the left side of the line. [ListViewItems are loaded by ListViewSubItems to create a grid, only the first column shows an error]. There is a narrow strip that does not show off. The width of this bar is about the same as the title bar if I had a title bar.

If you have an idea of ​​what can be done to make a wallpaper, I would like to hear it.

Now, to try a new idea, I propose ten votes for the first solution that still has me, using this awful pseudo-network mess design. [I love outdated code.]

Edit:

Here is an example that shows the problem.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        ListView lv = new ListView();

        lv.Dock = System.Windows.Forms.DockStyle.Fill;
        lv.FullRowSelect = true;
        lv.GridLines = true;
        lv.HideSelection = false;
        lv.Location = new System.Drawing.Point(0, 0);
        lv.TabIndex = 0;
        lv.View = System.Windows.Forms.View.Details;
        lv.AllowColumnReorder = true;

        this.Controls.Add(lv);

        lv.MultiSelect = true;

        ColumnHeader ch = new ColumnHeader();
        ch.Name = "Foo";
        ch.Text = "Foo";
        ch.Width = 40;
        ch.TextAlign = HorizontalAlignment.Left;

        lv.Columns.Add(ch);

        ColumnHeader ch2 = new ColumnHeader();
        ch.Name = "Bar";
        ch.Text = "Bar";
        ch.Width = 40;
        ch.TextAlign = HorizontalAlignment.Left;

        lv.Columns.Add(ch2);

        lv.BeginUpdate();

        for (int i = 0; i < 3; i++)
        {


            ListViewItem lvi = new ListViewItem("1", "2");

            lvi.BackColor = Color.Black;
            lvi.ForeColor = Color.White;

            lv.Items.Add(lvi);
        }
        lv.EndUpdate();
    }
}
+3
source share
4 answers

Oh! Now I see:}

Do you want to hack? I present to you the following:

    ...
    lv.OwnerDraw = true;
    lv.DrawItem += new DrawListViewItemEventHandler( lv_DrawItem );
    ...

void lv_DrawItem( object sender, DrawListViewItemEventArgs e )
{
    Rectangle foo = e.Bounds;
    foo.Offset( -10, 0 );
    e.Graphics.FillRectangle( new SolidBrush( e.Item.BackColor ), foo );
    e.DrawDefault = true;
}

For a more inventive and no less hacky approach, you can try using a ListView background image;)

+5
source

(Before editing ...)

I just tried setting BackColor to System.Windows.Forms.ListView, and the color applied to the control is just fine (with and without images).

Do you do any ordinary painting?

+1
source

, . , , . , lv_DrawItem . , , .

0

Better ListView (and free Better ListView Express ) allows you to customize the background image with various alignment settings (with centering, tiling, stretching, landing, snap to border / corner). Alpha transparency is also supported:

enter image description here

0
source

Source: https://habr.com/ru/post/1696844/


All Articles