Drag and drop inside a container : Drag Drop « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » GUI Windows Form » Drag Drop 




Drag and drop inside a container
Drag and drop inside a container


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form
{
  internal System.Windows.Forms.Label lblDragger;
  public Form1()
  {
    InitializeComponent();
  }

  private void InitializeComponent()
  {
    this.lblDragger = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // lblDragger
    // 
    this.lblDragger.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.lblDragger.Image = new Bitmap("winter.jpg");
    this.lblDragger.Location = new System.Drawing.Point(110105);
    this.lblDragger.Name = "lblDragger";
    this.lblDragger.Size = new System.Drawing.Size(7256);
    this.lblDragger.TabIndex = 2;
    this.lblDragger.MouseUp += new System.Windows.Forms.MouseEventHandler(this.lblDragger_MouseUp);
    this.lblDragger.MouseMove += new System.Windows.Forms.MouseEventHandler(this.lblDragger_MouseMove);
    this.lblDragger.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lblDragger_MouseDown);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(513);
    this.ClientSize = new System.Drawing.Size(292266);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                    this.lblDragger});
    this.Name = "Form1";
    this.Text = "Fake Drag And Drop";
    this.ResumeLayout(false);

  }

  [STAThread]
  static void Main() 
  {
    Application.Run(new Form1());
  }


  private bool isDragging = false;


  private int clickOffsetX, clickOffsetY;


  private void lblDragger_MouseDown(System.Object sender, System.Windows.Forms.MouseEventArgs e)
  {
    isDragging = true;
    clickOffsetX = e.X;
    clickOffsetY = e.Y;
  }

  private void lblDragger_MouseUp(System.Object sender, System.Windows.Forms.MouseEventArgs e)
  {
    isDragging = false;
  }

  private void lblDragger_MouseMove(System.Object sender,
    System.Windows.Forms.MouseEventArgs e)
  {
    if (isDragging == true)
    {
      lblDragger.Left = e.X + lblDragger.Left - clickOffsetX;
      lblDragger.Top = e.Y + lblDragger.Top - clickOffsetY;
    }
  }

}


           
       














Related examples in the same category
1.Image Drop
2.Drag and drop image to another windowDrag and drop image to another window
3.Drag and drop the PictureBoxDrag and drop the PictureBox
4.Fake Drag And DropFake Drag And Drop
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.