Drag Drop Sample : Drag Drop « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » Drag Drop 
23.59.3.Drag Drop Sample
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

  public class DragDropForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.ListBox lbDragDropSource;
    private System.Windows.Forms.Splitter splitterCentral;
    private System.Windows.Forms.TextBox txtMain;

    public DragDropForm()
    {
      InitializeComponent();

      this.lbDragDropSource.Items.Add("<html>");
      this.lbDragDropSource.Items.Add("<head>");
      this.lbDragDropSource.Items.Add("<title>");
      this.lbDragDropSource.Items.Add("</title>");
      this.lbDragDropSource.Items.Add("</head>");
      this.lbDragDropSource.Items.Add("<body>");
      this.lbDragDropSource.Items.Add("</body>");
      this.lbDragDropSource.Items.Add("</html>");
    }

    private void InitializeComponent()
    {
      this.lbDragDropSource = new System.Windows.Forms.ListBox();
      this.splitterCentral = new System.Windows.Forms.Splitter();
      this.txtMain = new System.Windows.Forms.TextBox();
      this.SuspendLayout();
      // 
      // lbDragDropSource
      // 
      this.lbDragDropSource.Dock = System.Windows.Forms.DockStyle.Left;
      this.lbDragDropSource.Font = new System.Drawing.Font("Microsoft Sans Serif"12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.lbDragDropSource.IntegralHeight = false;
      this.lbDragDropSource.ItemHeight = 20;
      this.lbDragDropSource.Name = "lbDragDropSource";
      this.lbDragDropSource.Size = new System.Drawing.Size(152301);
      this.lbDragDropSource.TabIndex = 0;
      this.lbDragDropSource.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbDragDropSource_MouseDown);
      this.lbDragDropSource.QueryContinueDrag += new System.Windows.Forms.QueryContinueDragEventHandler(this.lbDragDropSource_QueryContinueDrag);
      // 
      // splitterCentral
      // 
      this.splitterCentral.Location = new System.Drawing.Point(1520);
      this.splitterCentral.Name = "splitterCentral";
      this.splitterCentral.Size = new System.Drawing.Size(3301);
      this.splitterCentral.TabIndex = 1;
      this.splitterCentral.TabStop = false;
      // 
      // txtMain
      // 
      this.txtMain.AcceptsReturn = true;
      this.txtMain.AcceptsTab = true;
      this.txtMain.AllowDrop = true;
      this.txtMain.Dock = System.Windows.Forms.DockStyle.Fill;
      this.txtMain.Font = new System.Drawing.Font("Microsoft Sans Serif"12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.txtMain.Location = new System.Drawing.Point(1550);
      this.txtMain.Multiline = true;
      this.txtMain.Name = "txtMain";
      this.txtMain.Size = new System.Drawing.Size(333301);
      this.txtMain.TabIndex = 2;
      this.txtMain.Text = "";
      this.txtMain.DragOver += new System.Windows.Forms.DragEventHandler(this.txtMain_DragOver);
      this.txtMain.DragDrop += new System.Windows.Forms.DragEventHandler(this.txtMain_DragDrop);
      this.txtMain.TextChanged += new System.EventHandler(this.txtMain_TextChanged);
      // 
      // DragDropForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(488301);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.txtMain,
                                      this.splitterCentral,
                                      this.lbDragDropSource});
      this.Name = "DragDropForm";
      this.Text = "Drag and Drop Sample";
      this.ResumeLayout(false);

    }
    static void Main() 
    {
      Application.Run(new DragDropForm());
    }

    private void lbDragDropSource_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      int nSelectedIndex = lbDragDropSource.SelectedIndex;
      string strItem = (string)lbDragDropSource.Items[nSelectedIndex];

      DragDropEffects dde = lbDragDropSource.DoDragDrop(strItem,
        DragDropEffects.Copy);

      if (DragDropEffects.None == dde)
        MessageBox.Show("Our drag and drop offer was not accepted.");
    }

    private void txtMain_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
    {
      if (e.Data.GetDataPresent(typeof(string)))
      {
        e.Effect = DragDropEffects.Copy;
      }
    }

    private void txtMain_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
    {
      if (e.Data.GetDataPresent(typeof(string)))
      {
        string strData = (string)e.Data.GetData(typeof(string));
        txtMain.AppendText(strData);
      }
    }

    private void lbDragDropSource_QueryContinueDrag(object sender, System.Windows.Forms.QueryContinueDragEventArgs e)
    {
      // DO NOT DO THIS
      // e.Action = DragAction.Continue;
    }

    private void txtMain_TextChanged(object sender, System.EventArgs e)
    {
    
    }
  }
23.59.Drag Drop
23.59.1.Drag and Drop TextBoxDrag and Drop TextBox
23.59.2.File Drop Target
23.59.3.Drag Drop Sample
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.