国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

用C#.NET實(shí)現(xiàn)拖放操作

2019-11-18 17:06:58
字體:
供稿:網(wǎng)友

在應(yīng)用程序中,是通過處理一系列事件,如DragEnter,DragLeave和DragDrop事件來實(shí)現(xiàn)在Windows應(yīng)用程序中的拖放操作的。通過使用這些事件參數(shù)中的可用信息,可以輕松實(shí)現(xiàn)拖放操作。
拖放操作在代碼中是通過三步實(shí)現(xiàn)的,首先是啟動(dòng)拖放操作,在需要拖動(dòng)數(shù)據(jù)的控件上實(shí)現(xiàn)MouseDown事件響應(yīng)代碼,并調(diào)用DoDragDrop()方法;其次是實(shí)現(xiàn)拖放效果,在目標(biāo)控件上添加DragEnter事件響應(yīng)代碼,使用DragDropEffects枚舉類型實(shí)現(xiàn)移動(dòng)或復(fù)制等拖動(dòng)效果;最后是放置數(shù)據(jù)操作,在目標(biāo)控件上添加DragDrop響應(yīng)代碼,把數(shù)據(jù)添加到目標(biāo)控件中。
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>
  /// 必需的設(shè)計(jì)器變量。
  /// </summary>
  private System.ComponentModel.Container components = null;

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

   //
   // TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
   //
  }

  /// <summary>
  /// 清理所有正在使用的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗體設(shè)計(jì)器生成的代碼
  /// <summary>
  /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內(nèi)容。
  /// </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>
  /// 應(yīng)用程序的主入口點(diǎn)。
  /// </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"));
  }
 }
}

http://maxianghui.VEVb.com/archive/2006/07/07/445155.html


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 冀州市| 德江县| 留坝县| 绵阳市| 齐齐哈尔市| 股票| 镇康县| 蓬安县| 慈溪市| 师宗县| 临城县| 勃利县| 阳朔县| 道孚县| 虎林市| 无棣县| 南丰县| 旌德县| 新安县| 咸丰县| 兴安盟| 丰原市| 丰宁| 靖州| 新乐市| 获嘉县| 堆龙德庆县| 阿尔山市| 阳西县| 巴里| 鄂伦春自治旗| 兖州市| 林口县| 寻甸| 山西省| 大渡口区| 五大连池市| 宣威市| 商都县| 会宁县| 安徽省|