天道酬勤,学无止境

ObjectListView显示图标(ObjectListView show icons)

问题

试图在ObjectListview中放置图标,这是我应该在其中放置图标的代码:

objectListView1.SmallImageList = imageList1;

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

imageList1已经有一个图像,此代码应该在“删除”旁边放置一个图标,但是它完全没有出现,通过食谱和Google进行查看,我仍然不知道。 谁能帮我?

如果需要,这是完整的表单代码:

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

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

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

完成此操作后,将出现图像(我刚刚对此进行了测试)。

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

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。

相关推荐
  • ObjectListView show icons
    Trying to put icons in ObjectListview, here's my piece of code where icon should have been put: objectListView1.SmallImageList = imageList1; deleteColumn.IsEditable = true; deleteColumn.ImageGetter = delegate { return 0; }; deleteColumn.AspectGetter = delegate { return "Delete"; }; imageList1 already have an image, this code should have put an icon next to "Delete", but it did not appear at all, looked through cookbooks and Google and I still have no idea. Can anyone help me? this is the full form code in case needed: using System; using System.Collections.Generic; using System.ComponentModel
  • 如何为ListViewSubItem设置图标?(How can I set an icon for a ListViewSubItem?)
    问题 在ListView中,每个项目上都可以具有图标。 在“详细信息”模式下查看时,该图标显示在最左侧的列中。 我可以在其他栏中显示图标吗? 回答1 ListView控件本身不支持子项目中的图像。 最简单的方法是切换到DataGridView并使用DataGridViewImageColumn 。 如果这不可能,那么您将需要使用ListView控件中的自定义绘制支持自己绘制图标。 为此,请设置ListView.OwnerDraw = true并处理ListView.DrawSubItem和ListView.DrawColumnHeader事件。 private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) { // Only interested in 2nd column. if (e.Header != this.columnHeader2) { e.DrawDefault = true; return; } e.DrawBackground(); var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height); e.Graphics.DrawImage
  • ObjectListView cast exception (for hit testing)
    I am using Grammarian's ObjectListView. I changed my old listviews to that, but all I do is to fill in items. But when the app starts up and my mouse is over the listview, it immediately throws an exception for: System.InvalidCastException was unhandled Message="Unable to cast object of type 'System.Windows.Forms.ListViewItem' to type 'BrightIdeasSoftware.OLVListItem'." Source="ObjectListView" How to fix this? Also I use Win7 if that's important. EDIT: I use a dictionary. It looks like I need to use the SetObjects method instead of adding items. Ok this is great, but I am just using the dict
  • How to add a new item into ObjectListView?
    I tried the demo code in demo project but I can't add new item successfully. It just add new new NULL group and NULL item. Please give me an simple example code to add new item (text and image). Thank you! Oh sorry! I forgot it. This is the first time I participate in this site. I use C#. And the code is: objectListView1.BeginUpdate(); objectListView1.AddObject(new string [] {"Hello","dfdsF" }); objectListView1.EndUpdate(); and objectListView1.BeginUpdate(); OLVListItem item = new OLVListItem(new string [] {"Hello","dfdsF" }); objectListView1.Items.Add(item); objectListView1.EndUpdate(); It's
  • Filtering ListView with custom (object) adapter
    I'm trying to implement filtering of a ListView which is uses a custom object adapter, but I can't find any useful samples. The included code is very simplified, so no- keep in mind I can't use an regular ArrayAdapter. I have a EditText above the ListView, and when the user enters text in the EditText widget I would like to filter the ListView by the text written in the EditText. Any suggestions would be much appreciated! Here is the snippet from the activity class: public class management_objects extends Activity { private static List<User> UserList; private EfficientAdapter adapter = null
  • 在ListView的网格中显示128x128像素或更大的缩略图图标(Displaying thumbnail icons 128x128 pixels or larger in a grid in ListView)
    问题 原始问题(请参阅下面的更新) 我有一个WinForms程序,它需要一个带有大图标(实际上是128x128或更大的缩略图)的体面的可滚动图标控件,可以单击该图标来突出显示或双击来执行某些操作。 最好将空间浪费最小化(每个图标下方可能需要短文件名标题;如果文件名太长,我可以添加省略号)。 (来源:updike.org) 我尝试将ListView与LargeIcon(默认为.View)一起使用,结果令人失望: (来源:updike.org) 也许我没有正确填充控件? 代码: ImageList ilist = new ImageList(); this.listView.LargeImageList = ilist; int i = 0; foreach (GradorCacheFile gcf in gc.files) { Bitmap b = gcf.image128; ilist.Images.Add(b); ListViewItem lvi = new ListViewItem("text"); lvi.ImageIndex = i; this.listView.Items.Add(lvi); i++; } 我需要带有很少空白空间的大图标,而不需要带有令人尴尬的小图标的大空白空间。 是否有我需要的.NET控件? 有没有喜欢的第三方控件可以做到这一点? 如果不是
  • 即使将焦点放在另一个控件上,如何更改listview所选行的背景色?(How to change listview selected row backcolor even when focus on another control?)
    问题 我有一个使用条形码扫描仪作为输入设备的程序,这意味着我需要将重点放在文本框上。 该程序具有一个listview控件,当扫描特定条形码时,我将以编程方式选择其中一项。 我通过以下方式设置行的背景色: listviewitem.BackColor = Color.LightSteelBlue; 我尝试过的事情: listview.HideSelection设置为false 设置颜色后调用listview.Focus() listviewitem.Focused设置为true 呼叫listview.Invalidate 呼叫listview.Update() 调用listview.Refresh() 上面的不同组合 我还在计时器中进行了上述组合,因此它们在不同的线程上被调用,但仍然没有成功。 有任何想法吗? 更多信息: 这里的关键是控制焦点。 选择其中一项时, listview控件没有焦点。 我通过执行以下操作选择一项: listView1.Items[index].Selected = true; 焦点始终在文本框中。 该计算机没有键盘或鼠标,只有条形码读取器。 我有以下代码可以将重点放在文本框上: private void txtBarcode_Leave(object sender, EventArgs e) { this.txtBarcode.Focus(); }
  • VisualStyleRenderer和主题(WinForms)(VisualStyleRenderer and themes (WinForms))
    问题 我有自己的TreeView控件,它完全是OwnerDraw'n: myTreeView.DrawMode = TreeViewDrawMode.OwnerDrawAll; 我试图实现的是根据当前资源管理器主题绘制打开/关闭的字形。 尤其是在Vista和Win7盒子上,我希望看到新的字形(黑色三角形)而不是正负号。 我知道,对于非OwnerDraw'n TreeView来说,可以通过如下方式实现,效果很好: myTreeView.HandleCreated += delegate(object sender, EventArgs args) { MyNativeMethods.SetWindowTheme(myTreeView.Handle, "explorer", null); }; 我以为VisualStyleRenderer可以让我绘制字形主题感知: VisualStyleRenderer r = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Opened); r.DrawBackground(e.Graphics, e.Bounds); 不幸的是,以上代码在所有情况下都会绘制负号。 看起来VisualStyleRenderer不支持主题设置。 有人可以阐明这一点吗? 谢谢! 回答1
  • The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects
    I have 2 tables in SQL. One is called Training and one is called Consultants. Training is couple of fields like ID, Place, Time, Date and it also has ConsultantName which is foreign key and is connected with Consultants having ConsultantName, ConsultantFirstName etc. During save of Training i save the training first and then pass the Training value as varTraining. I tried to attatch it to context as per some suggestions I found on stack but it doesn't seem to work: Training Training = context.Trainings.First(p => p.TrainingID == varTraining.TrainingID); context.Trainings.Attach(Training)
  • How to wordWrap the text in a column using ObjectListView
    For example I have a big sentence: "I like to eat pie and have fun around the house all day long!" And I want it to appear like this: "I like to eat pie and have fun around the house all day long!" In this post: Multi-line list items on WinForms ListView control? Grammarian said that you only need to have WordWrap on but I cannot find that option. Thanks for the help in advanced
  • Objectlistview how to change the text in the group header?
    I want to change the name of the group header but i cant find any solutions in the documentation or on google. the text in the header should be a time which is summed up in the group. this is how it should look like:
  • 使用自定义(对象)适配器过滤ListView(Filtering ListView with custom (object) adapter)
    问题 我正在尝试对使用自定义对象适配器的ListView进行过滤,但是找不到任何有用的示例。 包含的代码非常简化,因此请记住,我不能使用常规的ArrayAdapter。 我在ListView上方有一个EditText,当用户在EditText小部件中输入文本时,我想用EditText中编写的文本过滤ListView。 我们欢迎所有的建议! 这是活动类的摘录: public class management_objects extends Activity { private static List<User> UserList; private EfficientAdapter adapter = null; private ListView objectListView = null; private EditText SearchText = null; private static class EfficientAdapter extends BaseAdapter implements Filterable{ private LayoutInflater mInflater; public EfficientAdapter(Context context) { mInflater = LayoutInflater.from(context); } public int
  • How to tweak my tooltips in wxpython?
    I was trying to add a tooltip to show the full content of a truncated ObjectListView, until it turned out it had such a feature built-in: I tried making my own tool tips using wx.TipWindow, wx.PopupWindow and SuperToolTip, but none of them looked as 'native' as this one. While I'm aware of this wiki article that supposedly enables the tooltip for truncated wx.Listrctrls, I didn't really understand how to get it working. I also expect that it only works when something is truncated, whereas I'd like to be able to use it to display some more information. I guess the SuperToolTip comes close, but
  • Flattern child/parent data with unknown number of columns
    I'm struggling to find the best way to store and represent the data I have in SQL (MySQL DB) and C# windows form. My data when mapped to classes which looks like this; public class Parent { public string UniqueID { get; set; } //Key public DateTime LoadTime { get; set; } public string Reference { get; set; } private List<Child> Elements { get; set; } } public class Child { public int MemberCode { get; set; } //Composite key public int ElementCode { get; set; } //Composite key public Object Data { get; set; } } My data is very dynamic. So a parent record can have any number of child records. In
  • ListView Final Column Autosize创建滚动条(ListView Final Column Autosize creates scrollbar)
    问题 我正在实现一个自ListView派生的自定义控件。 我希望最后一栏填充剩余的空间(这是一项常见的任务),我通过重写OnResize方法来解决了这个问题: protected override void OnResize(EventArgs e) { base.OnResize(e); if (Columns.Count == 0) return; Columns[Columns.Count - 1].Width = -2; // -2 = Fill remaining space } 或通过另一种方法: protected override void OnResize(EventArgs e) { base.OnResize(e); if (!_autoFillLastColumn) return; if (Columns.Count == 0) return; int TotalWidth = 0; int i = 0; for (; i < Columns.Count - 1; i++) { TotalWidth += Columns[i].Width; } Columns[i].Width = this.DisplayRectangle.Width - TotalWidth; } 编辑: 直到我将ListView停靠在父容器中并通过该控件调整大小之前,它都可以正常工作。
  • WinForms ListView控件上的多行列表项?(Multi-line list items on WinForms ListView control?)
    问题 有没有一种方法可以在标准Winforms listview控件中显示多行文本? 回答1 如果要在ListView中使用多行文本,请查看ObjectListView(围绕.NET WinForms ListView的开源包装程序)。 这可以解决所有者绘图(和其他ListView烦恼)所涉及的许多问题。 在以下屏幕截图中,第一列已启用WordWrap : 只需记住ListView不能包含不同高度的行。 在上面的屏幕截图中,我无法使第一行和第三行更高以显示更多文本,而其他行则更短。 每行必须具有相同的高度。 如果必须具有不同高度的行,则ListView将不是您的解决方案。 您可能需要考虑Matthew Hall出色的XPTable及其更新项目,以及Lee Paul Alexander出色的Outlook样式列表。 回答2 是的,但仅在所有者绘制的列表视图中。 编辑 检查以下链接: http://www.codeproject.com/KB/list/aa_listview.aspx http://www.codeproject.com/Messages/2857480/Multiline-listview.aspx
  • How do I add/remove items to a ListView in virtual mode?
    If I'm using a ListView in virtual mode then, as I understand it, the list view only keeps track of a small number of items in the list. As the user scrolls it dynamically retrieves items it needs to show from the virtual list. But what if an item is added or removed from the master list? If an item is added/removed outside of the range of indexes being shown by the list view then I would assume the list view would show the added/missing items when the user scrolls to that index. Is this correct? But what if an item is added/removed from the range of indexes the user is currently viewing? How
  • 按列对ListView排序(Sorting A ListView By Column)
    问题 当前,我在列表视图上使用自定义排序器,并且每次单击FIRST列时都可以对列表视图进行排序,但是不会按其他列进行排序。 SortStyle:用于确定是升序还是降序的变量。 if (e.Column == 0) { if (SortStyle == 0) { List.ListViewItemSorter = customSortDsc; SortStyle = 1; } else { List.ListViewItemSorter = customSortAsc; SortStyle = 0; } } 在对第一列进行排序时,这可以很好地工作,但是如果要在其他任何列上进行排序,则只会按第一列进行排序。 有没有一种方法可以对单击的列进行排序? 回答1 如果您从ListView开始,请帮自己一个忙,使用ObjectListView代替。 ObjectListView是.NET WinForms ListView的开源包装,它使ListView更加易于使用,并为您解决了许多常见问题。 按列单击排序是它自动为您处理的许多事情之一。 认真地说,您永远不会后悔使用ObjectListView而不是普通的ListView。 ObjectListView主页 ObjectListView Nuget软件包 回答2 忘记您的自定义分类器。 重新开始使用以下页面上的代码。
  • Firefox附加组件:扩展程序图标未显示(Firefox add-on : extension icon not showing)
    问题 我正在使用Add-on SDK开发Firefox插件。 使用SDK时,我的扩展程序工作正常,但是当我创建.xpi文件以将其手动添加到浏览器时,它不显示扩展程序图标。 我试图更改package.json文件中的图标路径,但仍然没有显示图标。 package.json文件: {... "icon" : "Phone-icon48.png", "icon64" : "Phone-icon64.png", ...} 用于显示图标的小部件面板: WidgetPackage.Widget({ label: "Phone Dial", id: "phone_dial", contentURL: data.url("images/Phone-icon19.png"), panel: panel_name }); 谁能帮我解决问题? 谢谢你。 回答1 这可能是一个永久性的错误。 该线程报告了类似的问题。 啊-这是解决方案。 您可以将图标放在加载项目录根目录中名为data的文件夹中,并as if在根目录中一样对其进行调用。 {... icon: { "16": "./icon-16.png", "32":"./icon-32.png", "64": "./icon-64.png" }, ...} 当我这样做时,图标将显示在工具栏中。 这还不是很清楚,但是如果将各个部分组合在一起
  • 如果未用作“ image / x-icon”,则IE不会在HTML中显示.ico图标。(IE doesn't show .ico icons in HTML if not served as “image/x-icon”)
    问题 我在列表中以<img />元素的形式显示网站的图标。 一些网站将其用作: <link rel="icon" type="image/x-icon" href="favicon.ico" /> 有些充当它们: <link rel="shortcut icon" href="favicon.ico" /> IE不会显示没有type =“ image / x-icon”的图标图标。 我无法控制网站如何为其网站图标提供服务,那么我该怎么办? 回答1 我认为最好的办法是将它们转换为另一种格式。 如果要手动显示图标,则可以下载每个图标,将其与图像程序进行转换,然后将其上传到您的站点。 如果您是自动执行的,则需要查看服务器端提供的任何图像处理工具。 如果您使用的是PHP,那将是GD或ImageMagick库。 例如,只需转换为PNG,然后将其缓存在您的网站上即可。 如果IE不能在页面内联显示.ico文件,那么不进行某种处理就不太可能显示它。 在最简单的级别上,您可以读取外部图像并将其用作正确的mime类型,而无需进行转换。 回答2 将其放入您的apache配置或.htaccess文件中: AddType image/x-icon .ico 回答3 不幸的是,似乎无法解决我遇到的问题。 IE无法显示我的链接列表(http://www.jerbils.info/wxlinks.php