-1

Problem:
I've got a ListView with an Image in the first column. But the problem is that the Image isn't positioned at the left but weird in the center, so that you can't see the full username.
Question:
So is there a way to position the image at the left, so that you can see the username correctly?
Code:
The Code that adds the ListViewItem

public void AddMessage(string from, string message)
        {
            ListViewItem item = new ListViewItem(new string[] {from, DateTime.Now.GetDateTimeFormats()[7], message});
            item.ImageIndex = 0;
            listView1.Items.Add(item);
        }

Screenshot

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
  • A ListViewItem with Image and Text is usually aligned to the Left (or to the right, if RTL and RTL layout are active). Are you sure your Images don't have a transparent / white background on their left side? – Jimi Jun 13 '21 at 18:36
  • I'm 100% sure, as you can see on [this screenshot](https://imgur.com/a/4A2fTRK). – CraftingDragon007 Jun 13 '21 at 18:45
  • How could I see anything if the Image has transparent / white background (that extends to the left of the black area)? Have you tried to use another Image, add it to another ImageList (set to `ImageSize = 16x16` and `ColorDepth = Depth32Bit`), set the new ImageList to the `SmallImageList` of the Control? What happens when you do this in the Designer? – Jimi Jun 13 '21 at 18:49
  • As you can see on [this screenshot](https://imgur.com/a/V5yYfjT), I've the same problem. – CraftingDragon007 Jun 13 '21 at 18:58
  • Sorry, I cannot reproduce it in any way -- Use [this web resource](https://icoconvert.com/) to convert your Image to an Icon. Upload your Image, then go to `Step 4`, select all sizes, download your new Icon and add this to the ImageList. Set the `ColorDepth` to `Depth32Bit` beforehand and any size you prefer (it can be changed at Design-Time to any other Size). – Jimi Jun 13 '21 at 19:30
  • It still doesn't works – CraftingDragon007 Jun 13 '21 at 19:47
  • Is there something you're not saying about that ListView? Is that the standard WinForms' ListView? -- Make a Test, add a new one (default properties) to a clean Form of a new Project (default Template), set the ImageList as described, add 3 Columns in the Designer, set `View = Details` and set the ImageList to the `SmallImageList` property. At Run-Time, add your ListViewItem. How does it look like? -- As mentioned, I cannot reproduce the behavior described. -- BTW, don't name a Parameter (or any variable /field) `from`, that's a reserved keyword. – Jimi Jun 13 '21 at 20:31

1 Answers1

0

Here you can try two ways, One

listView1.Columns["columnName"].TextAlign = HorizontalAlignment.Left;

or you can use event DrawSubItem in this way

private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    TextFormatFlags flags = TextFormatFlags.Left;
    if (e.ColumnIndex == 0)
    {
        flags = TextFormatFlags.Right;
    }

    e.DrawText(flags);
}