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

首頁(yè) > 編程 > .NET > 正文

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

2024-07-10 13:10:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

在應(yīng)用程序中,是通過(guò)處理一系列事件,如dragenter,dragleave和dragdrop事件來(lái)實(shí)現(xiàn)在windows應(yīng)用程序中的拖放操作的。通過(guò)使用這些事件參數(shù)中的可用信息,可以輕松實(shí)現(xiàn)拖放操作。
拖放操作在代碼中是通過(guò)三步實(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 的摘要說(shuō)明。
 /// </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"));
  }
 }
}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 合水县| 馆陶县| 定远县| 西畴县| 永定县| 句容市| 东宁县| 中宁县| 云安县| 绍兴市| 沅江市| 五莲县| 广昌县| 嘉兴市| 孟连| 安岳县| 沅陵县| 营口市| 花莲市| 枣阳市| 凌源市| 慈溪市| 荆州市| 定日县| 丹东市| 临城县| 尼勒克县| 阳信县| 霞浦县| 定襄县| 钟山县| 宜春市| 滕州市| 旬邑县| 华安县| 赫章县| 余姚市| 木兰县| 平潭县| 额尔古纳市| 金秀|