a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

用C#.NET實現拖放操作

2023-05-18 12:33:16
字體:
來源:轉載
供稿:網友

在應用程序中,是通過處理一系列事件,如DragEnter,DragLeave和DragDrop事件來實現在Windows應用程序中的拖放操作的。通過使用這些事件參數中的可用信息,可以輕松實現拖放操作。
拖放操作在代碼中是通過三步實現的,首先是啟動拖放操作,在需要拖動數據的控件上實現MouseDown事件響應代碼,并調用DoDragDrop()方法;其次是實現拖放效果,在目標控件上添加DragEnter事件響應代碼,使用DragDropEffects枚舉類型實現移動或復制等拖動效果;最后是放置數據操作,在目標控件上添加DragDrop響應代碼,把數據添加到目標控件中。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DragDrop
{
 /// <summary>
 /// Form1 的摘要說明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.ListBox listBox1;
  private System.Windows.Forms.ListBox listBox2;
  /// <summary>
  /// 必需的設計器變量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗體設計器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 調用后添加任何構造函數代碼
   //
  }

  /// <summary>
  /// 清理所有正在使用的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }
  #region Windows 窗體設計器生成的代碼
  /// <summary>
  /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {
   this.listBox1 = new System.Windows.Forms.ListBox();
   this.listBox2 = new System.Windows.Forms.ListBox();
   this.SuspendLayout();
   //
   // listBox1
   //
   this.listBox1.ItemHeight = 12;
   this.listBox1.Location = new System.Drawing.Point(32, 24);
   this.listBox1.Name = "listBox1";
   this.listBox1.Size = new System.Drawing.Size(120, 280);
   this.listBox1.TabIndex = 0;
   this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDown);
   //
   // listBox2
   //
   this.listBox2.ItemHeight = 12;
   this.listBox2.Location = new System.Drawing.Point(248, 24);
   this.listBox2.Name = "listBox2";
   this.listBox2.Size = new System.Drawing.Size(120, 280);
   this.listBox2.TabIndex = 0;
   this.listBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.listBox2_DragDrop);
   this.listBox2.DragEnter += new System.Windows.Forms.DragEventHandler(this.listBox2_DragEnter);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(408, 333);
   this.Controls.Add(this.listBox1);
   this.Controls.Add(this.listBox2);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 應用程序的主入口點。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   this.listBox1.AllowDrop = true;
   this.listBox2.AllowDrop = true;
   this.listBox1.Items.Add("a");
   this.listBox1.Items.Add("b");
   this.listBox1.Items.Add("c");
  }

  private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   this.listBox1.DoDragDrop(this.listBox1.Items[this.listBox1.SelectedIndex],DragDropEffects.Move);
  }

  private void listBox2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
  {
   if(e.Data.GetDataPresent("Text"))
   {
    e.Effect = DragDropEffects.Move;
   }
  }

  private void listBox2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
  {
   this.listBox2.Items.Add(e.Data.GetData("Text"));
   this.listBox1.Items.Remove(e.Data.GetData("Text"));
  }
 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 福利片在线 | 日韩欧美精品在线 | 欧美性一区二区三区 | 日韩99 | 日韩福利 | 久久亚洲精品中文字幕 | 激情婷婷综合 | 国产高清无密码一区二区三区 | jjzz日本| 精品自拍网 | 操一草| 国产精品久久久久久一区二区三区 | 成人欧美一区二区三区黑人孕妇 | 日韩在线视频播放 | 一区二区三区在线观看视频 | 欧美精三区欧美精三区 | 日本免费一区二区在线观看 | 天堂亚洲| 亚洲国产精品99 | 成人免费网站视频 | 国产精品国产 | www.国产视频 | 午夜精品一区二区三区在线 | 精品一二三区在线观看 | 夜夜夜操操操 | 午夜精品亚洲日日做天天做 | 成人国产一区二区 | 777xacom | 欧美亚洲一 | 久热精品在线 | 免费黄色小视频 | 五月婷婷六月香 | 欧日韩在线 | 日韩欧美一区二区三区久久婷婷 | 欧美一区二区久久久 | 综合久久亚洲 | 九九九久久国产免费 | 亚洲欧美激情精品一区二区 | 亚洲一区视频在线 | 97品白浆高清久久久久久 | 伊人激情四射 |