Using Drag and Drop

By default, a form and the controls on it do not support drag and drop. A text box will support copy and paste with Ctrl+C and Ctrl+V, but will not allow you to drag a little piece of text onto the text box. It will also not allow you to grab the text in a text box and drag it elsewhere.

Making a Control a Drop Target

Changing a control on your form so that it can act as a drop target requires three steps:

  1. Change the AllowDrop property to True .

  2. Write code to handle the DragEnter event.

  3. Write code to handle the DragDrop event.

Open Form1 of the AdvancedUI project in the designer, and select textBox2 , the ordinary text box. In the Properties window, find the AllowDrop property and change it to True . Then click the lightning bolt at the top of the Properties window to display events. Find DragEnter in the list and double-click it to add a handler. Add code to the handler so that it reads like this:

 
 private: System::Void textBox2_DragEnter(System::Object *  sender,                                System::Windows::Forms::DragEventArgs *  e) {    if (e->Data->GetDataPresent(DataFormats::Text))        e->Effect = DragDropEffects::Copy;    else        e->Effect = DragDropEffects::None; } 

This code uses the DragEventArgs parameter to change the cursor style as the user drags something over textBox2 . If the data being dragged is text, the familiar copy cursor is displayed. If it's any other format, the slashed circle icon that indicates you can't drop your data here is displayed.

If you've shown the user a copy cursor, there is every chance the user will let go of the drag and drop over textBox2 . That will trigger a DragDrop event. Back in the designer, use the event view of the Properties window to add a handler for DragDrop , and then edit its code to read like this:

 
 private: System::Void textBox2_DragDrop(System::Object *  sender,                                 System::Windows::Forms::DragEventArgs *  e) {    textBox2->Text = e->Data->GetData(DataFormats::Text)->ToString(); } 

The single line of code inside this handler just gets the data that was dropped, makes sure it's a string, and puts it into the Text property of the text box. Notice that even though this is the DragDrop handler for textBox2 , you could put the data wherever you wanted. It's up to you to write a meaningful and consistent user interface.

Build the project and run it to test dragging and dropping. Open some other application (Word, Notepad, or Internet Explorer will all do) and highlight some text. Click and hold to drag the text, and drag it over the form as it runs. You should see the "No Drop" icon (the slashed circle) as you drag the text scrap over the background of the form, or any of the controls other than textBox2 . When you drag over textBox2 , the cursor will change to the arrow with a small + that indicates a copy. Let go, and the text you dragged appears in the text box.

Now try dragging something else. For example, open Windows Explorer and drag a file over the surface of the application. Even as you drag it over textBox2 , the cursor doesn't change, because this application doesn't accept files as drag-and-drop data.

Try one more test before closing the application. Try dragging information out of one of the text boxes: highlight the text with the mouse, and then click and drag. Nothing happens. Although textBox2 has been enabled as a drop target, none of the boxes has been enabled as a drag source.

Making a Control a Drag Source

Enabling a text box as a drag source is a little trickier than enabling one to be a drop target. The first decision to make is which user behavior will start the dragging? It's usually a click-and-hold on the mouse, so you will handle the MouseDown event in the text box. But, of course, users click the mouse in a text box for many reasons other than to drag and drop. When you add a handler for the MouseDown event in the form itself, you prevent ordinary mouse behavior from happening in that text boxincluding such simple tasks as selecting the text you might want to drag! There are some complicated ways around it: A simple one is to support dragging from your text box with a right-click and drag rather than a left-click.

In the designer for Form1 , select textBox1 and switch to the Events view in the Properties window. Double-click MouseDown to add a handler. Edit the handler so it reads like this:

 
 private: System::Void textBox1_MouseDown(System::Object *  sender,                                 System::Windows::Forms::MouseEventArgs *  e) {     if (e->Button == System::Windows::Forms::MouseButtons::Right)         textBox1->DoDragDrop(textBox1->Text, DragDropEffects::Copy); } 

That's all it takes to hand the contents of your text box to the framework, ready to be dropped on a waiting drop target, on this same application or another one. Build and run the project, type a number into the upper text box, and then select the number and right-click and drag it into the lower text box. It's a nice intuitive interface. Also, try dragging into Notepad or some other drop-target application, just to demonstrate to yourself that it works.

It's actually simpler to drag from other kinds of controls, such as list boxes, than from a text box, because the act of clicking the mouse on those controls doesn't wipe out the user's selection. Experiment a little, and you'll be ready to add full mouse support to all your Windows applications.



Microsoft Visual C++. NET 2003 Kick Start
Microsoft Visual C++ .NET 2003 Kick Start
ISBN: 0672326000
EAN: 2147483647
Year: 2002
Pages: 141
Authors: Kate Gregory

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net