【转】稍改进过的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 ...
随机推荐
- 杭电ACM 1201
#include <stdio.h> int func(int year){ if ( year % 400 == 0 || (year % 4 == 0 &&year % ...
- MySQL Access denied for user root@localhost 解决方法
今天把本地开发的项目(ThinkPHP 3.2.3)移到虚拟机上(CentOS 6.6,LNMP 1.2,MySQL 5.6.23),配置好 MySQL 之后访问首页,出现: 此时 MySQL 的用户 ...
- C 到C++的升级
C++所有的变量都可以在需要使用时再定义. C语言中的变量都必须在作用域开始的位置定义. register 关键字请求编译器将局部变量存储于寄存器中 在C语言无法获取register 变量的地址 在C ...
- 生产环境下的mysql主从复制
一.主mysql配置:1.配置my.cnf[mysqld]server-id = 10 #服务器标示log-bin= mysql-bin #二进制日志binlog-do-db=mydb #需要同步的数 ...
- CSS 伪元素&伪类
单冒号(:)用于CSS3伪类,双冒号(::)用于CSS3伪元素 伪元素 属性 描述 CSS :first-letter 向文本的第一个字母添加特殊样式 1 :first-line 向文本的首行添加特殊 ...
- 回归测试---junit
回归测试是指修改了旧代码后,重新进行测试以确认修改没有引入新的错误或导致其他代码产生错误. JUnit是一个Java语言的单元测试框架. http://blog.csdn.net/andycpp/ar ...
- SQL server2012怎么备份数据库(设置自动备份)
1.打开SQL server配置管理器,设置sql server服务里的SQL server代理服务为自动并启动 2.启动Master Data Services Configuration Mana ...
- ps aux和ps -ef命令区别
ps aux 是用BSD的格式来显示 java这个进程 显示的项目有:USER,PID,%CPU,%MEM,VSZ,RSS,TTY,STAT,START,TIME,COMMAND ps -ef ...
- SQL2005 数据库——查看索引
sqlserver查询表索引 2012-09-19 18:18 by Spring.Guo, 4599 阅读, 0 评论, 收藏, 编辑 SELECT 索引名称=a.name ,表名=c.nam ...
- 以一则LUA实例说明敏捷开发中“分离构造和使用”原则
分离构造和使用 构造含义是功能的实现, 此功能是一个定义明确的处理过程, 开放出明确的接口给调用者使用. 则使用者可以直接调用接口进行使用, 但是使用者需要搞清楚, 那些是构造, 那些是使用. 不要再 ...