通过"ItemDrag"、"DragEnter"、"DragDrop"三个事件来处理:
参考代码:using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
//导入程序中使用的命名空间
public class Form1 : Form {
private TreeView treeView1 ;
private Point Position = new Point( 0 , 0) ;
// bool lv1_mdown = false ;
private ListView listView1 ;
private System.ComponentModel.Container components = null ;
public Form1 ( ) {
InitializeComponent ( ) ;
//初始化窗体中的各个组件 }
//清除程序中使用到的各种资源
protected override void Dispose ( bool disposing ) {
if ( disposing )
{
if ( components != null )
{components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}
private void InitializeComponent ( )
{
ListViewItem listViewItem1 = new ListViewItem ( "Item01" ) ; ListViewItem listViewItem2 = new ListViewItem ( "Item02" ) ;
treeView1 = new TreeView ( ) ;
listView1 = new ListView ( ) ;
SuspendLayout ( ) ; //此属性必须设定为"真",这样才能进行拖放操作 treeView1.AllowDrop = true ;
treeView1.ImageIndex = -1 ;
treeView1.Location = new Point ( 48 , 40 ) ;
treeView1.Name = "treeView1" ; //在TreeView组件中加入初始化的节点 treeView1.Nodes.Add ( new TreeNode ( "节点01" ) );
treeView1.Nodes.Add ( new TreeNode ( "节点02" ) ); treeView1.SelectedImageIndex = -1 ;
treeView1.Size = new Size ( 121 , 144 ) ;
treeView1.TabIndex = 0 ;
treeView1.DragEnter += new DragEventHandler ( treeView1_DragEnter ) ; treeView1.ItemDrag += new ItemDragEventHandler ( treeView1_ItemDrag ) ; treeView1.DragDrop += new DragEventHandler ( treeView1_DragDrop ) ;
//此属性必须设定为"真",这样才能进行拖放操作
listView1.AllowDrop = true ;
//在ListView组件中加入项目
listView1.Items.Add ( listViewItem1 ) ;
listView1.Items.Add ( listViewItem2 ) ;
listView1.Location = new Point ( 208 , 40 ) ;
listView1.Name = "listView1" ;
listView1.Size = new Size ( 121 , 144 ) ;
listView1.TabIndex = 2 ;
listView1.View = View.List ;
listView1.DragDrop += new DragEventHandler ( listView1_DragDrop ) ; listView1.DragEnter += new DragEventHandler ( listView1_DragEnter ) ; listView1.ItemDrag += new ItemDragEventHandler ( listView1_ItemDrag ) ; AutoScaleBaseSize = new Size ( 6 , 14 ) ;
ClientSize = new Size ( 376 , 237 ) ;
Controls.Add ( listView1 );
Controls.Add ( treeView1 );
this.MaximizeBox = false ;
this.MinimizeBox = false ;
this.Name = "Form1" ;
this.Text = "全面掌握C#中的拖放操作" ;
this.ResumeLayout ( false ) ;
}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
private void treeView1_ItemDrag ( object sender , ItemDragEventArgs e ) {
string strItem = e.Item.ToString ( ) ;
//开始进行"Drag"操作
DoDragDrop ( strItem , DragDropEffects.Copy | DragDropEffects.Move ) ;
}
private void listView1_DragEnter ( object sender , DragEventArgs e )
{
//判断是否目前拖动的数据是字符串,如果是,则拖动符串对目的组件进行拷贝
if ( e.Data.GetDataPresent ( DataFormats.Text ) )
e.Effect = DragDropEffects.Move ;
else e.Effect = DragDropEffects.None ;
}
private void listView1_DragDrop ( object sender , DragEventArgs e )
{
string dummy = "temp" ;
//获得进行"Drag"操作中拖动的字符串
string s = ( string ) e.Data.GetData ( dummy.GetType ( ) ) ;
s = s.Substring ( s.IndexOf ( ":" ) + 1 ).Trim ( ) ;
Position.X = e.X ;
Position.Y = e.Y ;
Position = listView1.PointToClient ( Position ) ;
//在目标组件中加入以此字符串为标题的项目
listView1.Items.Add ( new ListViewItem ( s , 0 ) ) ;
}
private void listView1_ItemDrag ( object sender , ItemDragEventArgs e ) {
//判断是否是鼠标右键按动
if ( e.Button == MouseButtons.Right ) return ;
int nTotalSelected = listView1.SelectedIndices.Count ;
//判断组件中是否存在项目
if ( nTotalSelected <= 0 ) return ;
IEnumerator selCol = listView1.SelectedItems.GetEnumerator ( ) ; selCol.MoveNext ( ) ;
ListViewItem lvi = ( ListViewItem )selCol.Current ;
string mDir = "" ;
for ( int i = 0 ; i < lvi.SubItems.Count ; i++ )
mDir += lvi.SubItems[ i ].Text + " ," ;
string str = mDir.Substring ( 0 , mDir.Length-1 ) ;
if ( str == "" ) return ;
//对组件中的字符串开始拖放操作
listView1.DoDragDrop ( str , DragDropEffects.Copy | DragDropEffects.Move ) ;
}
private void treeView1_DragEnter ( object sender , DragEventArgs e )
{
//判断是否目前拖动的数据是字符串,如果是,则拖动符串对目的组件进行拷贝
if ( e.Data.GetDataPresent ( DataFormats.Text ) )
e.Effect = DragDropEffects.Copy ;
else e.Effect = DragDropEffects.None ;
}
private void treeView1_DragDrop ( object sender , DragEventArgs e )
{
//获得进行"Drag"操作中拖动的字符串
string dummy = "temp" ;
string s = ( string ) e.Data.GetData ( dummy.GetType ( ) ) ;
s = s.Substring ( s.IndexOf ( ":" ) + 1 ).Trim ( ) ;
Position.X = e.X ;
Position.Y = e.Y ;
Position = treeView1.PointToClient ( Position ) ;
TreeNode DropNode = this.treeView1.GetNodeAt ( Position ) ;
//在目标组件中加入以此字符串为标题的项目
if ( DropNode != null )
{
TreeNode DragNode = new TreeNode ( s ) ;
treeView1.Nodes.Insert ( DropNode.Index+1 , DragNode ) ;
}
}
}
参考代码:using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
//导入程序中使用的命名空间
public class Form1 : Form {
private TreeView treeView1 ;
private Point Position = new Point( 0 , 0) ;
// bool lv1_mdown = false ;
private ListView listView1 ;
private System.ComponentModel.Container components = null ;
public Form1 ( ) {
InitializeComponent ( ) ;
//初始化窗体中的各个组件 }
//清除程序中使用到的各种资源
protected override void Dispose ( bool disposing ) {
if ( disposing )
{
if ( components != null )
{components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}
private void InitializeComponent ( )
{
ListViewItem listViewItem1 = new ListViewItem ( "Item01" ) ; ListViewItem listViewItem2 = new ListViewItem ( "Item02" ) ;
treeView1 = new TreeView ( ) ;
listView1 = new ListView ( ) ;
SuspendLayout ( ) ; //此属性必须设定为"真",这样才能进行拖放操作 treeView1.AllowDrop = true ;
treeView1.ImageIndex = -1 ;
treeView1.Location = new Point ( 48 , 40 ) ;
treeView1.Name = "treeView1" ; //在TreeView组件中加入初始化的节点 treeView1.Nodes.Add ( new TreeNode ( "节点01" ) );
treeView1.Nodes.Add ( new TreeNode ( "节点02" ) ); treeView1.SelectedImageIndex = -1 ;
treeView1.Size = new Size ( 121 , 144 ) ;
treeView1.TabIndex = 0 ;
treeView1.DragEnter += new DragEventHandler ( treeView1_DragEnter ) ; treeView1.ItemDrag += new ItemDragEventHandler ( treeView1_ItemDrag ) ; treeView1.DragDrop += new DragEventHandler ( treeView1_DragDrop ) ;
//此属性必须设定为"真",这样才能进行拖放操作
listView1.AllowDrop = true ;
//在ListView组件中加入项目
listView1.Items.Add ( listViewItem1 ) ;
listView1.Items.Add ( listViewItem2 ) ;
listView1.Location = new Point ( 208 , 40 ) ;
listView1.Name = "listView1" ;
listView1.Size = new Size ( 121 , 144 ) ;
listView1.TabIndex = 2 ;
listView1.View = View.List ;
listView1.DragDrop += new DragEventHandler ( listView1_DragDrop ) ; listView1.DragEnter += new DragEventHandler ( listView1_DragEnter ) ; listView1.ItemDrag += new ItemDragEventHandler ( listView1_ItemDrag ) ; AutoScaleBaseSize = new Size ( 6 , 14 ) ;
ClientSize = new Size ( 376 , 237 ) ;
Controls.Add ( listView1 );
Controls.Add ( treeView1 );
this.MaximizeBox = false ;
this.MinimizeBox = false ;
this.Name = "Form1" ;
this.Text = "全面掌握C#中的拖放操作" ;
this.ResumeLayout ( false ) ;
}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
private void treeView1_ItemDrag ( object sender , ItemDragEventArgs e ) {
string strItem = e.Item.ToString ( ) ;
//开始进行"Drag"操作
DoDragDrop ( strItem , DragDropEffects.Copy | DragDropEffects.Move ) ;
}
private void listView1_DragEnter ( object sender , DragEventArgs e )
{
//判断是否目前拖动的数据是字符串,如果是,则拖动符串对目的组件进行拷贝
if ( e.Data.GetDataPresent ( DataFormats.Text ) )
e.Effect = DragDropEffects.Move ;
else e.Effect = DragDropEffects.None ;
}
private void listView1_DragDrop ( object sender , DragEventArgs e )
{
string dummy = "temp" ;
//获得进行"Drag"操作中拖动的字符串
string s = ( string ) e.Data.GetData ( dummy.GetType ( ) ) ;
s = s.Substring ( s.IndexOf ( ":" ) + 1 ).Trim ( ) ;
Position.X = e.X ;
Position.Y = e.Y ;
Position = listView1.PointToClient ( Position ) ;
//在目标组件中加入以此字符串为标题的项目
listView1.Items.Add ( new ListViewItem ( s , 0 ) ) ;
}
private void listView1_ItemDrag ( object sender , ItemDragEventArgs e ) {
//判断是否是鼠标右键按动
if ( e.Button == MouseButtons.Right ) return ;
int nTotalSelected = listView1.SelectedIndices.Count ;
//判断组件中是否存在项目
if ( nTotalSelected <= 0 ) return ;
IEnumerator selCol = listView1.SelectedItems.GetEnumerator ( ) ; selCol.MoveNext ( ) ;
ListViewItem lvi = ( ListViewItem )selCol.Current ;
string mDir = "" ;
for ( int i = 0 ; i < lvi.SubItems.Count ; i++ )
mDir += lvi.SubItems[ i ].Text + " ," ;
string str = mDir.Substring ( 0 , mDir.Length-1 ) ;
if ( str == "" ) return ;
//对组件中的字符串开始拖放操作
listView1.DoDragDrop ( str , DragDropEffects.Copy | DragDropEffects.Move ) ;
}
private void treeView1_DragEnter ( object sender , DragEventArgs e )
{
//判断是否目前拖动的数据是字符串,如果是,则拖动符串对目的组件进行拷贝
if ( e.Data.GetDataPresent ( DataFormats.Text ) )
e.Effect = DragDropEffects.Copy ;
else e.Effect = DragDropEffects.None ;
}
private void treeView1_DragDrop ( object sender , DragEventArgs e )
{
//获得进行"Drag"操作中拖动的字符串
string dummy = "temp" ;
string s = ( string ) e.Data.GetData ( dummy.GetType ( ) ) ;
s = s.Substring ( s.IndexOf ( ":" ) + 1 ).Trim ( ) ;
Position.X = e.X ;
Position.Y = e.Y ;
Position = treeView1.PointToClient ( Position ) ;
TreeNode DropNode = this.treeView1.GetNodeAt ( Position ) ;
//在目标组件中加入以此字符串为标题的项目
if ( DropNode != null )
{
TreeNode DragNode = new TreeNode ( s ) ;
treeView1.Nodes.Insert ( DropNode.Index+1 , DragNode ) ;
}
}
}
解决方案 »
- 谁在MonoDevelop平台用C#作开发的,现在要连接mysql 不知道怎么样在ubuntu下安装mysql,谁知道?
- 关于转换
- 一个java和C#编码的问题
- 如何用c#.net实现div分层显示??
- 没有人知道吗,
- if (ds.Tables[0].Rows.Count != 0) 出错 无法找到表 0。
- 为什么vs2003与vs2005中的datagrid控件效率会差这么多(DATAGRIDVIEW也好不到哪里去)
- nhibernate中用linq条件查询,tolist全部都是重复记录
- 关于字符串处理的问题
- Windows Service 中New Timer造成内存无止境增长的问题
- 有没有办法让远程机器弹出一个对话框?
- 关闭正在运行的某个程序
我也是借花献佛,你自己再钻研一下啦,祝好运!