Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Implement Drag and Drop in Your Windows Forms Applications : Page 2

Find out how to empower your users by supporting drag and drop features in your Windows Forms applications.


advertisement
Drag and Drop Text
Here's an example application that implements drag and drop. Using Visual Studio 2005, create a new Windows Forms application and name it DragAndDrop. Coding drag and drop operations differs slightly for some controls and for varying data types. This section shows you how to drag and drop some text into a TextBox control.

Populate the default Form1 with a TextBox control (see Figure 2), and set these two TextBox properties as follows:

Multiline-True BorderStyle-Fixed3D

 
Figure 2. TextBox Control in Form1.
Switch to the code view for Form1 and declare the following constant:

Public Class Form1 Const CtrlMask As Byte = 8

In the Form1_Load event, set the TextBox's AllowDrop property to True:



Private Sub Form1_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load '---set the control to allow drop--- TextBox1.AllowDrop = True End Sub

Next, you service the DragEnter event. As mentioned earlier, the control fires this event when you drag something into the control. Here, you will determine if the operation is copy or move, (depending on whether the user is holding down the Ctrl key) and then set the mouse pointer accordingly. To check for this special keystroke, you AND the KeyState property with a CtrlMask (defined as a byte with a value of 8). In addition, change the border style of the TextBox control to FixedSingle so that it serves as a visual cue to the user. Here's the code:

Private Sub TextBox1_DragEnter( _ ByVal sender As Object, _ ByVal e As _ System.Windows.Forms.DragEventArgs) _ Handles TextBox1.DragEnter '---if the data to be dropped is a text format--- If (e.Data.GetDataPresent(DataFormats.Text)) Then '---determine if this is a copy or move--- If (e.KeyState And CtrlMask) = CtrlMask Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.Move End If '---change the border style of the control--- TextBox1.BorderStyle = BorderStyle.FixedSingle End If End Sub

When a user drops the dragged text, you handle the DragDrop event:

Private Sub TextBox1_DragDrop( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) _ Handles TextBox1.DragDrop '---if the data to be dropped is a text format--- If (e.Data.GetDataPresent(DataFormats.Text)) Then '---set the control to display ' the text being dropped--- TextBox1.Text = e.Data.GetData( _ DataFormats.Text) End If '---set the borderstyle back to its original--- TextBox1.BorderStyle = BorderStyle.Fixed3D End Sub

You can now press F5 to test the application. Figure 3 shows the mouse pointer when a user performs a move (top of the figure) and a copy (bottom of the figure) operation on the TextBox control. To perform a copy operation, simply hold the CTRL key when dragging your mouse. Figure 4 shows the pointer when a user drags an object over a control that does not (bottom of the figure) accept a drop.

 
Figure 3. Move and Copy Mouse Pointers.
 
Figure 4. Dragging Out of a Control.
With the ability to drag and drop text under your belt, you can move on to dragging and dropping images to and from PictureBox controls.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date