麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > 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"));
  }
 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久新网址 | 欧美77| 欧美日韩在线视频一区二区 | 日韩精品中文字幕在线播放 | 黄色特级视频 | 亚洲免费视 | 日韩一级免费毛片 | 亚洲91网 | 懂色av懂色aⅴ精彩av | 久久久久久久一区二区三区 | 色女生影院 | www.99久久久| 久久免费视频一区二区三区 | 老师你怎么会在这第2季出现 | 2级毛片 | 97黄色网| h色网站免费观看 | 中文字幕精品在线播放 | 性片网站| 久草在线综合网 | 欧美日韩1区2区 | 免费毛片小视频 | 日产精品一区二区三区在线观看 | 国产1区视频 | 日本网站一区 | 成人毛片100免费观看 | 毛片a区 | 欧美一级毛片欧美一级成人毛片 | 国产乱淫av| 午夜精品福利视频 | 国产美女视频黄a视频免费 日韩黄色在线播放 | 欧美中文字幕在线视频 | 美女福利视频国产 | 久久精品电影网 | 日韩欧美高清片 | 国产自91精品一区二区 | 4p一女两男做爰在线观看 | 亚洲第五色综合网 | jizzzxxxxhd| xnxx 日本19| 国产九九九九 |