Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)qqpublished in(发表于) 2014/7/11 9:27:39 C#教程:拖放技术的典型应用实例
C#教程:拖放技术的典型应用实例|方法
拖放技术的典型应用在应用程序中,用户有时希望将数据从一个控件中拖到另一个控件中,此时就需要用到拖放技术。图1和图2演示在两个文本框之间拖放数据。图1 拖放前图2 拖放后程序开发步骤如下所示。(1)新建一个窗体,在窗体中添加两个Label控件和两个TextBox控件,并将两个TextBox控件分别命名为txtDataTart和txtScoure。(2)将txtDataTart文本框的AllowDrop属性设置为True。(3)程序主要代码如下所示:private void txtDataTart_DragDrop(object sender, DragEventArgs e){txtDataTart.Text = e.Data.GetData(DataFormats.Text).ToString();}private void txtDataTart_DragEnter(object sender, DragEventArgs e){e.Effect = DragDropEffects.Copy;}private void txtScoure_MouseMove(object sender, MouseEventArgs e){if ((e.Button & MouseButtons.Left) == MouseButtons.Left){string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf("")).LastIndexOf(""));reportPath += @"sl3293dwarro.cur";MyNoDropCursor = new Cursor(reportPath);DragDropEffects dropEffect = this.txtScoure.DoDragDrop(this.txtScoure.Text, DragDropEffects.Copy | Drag DropEffects.Link);}}完整程序代码如下:★ ★★★★frmDoDragDrop.cs窗体代码文件完整程序代码★★★★★using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace _2_01{public partial class frmDoDragDrop : Form{public frmDoDragDrop(){InitializeComponent();}private Cursor MyNoDropCursor;private void txtDataTart_DragDrop(object sender, DragEventArgs e){txtDataTart.Text = e.Data.GetData(DataFormats.Text).ToString();}private void txtDataTart_DragEnter(object sender, DragEventArgs e){e.Effect = DragDropEffects.Copy;}private void txtScoure_MouseMove(object sender, MouseEventArgs e){if ((e.Button & MouseButtons.Left) == MouseButtons.Left){string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf("")).LastIndexOf(""));reportPath += @"3dwarro.cur";MyNoDropCursor = new Cursor(reportPath);DragDropEffects dropEffect = this.txtScoure.DoDragDrop(this.txtScoure.Text, DragDropEffects.Copy | DragDropEffects.Link);}}private void txtDataTart_DragOver(object sender, DragEventArgs e){this.Cursor = MyNoDropCursor;}private void frmDoDragDrop_Load(object sender, EventArgs e){}}}★ ★★★★frmDoDragDrop.designer.cs窗体设计文件完整程序代码★★★★★namespace _2_01{partial class frmDoDragDrop{/// /// 必需的设计器变量。/// private System.ComponentModel.IContainer components = null;/// /// 清理所有正在使用的资源。/// /// 如果应释放托管资源,为 true;否则为 false。protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码/// /// 设计器支持所需的方法 - 不要/// 使用代码编辑器修改此方法的内容。/// private void InitializeComponent(){this.label1 = new System.Windows.Forms.Label();this.label2 = new System.Windows.Forms.Label();this.txtScoure = new System.Windows.Forms.TextBox();this.txtDataTart = new System.Windows.Forms.TextBox();this.SuspendLayout();//// label1//this.label1.AutoSize = true;this.label1.Location = new System.Drawing.Point(26, 20);this.label1.Name = "label1";this.label1.Size = new System.Drawing.Size(53, 12);
赞