天道酬勤,学无止境

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;
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)
    {

    }

}

}

评论

In order for images to appear next to the text in a column, you must:

  1. Connect the ObjectListView to an ImageList (using the SmallImageList property);
  2. Install an ImageGetter delegate for the column that must show the images;
  3. Make sure that there are actually images in the ImageList.

With this done, images will appear (I just tested this).

There is one catch, though. From your question, I suspect that the "Delete" column may not be the first column in the ObjectListView. The above steps only allow you to show an image in the very first column. For subsequent columns, you will have to set the ShowImagesOnSubItems property to True. Could that be it?

受限制的 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>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。

相关推荐
  • 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(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
  • 如何为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
  • 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
  • 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)
  • 在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(); }
  • 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
  • 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
  • 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 忘记您的自定义分类器。 重新开始使用以下页面上的代码。
  • 在Winforms中自定义ListView?(Custom ListView in Winforms?)
    问题 是否可以在列表视图上绘制一些字符串? 我重写了OnPaint事件,但看不到任何变化。 我在自定义列表视图上签出了一些代码,但是似乎人们在使用p / invoke等。为什么? 列表不是像其他Winform一样可自定义的,例如Button控件吗? 我不会疯狂地进行自定义,只是在完成标准绘画后再绘画一些。 回答1 class MyCustomlistView : ListView { public MyCustomlistView() : base() { SetStyle(ControlStyles.UserPaint, true); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.DrawString("This is a custom string", new Font(FontFamily.GenericSerif, 10, FontStyle.Bold), Brushes.Black, new PointF(0, 50)); } } 回答2 您不能只重写OnPaint()方法。 该方法在ListView中不执行任何操作。 类似地, OwnerDrawn允许您自定义绘制每个单元格,但不允许您绘制整个控件。 使用ObjectListView(.NET
  • 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
  • 在WinForms中将按钮添加到Listview中(Adding button into a Listview in WinForms)
    问题 有没有一种方法可以将按钮控件添加到WinForms应用程序中ListView内部的单元格中? 回答1 这是可以重用的类ListViewExtender的代码。 它不是ListView的派生类,基本上,您只是声明特定列显示为按钮而不是文本。 按钮的文本是子项的文本。 它允许没有问题的大型列表视图,不使用p / invoke,并且还可以与水平滚动条一起使用(此处提出的一些答案的代码在很多项目中没有或相当慢)。 注意,它要求扩展ListView的FullRowSelect设置为true ,视图类型设置为Details 。 这是使用它的示例代码: namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); // you need to add a listView named listView1 with the designer listView1.FullRowSelect = true; ListViewExtender extender = new ListViewExtender(listView1); // extend 2nd column ListViewButtonColumn buttonAction
  • ionic 2 back button or any icon is not showing. only small box icon is showing
    I am facing this icon problem in many screen. for example. i have one screen , where i have added some icon at left side of nav bar. but its not at all showing the icon. only small back box alone is showing .here is my code: <ion-buttons> <button ion-button > <ion-icon name="close"></ion-icon> </button> </ion-buttons I use this also : <ion-buttons> <button ion-button > <ion-icon ios="ios-close" md="md-close"></ion-icon> </button> </ion-buttons but no use.still black small box icon is showing. okay now, i tried with push nav.but there alos its not showing the back button.here is my code : my
  • 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" }, ...} 当我这样做时,图标将显示在工具栏中。 这还不是很清楚,但是如果将各个部分组合在一起
  • 是否可以在悬停时的两个字体图标之间切换?(Is it possible to change between two fontawesome icons on hover?)
    问题 我有一个带有真棒字体图标的锚标记,如下所示 <a href="#" class="lock"><i class="icon-unlock"></i></a> 是否可以将悬停图标更改为其他图标? 我的CSS .lock:hover{color:red} 除了图标变为红色外,我还想将图标更改为以下内容 <i class="icon-lock"></i> 如果没有JavaScript的帮助,这可能吗? 还是我需要在悬停时使用Javascript / Jquery? 谢谢你。 回答1 您可以切换悬停显示的人: HTML: <a href="#" class="lock"> <i class="icon-unlock"></i> <i class="icon-lock"></i> </a> CSS: .lock:hover .icon-unlock, .lock .icon-lock { display: none; } .lock:hover .icon-lock { display: inline; } 或者,你可以改变content的的icon-unlock类: .lock:hover .icon-unlock:before { content: "\f023"; } 回答2 在我的模板中,我经常使用FontAwesome,并且想到了这个CSS技巧 * > .fa-hover
  • phonegap build not showing splashscreen
    This question has been asked in the past but none of the answers that I have found have solved the problem for me. I am trying to make a very simple phonegap app to show a splash screen and announce when it is ready. It is just a test app to work out how to use the splash screen. I am using phonegap build through the phonegap cli. phonegap version 3.5.0 tested on android only I have a www folder where config.xml is located. All the splashscreens are located within www/res/screen/<platform>/filename.png my config file is <?xml version='1.0' encoding='utf-8'?> <widget id="uk.co.workshop14
  • 应用程序图标未显示在操作栏上(The application icon does not show on action bar)
    问题 我遵循了建立一个新的android项目的说明,除了操作栏出现问题外,我得到了一个可运行的项目。 问题在于,操作栏上的应用程序标题旁边未显示该应用程序图标。 我使用以下配置创建了项目: 所需的最低SDK:API 8:Android 2.2(Froyo) 目标SDK:API 21:Android 4.X(L预览) 编译为:API 21:Android 4.X(L预览) 主题:带有黑暗动作栏的“ Holo Light”(Eclipse设置了相应的appcompat主题) Android支持库21.0.1 Android SDK构建工具21.1.1 由于我的最低SDK为不支持操作栏的api 8,因此该项目包含一个允许使用操作栏功能的appcompat_v7 library 。 如果我将最小sdk设置为api 14(android 4.0)或更高版本,则该项目不包含appcompat_v7 library并且也成功显示了应用程序图标。 但是我需要我的应用程序来支持api 8以下的较早版本的android os。那我该怎么做才能解决此问题? 真的感谢你们的关注。 PS:我在Windows,Mac,Eclipse,Android Studio上完成了上述任务,并得到了相同的结果。 回答1 您正在使用AppCompat 21+版本,这是正常的。 动作栏遵循材料设计准则,并使用工具栏。
  • 如何在ActionBar的“溢出”菜单中显示图标(How To show icons in Overflow menu in ActionBar)
    问题 我知道使用原生API是不可能的。 有没有解决方法来实现这种观点? 回答1 一般来说,以前发布的答案还可以。 但是,它基本上删除了“溢出”菜单的默认行为。 像可以在不同屏幕尺寸上显示多少个图标之类的东西,然后在无法显示时会掉入溢出菜单。 通过执行上述操作,您删除了许多重要的功能。 更好的方法是告诉溢出菜单直接显示图标。 您可以通过将以下代码添加到“活动”中来执行此操作。 @Override public boolean onMenuOpened(int featureId, Menu menu) { if(featureId == Window.FEATURE_ACTION_BAR && menu != null){ if(menu.getClass().getSimpleName().equals("MenuBuilder")){ try{ Method m = menu.getClass().getDeclaredMethod( "setOptionalIconsVisible", Boolean.TYPE); m.setAccessible(true); m.invoke(menu, true); } catch(NoSuchMethodException e){ Log.e(TAG, "onMenuOpened", e); } catch(Exception e){