C#列表视图中添加项目具有图像和文字,并对齐文本到左 [英] C# Listview adding item with image and text, and align the text to left
问题描述
我试图创造一些测试程序,只是为了好玩学习C#和我来的东西我真的无法弄清楚。
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.
我想将图像添加到项目中在ListView。我发现了#2一篇文章,解释如何做到这一点,和它的工作。但是,我不能额外的文本添加到该项目。我想有它旁边的文本图像。我当前的代码:
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. 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.
我也得到了第二个问题。
我没有列(其中加没有做的伎俩其一)。我想有对准到ListView的左侧的项目。我怎么能这样做呢?
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. How could I do this?
感谢您!
推荐答案
这应该解决您的问题。
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
这篇关于C#列表视图中添加项目具有图像和文字,并对齐文本到左的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!