【问题标题】:how could I show filename under the image shown in the listview如何在列表视图中显示的图像下显示文件名
【发布时间】:2013-07-03 00:23:33
【问题描述】:

我正在编写一个代码,我需要在列表视图中显示多个图像,我可以得到显示的图像,但我怎样才能在图像下方显示文件名?

以下是我的部分代码

string stu_name_1 = listBox4.SelectedItem.ToString();
string stu_name_2 = listBox6.SelectedItem.ToString();
string add = stu_name_1 +"/"+ stu_name_2;
Directory.CreateDirectory(add);
OpenFileDialog sf = new OpenFileDialog();
Dictionary<string, string> imageDictionary = new Dictionary<string, string>();
sf.Multiselect = true;
if (sf.ShowDialog() == DialogResult.OK)
{
    string[] files = sf.FileNames;
    foreach (string file in files)
    {
        // Use static Path methods to extract only the file name from the path.
        string fileName = System.IO.Path.GetFileName(file);
        string destFile = System.IO.Path.Combine(add, fileName);
        Image imgToAdd = Image.FromFile(file);
        imgToAdd.Tag = file;
        System.IO.File.Copy(file, destFile, true);
        imageList1.Images.Add(imgToAdd);
        //imageDictionary.Add(Path.GetFileName(file), file);
    }
}
listView1.Items.Clear();

for (int i = 0; i < imageList1.Images.Count; i++)
{
    ListViewItem lvi = new ListViewItem();
    lvi.ImageIndex = i;
    listView1.Items.Add(lvi);

【问题讨论】:

标签: c# winforms listview imagelist


【解决方案1】:
    string stu_name_1 = listBox4.SelectedItem.ToString();
    string stu_name_2 = listBox6.SelectedItem.ToString();
    string add = stu_name_1 +"/"+ stu_name_2;
    Directory.CreateDirectory(add);
    OpenFileDialog sf = new OpenFileDialog();
    Dictionary<string, string> imageDictionary = new Dictionary<string, string>();
    sf.Multiselect = true;
    if (sf.ShowDialog() == DialogResult.OK)
    {
        listView1.Items.Clear();//Clear first
        string[] files = sf.FileNames;            
        foreach (string file in files)
        {
            // Use static Path methods to extract only the file name from the path.
            string fileName = System.IO.Path.GetFileName(file);
            string destFile = System.IO.Path.Combine(add, fileName);
            Image imgToAdd = Image.FromFile(file);
            imgToAdd.Tag = file;
            System.IO.File.Copy(file, destFile, true);
            imageList1.Images.Add(imgToAdd);//Suppose the imageList1 is dedicated to your listView1 only.
            listView1.Items.Add(fileName, imageList1.Images.Count-1);
            //imageDictionary.Add(Path.GetFileName(file), file);
        }
    }

【讨论】:

  • 谢谢,但这次只显示文件名,没有图像。这次我该怎么办?非常感谢您的回复。
  • 哈,我知道错了!非常感谢!
猜你喜欢
  • 1970-01-01
  • 2013-05-18
  • 1970-01-01
  • 1970-01-01
  • 2012-09-05
  • 2013-03-26
  • 2013-04-24
相关资源
最近更新 更多