【转】稍改进过的ListView,很好用哈
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Collections; namespace Emao_ListView
{
public class MyListView : System.Windows.Forms.ListView
{
#region Private Members private ListViewItem m_previousItem;
private bool m_allowReorder;
private Color m_lineColor; #endregion #region Public Properties [Category("Behavior")]
public bool AllowReorder
{
get { return m_allowReorder; }
set { m_allowReorder = value; }
} [Category("Appearance")]
public Color LineColor
{
get { return m_lineColor; }
set { m_lineColor = value; }
} #endregion #region Protected and Public Methods public MyListView()
: base()
{
this.FullRowSelect = true;
this.GridLines = true;
this.View = View.Details;
m_allowReorder = true;
m_lineColor = Color.Red;
SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer |
System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer |
System.Windows.Forms.ControlStyles.AllPaintingInWmPaint,
true);
UpdateStyles();
}
protected override void OnDragDrop(DragEventArgs drgevent)
{
if (!m_allowReorder)
{
base.OnDragDrop(drgevent);
return;
} // get the currently hovered row that the items will be dragged to
Point clientPoint = base.PointToClient(new Point(drgevent.X, drgevent.Y));
ListViewItem hoverItem = base.GetItemAt(clientPoint.X, clientPoint.Y); if (!drgevent.Data.GetDataPresent(typeof(DragItemData).ToString()) || ((DragItemData)drgevent.Data.GetData(typeof(DragItemData).ToString())).ListView == null || ((DragItemData)drgevent.Data.GetData(typeof(DragItemData).ToString())).DragItems.Count == )
return; // retrieve the drag item data
DragItemData data = (DragItemData)drgevent.Data.GetData(typeof(DragItemData).ToString()); if (hoverItem == null)
{
// the user does not wish to re-order the items, just append to the end
for (int i = ; i < data.DragItems.Count; i++)
{
ListViewItem newItem = (ListViewItem)data.DragItems[i];
base.Items.Add(newItem);
}
}
else
{
// the user wishes to re-order the items // get the index of the hover item
int hoverIndex = hoverItem.Index; // determine if the items to be dropped are from
// this list view. If they are, perform a hack
// to increment the hover index so that the items
// get moved properly.
if (this == data.ListView)
{
if (hoverIndex > base.SelectedItems[].Index)
hoverIndex++;
} // insert the new items into the list view
// by inserting the items reversely from the array list
for (int i = data.DragItems.Count - ; i >= ; i--)
{
ListViewItem newItem = (ListViewItem)data.DragItems[i];
base.Items.Insert(hoverIndex, newItem);
}
} // remove all the selected items from the previous list view
// if the list view was found
if (data.ListView != null)
{
foreach (ListViewItem itemToRemove in data.ListView.SelectedItems)
{
data.ListView.Items.Remove(itemToRemove);
}
} // set the back color of the previous item, then nullify it
if (m_previousItem != null)
{
m_previousItem = null;
} this.Invalidate(); // call the base on drag drop to raise the event
base.OnDragDrop(drgevent);
} protected override void OnDragOver(DragEventArgs drgevent)
{
if (!m_allowReorder)
{
base.OnDragOver(drgevent);
return;
} if (!drgevent.Data.GetDataPresent(typeof(DragItemData).ToString()))
{
// the item(s) being dragged do not have any data associated
drgevent.Effect = DragDropEffects.None;
return;
} if (base.Items.Count > )
{
// get the currently hovered row that the items will be dragged to
Point clientPoint = base.PointToClient(new Point(drgevent.X, drgevent.Y));
ListViewItem hoverItem = base.GetItemAt(clientPoint.X, clientPoint.Y); Graphics g = this.CreateGraphics(); if (hoverItem == null)
{
//MessageBox.Show(base.GetChildAtPoint(new Point(clientPoint.X, clientPoint.Y)).GetType().ToString()); // no item was found, so no drop should take place
drgevent.Effect = DragDropEffects.Move; if (m_previousItem != null)
{
m_previousItem = null;
Invalidate();
} hoverItem = base.Items[base.Items.Count - ]; if (this.View == View.Details || this.View == View.List)
{
g.DrawLine(new Pen(m_lineColor, ), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X + this.Bounds.Width, hoverItem.Bounds.Y + hoverItem.Bounds.Height));
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height - ), new Point(hoverItem.Bounds.X + , hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height + ) });
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(this.Bounds.Width - , hoverItem.Bounds.Y + hoverItem.Bounds.Height - ), new Point(this.Bounds.Width - , hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(this.Bounds.Width - , hoverItem.Bounds.Y + hoverItem.Bounds.Height + ) });
}
else
{
g.DrawLine(new Pen(m_lineColor, ), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width, hoverItem.Bounds.Y + hoverItem.Bounds.Height));
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width - , hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width + , hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width, hoverItem.Bounds.Y + ) });
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width - , hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width + , hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width, hoverItem.Bounds.Y + hoverItem.Bounds.Height - ) });
} // call the base OnDragOver event
base.OnDragOver(drgevent); return;
} // determine if the user is currently hovering over a new
// item. If so, set the previous item's back color back
// to the default color.
if ((m_previousItem != null && m_previousItem != hoverItem) || m_previousItem == null)
{
this.Invalidate();
} // set the background color of the item being hovered
// and assign the previous item to the item being hovered
//hoverItem.BackColor = Color.Beige;
m_previousItem = hoverItem; if (this.View == View.Details || this.View == View.List)
{
g.DrawLine(new Pen(m_lineColor, ), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + this.Bounds.Width, hoverItem.Bounds.Y));
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y - ), new Point(hoverItem.Bounds.X + , hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + ) });
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(this.Bounds.Width - , hoverItem.Bounds.Y - ), new Point(this.Bounds.Width - , hoverItem.Bounds.Y), new Point(this.Bounds.Width - , hoverItem.Bounds.Y + ) });
}
else
{
g.DrawLine(new Pen(m_lineColor, ), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height));
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X - , hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + , hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + ) });
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X - , hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X + , hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height - ) });
} // go through each of the selected items, and if any of the
// selected items have the same index as the item being
// hovered, disable dropping.
foreach (ListViewItem itemToMove in base.SelectedItems)
{
if (itemToMove.Index == hoverItem.Index)
{
drgevent.Effect = DragDropEffects.None;
hoverItem.EnsureVisible();
return;
}
} // ensure that the hover item is visible
hoverItem.EnsureVisible();
} // everything is fine, allow the user to move the items
drgevent.Effect = DragDropEffects.Move; // call the base OnDragOver event
base.OnDragOver(drgevent);
} protected override void OnDragEnter(DragEventArgs drgevent)
{
if (!m_allowReorder)
{
base.OnDragEnter(drgevent);
return;
} if (!drgevent.Data.GetDataPresent(typeof(DragItemData).ToString()))
{
// the item(s) being dragged do not have any data associated
drgevent.Effect = DragDropEffects.None;
return;
} // everything is fine, allow the user to move the items
drgevent.Effect = DragDropEffects.Move; // call the base OnDragEnter event
base.OnDragEnter(drgevent);
} protected override void OnItemDrag(ItemDragEventArgs e)
{
if (!m_allowReorder)
{
base.OnItemDrag(e);
return;
} // call the DoDragDrop method
base.DoDragDrop(GetDataForDragDrop(), DragDropEffects.Move); // call the base OnItemDrag event
base.OnItemDrag(e);
} protected override void OnLostFocus(EventArgs e)
{
// reset the selected items background and remove the previous item
ResetOutOfRange(); Invalidate(); // call the OnLostFocus event
base.OnLostFocus(e);
} protected override void OnDragLeave(EventArgs e)
{
// reset the selected items background and remove the previous item
ResetOutOfRange(); Invalidate(); // call the base OnDragLeave event
base.OnDragLeave(e);
} #endregion
#region Private Methods private DragItemData GetDataForDragDrop()
{
// create a drag item data object that will be used to pass along with the drag and drop
DragItemData data = new DragItemData(this); // go through each of the selected items and
// add them to the drag items collection
// by creating a clone of the list item
foreach (ListViewItem item in this.SelectedItems)
{
data.DragItems.Add(item.Clone());
} return data;
} private void ResetOutOfRange()
{
// determine if the previous item exists,
// if it does, reset the background and release
// the previous item
if (m_previousItem != null)
{
m_previousItem = null;
} } #endregion #region DragItemData Class private class DragItemData
{
#region Private Members private MyListView m_listView;
private ArrayList m_dragItems; #endregion #region Public Properties public MyListView ListView
{
get { return m_listView; }
} public ArrayList DragItems
{
get { return m_dragItems; }
} #endregion #region Public Methods and Implementation public DragItemData(MyListView listView)
{
m_listView = listView;
m_dragItems = new ArrayList();
} #endregion
} #endregion
}
}
改进了一点点的ListView,数据多不会闪。可以拖动行来进行排序了。
以前主要是自己用。就这样了
要启用拖放行,设置一下 AllowRecoder为true即可。另外多行选择=false
原文:http://www.oschina.net/code/snippet_191468_13278
【转】稍改进过的ListView,很好用哈的更多相关文章
- ListView 使用方法(Asp.Net)
您将须要用到的独有数据绑定控件. Fritz Onion 代码下载位置: ExtremeASPNET2008_03.exe (192 KB) Browse the Code Online 文件夹 L ...
- 【腾讯Bugly干货分享】跨平台 ListView 性能优化
本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/FbiSLPxFdGqJ00WgpJ94yw 导语 精 ...
- CocosCreator之分层管理的ListView
前言 进入公众号回复listview即可获得demo的git地址. 之前写的一篇文章<Creator之ScrollView那些事>中提到了官方Demo中提供的ListViewCtl,只是实 ...
- Listview详解
Listview应该是最为常见的控件.对于大多数规则排列的界面,几乎都可以用ListView进行编写.对于单一界面来说,ListView既是最难的控件,又是使用最为频繁的控件.ListView 通常用 ...
- 改进duilib的richedit控件的部分功能
转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41208207 如果要使用透明异形窗体功能,首先要改进duilib库让他本 ...
- Android优化系列之ListView优化老生常谈
本文内容:adapter,listview的优化,RecycleBi,google大会推荐优化, 实现ListView的过程,Adapter起到了至关重要的作用,不仅仅因为getview()方法.那么 ...
- android 一个页面内 多个listview的实现
如果很平常的两个listview组件竖直放在linearLayout布局中,结果是: 两个listview 很独立,中间似乎有个分割线,完全吧他们分离了,各自独立滚动,如果上面的listview把整个 ...
- 【转】Android:ListView常见错位之CheckBox错位
原文网址:http://blog.csdn.net/lemon_tree12138/article/details/39337867 ListView在什么样的情况下会出现错位?错位的原因是什么?怎么 ...
- ListView学习小结
ListView小结 ListView 是Android UI中十分重要的一个组件,在数据的显示上能有着十分灵活的表现,其使用也比较简单,一般包括以下几个要点: 1. 可以通过编写ListActiv ...
随机推荐
- purge mysql自带命令清除binlog
#!/bin/bash DATAUSER=root DATAPASS=shiyiwen DAY=$1 if [ ! $# == 1 ];then echo -e "\033[32m USAG ...
- CMS .NET 程序框架 从2.0/3.5升级到4.0 版本后 需要调整的地方
问题一: document.forms1.action 不可使用 需要修改程 document.forms[0] .NET 程序框架 从2.0/3.5升级到4.0 版本后,document.forms ...
- Graph | Eulerian path
In graph theory, a Eulerian trail (or Eulerian path) is a trail in a graph which visits every edge e ...
- iTop各数据表联系图(持续更新中)
- mysql5.5手册读书日记(2)
<?php /* * * MySQL_5.5中文参考手册 485开始 * * mysql> SELECT CASE 1 WHEN 1 THEN 'one' -> WHEN 2 THE ...
- Javascript备忘模式
使用备忘模式,利用了函数的自定义属性,先看一个例子 var test = function (){} test.myAttr = "attr"; 这样,就给test加上了一个自定义 ...
- Fail to start neutron-server
问题: [root@localhost ~]# systemctl status neutron-server ● neutron-server.service - OpenStack Neutron ...
- RESTful框架调研
背景 当前的开放服务平台发展趋势,是服务使用者变得多种多样,其中既有各种前端设备(台式机.手机等),又有各种后端服务器,因此必须有一个统一的机制,方便各种服务使用者和开放服务平台进行通信.为了更好的实 ...
- iOS简易图片选择器 (图片可多选,仿微信)
调用方法 NickyImagePickerViewController *pickerController = [[NickyImagePickerViewController alloc]init] ...
- oslo.messaging 1.8.0 bug fix and blueprint
1366597 由于amqp_auto_delete可配置,但是NotifierPublisher使用的是没有在配置中获取而使用的默认的False,即非auo_delete,因而在用户配置了amqp_ ...