Control study -> Drag拖放数据操作示例

(一).功能描述

   涉及到DragEnter DragDrop DragLeave DragOver QueryContinueDrag事件用法的一个例子

(二).图片示例

演示将按钮Button1.Text文本"Button"拖动到TextBox中

拖动前:

拖动后:

(三).代码

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace 拖放数据操作_按键事件_
{
 ///


 /// Form1 的摘要说明。
 ///

 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.TextBox textBox1;
  ///
  /// 必需的设计器变量。
  ///

  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  ///


  /// 清理所有正在使用的资源。
  ///

  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  ///


  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///

  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(112, 72);
   this.button1.Name = "button1";
   this.button1.TabIndex = 0;
   this.button1.Text = "button1";
   this.button1.QueryContinueDrag += new System.Windows.Forms.QueryContinueDragEventHandler(this.button1_QueryContinueDrag);
   this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
   //
   // textBox1
   //
   this.textBox1.AccessibleDescription = "aaaaa";
   this.textBox1.AccessibleName = "bbbbbb";
   this.textBox1.AllowDrop = true;
   this.textBox1.Location = new System.Drawing.Point(64, 160);
   this.textBox1.Multiline = true;
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(168, 48);
   this.textBox1.TabIndex = 1;
   this.textBox1.Text = "";
   this.textBox1.DragOver += new System.Windows.Forms.DragEventHandler(this.textBox1_DragOver);
   this.textBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.textBox1_DragDrop);
   this.textBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.textBox1_DragEnter);
   this.textBox1.DragLeave += new System.EventHandler(this.textBox1_DragLeave);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(292, 266);
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.button1);
   this.Name = "Form1";
   this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion

  ///


  /// 应用程序的主入口点。
  ///

  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void button1_QueryContinueDrag(object sender, System.Windows.Forms.QueryContinueDragEventArgs e)
  {
   if(e.EscapePressed)
    e.Action=DragAction.Cancel;
  }

  private void button1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   button1.DoDragDrop(button1.Text,DragDropEffects.Copy|DragDropEffects.Move);
  }

  private void textBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
  {
   if(e.Data.GetDataPresent(DataFormats.Text))
    e.Effect=DragDropEffects.Copy;
   else
    e.Effect=DragDropEffects.None;
  }

  private void textBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
  {
   //使用KeyState属性
   if((e.KeyState&(1<<3))!=0)
   {
    if((e.AllowedEffect&DragDropEffects.Copy)!=0)
    {
     e.Effect=DragDropEffects.Copy;
     textBox1.Text=e.Data.GetData(DataFormats.Text).ToString();
    }
   }
   else
   {
    e.Effect=DragDropEffects.Move;
    textBox1.Text=e.Data.GetData(DataFormats.Text).ToString();
   }
  }

  private void textBox1_DragLeave(object sender, System.EventArgs e)
  {
   textBox1.Text="执行 DragLeave";
  }

  private void textBox1_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
  {
   textBox1.Text="执行 DragOver";
  }
 }
}


 (四). 示例下载

          http://www.cnblogs.com/Files/ChengKing/拖放数据操作(按键事件).rar

 

 

 

 

 

 

 



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=497607


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值