Control study-> drag-and-drop data operation example

Source: Internet
Author: User

(1). Function Description

An example of how to use the dragenter dragdrop dragleave dragover querycontinuedrag event

(2). Image example

Demonstrate dragging button button1.text "button" to textbox

Before dragging:

After dragging:

(3 ).Code

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;

Namespace drag-and-drop data operation _ key event _
{
/// <Summary>
/// Summary of form1.
/// </Summary>
Public class form1: system. Windows. Forms. Form
{
Private system. Windows. Forms. Button button1;
Private system. Windows. Forms. textbox textbox1;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;

Public form1 ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();

//
// Todo: add Any constructor code after initializecomponent calls
//
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. button1 = new system. Windows. Forms. Button ();
This. textbox1 = new system. Windows. Forms. Textbox ();
This. suspendlayout ();
//
// Button1
//
This. button1.location = new system. Drawing. Point (112, 72 );
This. button1.name = "button1 ";
This. button1.tabindex = 0;
This. button1.text = "button1 ";
This. button1.querycontinuedrag + = new system. Windows. Forms. querycontinuedrageventhandler (this. button#querycontinuedrag );
This. button1.mousedown + = new system. Windows. Forms. mouseeventhandler (this. button#mousedown );
//
// Textbox1
//
This. textbox1.accessibledescription = "AAAAA ";
This. textbox1.accessiblename = "bbbbbbbb ";
This. textbox1.allowdrop = true;
This. textbox1.location = new system. Drawing. Point (64,160 );
This. textbox1.multiline = true;
This. textbox1.name = "textbox1 ";
This. textbox1.size = new system. Drawing. Size (168, 48 );
This. textbox1.tabindex = 1;
This. textbox1.text = "";
This. textbox1.dragover + = new system. Windows. Forms. drageventhandler (this. textbox#dragover );
This. textbox1.dragdrop + = new system. Windows. Forms. drageventhandler (this. textbox#dragdrop );
This. textbox1.dragenter + = new system. Windows. Forms. drageventhandler (this. textbox#dragenter );
This. textbox1.dragleave + = new system. eventhandler (this. textbox?dragleave );
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (292,266 );
This. Controls. Add (this. textbox1 );
This. Controls. Add (this. button1 );
This. Name = "form1 ";
This. startposition = system. Windows. Forms. formstartposition. centerscreen;
This. Text = "form1 ";
This. resumelayout (false );

}
# Endregion

/// <Summary>
/// ApplicationProgram.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}

Private void button#querycontinuedrag (Object sender, system. Windows. Forms. querycontinuedrageventargs E)
{
If (E. escapepressed)
E. Action = dragaction. Cancel;
}

Private void button#mousedown (Object sender, system. Windows. Forms. mouseeventargs E)
{
Button1.dodragdrop (button1.text, dragdropeffects. Copy | dragdropeffects. Move );
}

Private void textboxincludragenter (Object sender, system. Windows. Forms. drageventargs E)
{
If (E. Data. getdatapresent (dataformats. Text ))
E. effect = dragdropeffects. copy;
Else
E. effect = dragdropeffects. None;
}

Private void textboxincludragdrop (Object sender, system. Windows. Forms. drageventargs E)
{
// Use the keystate attribute
If (E. keystate & (1 <3 ))! = 0)
{
If (E. allowedeffect & dragdropeffects. Copy )! = 0)
{
E. effect = dragdropeffects. copy;
Textbox1.text = E. Data. getdata (dataformats. Text). tostring ();
}
}
Else
{
E. effect = dragdropeffects. move;
Textbox1.text = E. Data. getdata (dataformats. Text). tostring ();
}
}

Private void textbox1_dragleave (Object sender, system. eventargs E)
{
Textbox1.text = "execute dragleave ";
}

Private void textboxincludragover (Object sender, system. Windows. Forms. drageventargs E)
{
Textbox1.text = "execute dragover ";
}
}
}

(4). Sample download

Http://www.cnblogs.com/files/chengking/drag and drop Data Operations (click the button for event details. rar

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.