简体   繁体   English

C#Listview添加带有图像和文本的项目,并将文本对齐到左侧

[英]C# Listview adding item with image and text, and align the text to left

I'm trying to create some test programs, just for fun to learn c#, and I came to something I really couldn't figure out. 我正在尝试创建一些测试程序,只是为了学习c#的乐趣,我找到了一些我真的无法弄清楚的东西。

I wanted to add an image to an item in a listview. 我想将图像添加到列表视图中的项目。 I found an article on Stackoverflow explaining how to do this, and it worked. 我在Stackoverflow上发现了一篇解释如何执行此操作的文章,并且它有效。 However, I cannot add extra text to the item. 但是,我无法在项目中添加额外的文字。 I'd like to have an image with text next to it. 我希望旁边有一个带文字的图像。 My current code: 我目前的代码:

ImageList Imagelist = new ImageList();
private void Form1_Load(object sender, EventArgs e)
    {
        //retrieve all image files
        String[] ImageFiles = Directory.GetFiles(@"C:\test");
        foreach (var file in ImageFiles)
        {
            //Add images to Imagelist
            Imagelist.Images.Add(Image.FromFile(file));
        }
        //set the amall and large ImageList properties of listview
        listView1.LargeImageList = Imagelist;
        listView1.SmallImageList = Imagelist;

        listView1.Items.Add(new ListViewItem() { ImageIndex = 0});
    }

Obviously it will only add one image, it's meant to. 显然它只会添加一个图像,这意味着。 Anyway, how would I enter text next to the image? 无论如何,我如何在图像旁边输入文字? For example 例如

listView1.Items.Add(new ListViewItem() { ImageIndex = 0} "Image 1");

The text must be located behind the image. 文本必须位于图像后面。

I've also got a second question. 我还有第二个问题。 I do not have columns (adding them doesn't do the trick either). 我没有列(添加它们也不起作用)。 I would like to have the item aligned to the left side of the ListView. 我想让项目与ListView的左侧对齐。 How could I do this? 我怎么能这样做?

Thank you! 谢谢!

This should solve your problems. 这应该可以解决你的问题。

listView1.View = View.Details; // Enables Details view so you can see columns
listView1.Items.Add(new ListViewItem { ImageIndex = 0, Text = "Image 1" }); // Using object initializer to add the text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM