gpt4 book ai didi

c# - ObjectListView 显示图标

转载 作者:太空狗 更新时间:2023-10-30 00:53:37 24 4
gpt4 key购买 nike

尝试将图标放入 ObjectListview,这是我应该放置图标的代码:

objectListView1.SmallImageList = imageList1;

deleteColumn.IsEditable = true;
deleteColumn.ImageGetter = delegate
{
return 0;
};
deleteColumn.AspectGetter = delegate
{
return "Delete";
};

imageList1 已经有一个图像,这段代码应该在“删除”旁边放一个图标,但它根本没有出现,翻阅了食谱和谷歌,我仍然不知道。谁能帮帮我?

这是完整的表单代码,以备不时之需:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
objectListView1.AllowDrop = true;
objectListView1.DragEnter += new DragEventHandler(objectListView1_DragEnter);
objectListView1.DragDrop += new DragEventHandler(objectListView1_DragDrop);
objectListView1.CellEditActivation = BrightIdeasSoftware.ObjectListView.CellEditActivateMode.SingleClick;
objectListView1.CellEditStarting += deleteItems;
objectListView1.SmallImageList = imageList1;

deleteColumn.IsEditable = true;
deleteColumn.ImageGetter = delegate
{
return 0;
};
deleteColumn.AspectGetter = delegate
{
return "Delete";
};
}

private void objectListView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}

private void objectListView1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
{
string[] droppedFiles = (string[]) e.Data.GetData(DataFormats.FileDrop);
foreach (string path in droppedFiles)
{
if (File.Exists(path))
{
FileObject fo = new FileObject(path, "added later");
objectListView1.AddObject(fo);
}
}
}
}

private void deleteItems(object sender, BrightIdeasSoftware.CellEditEventArgs e)
{
if(e.Column == deleteColumn)
{
e.Cancel = true;
objectListView1.RemoveObject(e.RowObject);
}
}

private void Form1_Load(object sender, EventArgs e)
{

}

}

最佳答案

为了使图像显示在列中的文本旁边,您必须:

  1. ObjectListView 连接到 ImageList(使用 SmallImageList 属性);
  2. 为必须显示图像的列安装一个 ImageGetter 委托(delegate);
  3. 确保 ImageList 中确实有图像。

完成后,图像将出现(我刚刚测试过)。

不过,有一个问题。根据您的问题,我怀疑“删除”列可能不是 ObjectListView 中的第一列。上述步骤只允许您在第一列中显示图像。对于后续列,您必须将 ShowImagesOnSubItems 属性设置为 True。会不会是这样?

关于c# - ObjectListView 显示图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15565899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com