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 3

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


advertisement
Drag and Drop Images
Using the same project, drag and drop a PictureBox control onto the default Form1 (see Figure 5).

 
Figure 5. Adding a PictureBox Control to Form1.

In the Form1_Load event, set the PictureBox control with the properties as shown below:

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 '---set the control to allow drop--- With PictureBox1 .AllowDrop = True .BorderStyle = BorderStyle.FixedSingle .SizeMode = PictureBoxSizeMode.StretchImage End With ...

Implementing Drop
As usual, service the DragEnter event so that you can change the mouse pointer appropriately when the mouse hovers over the PictureBox control. The only difference this time is that you check if the data to be dropped onto the PictureBox control is of type image:

Private Sub PictureBox1_DragEnter( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms. _ DragEventArgs) _ Handles PictureBox1.DragEnter '---if the data to be dropped is an image format--- If (e.Data.GetDataPresent(DataFormats.Bitmap)) 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--- PictureBox1.BorderStyle = BorderStyle.Fixed3D End If End Sub

For the DragDrop event, you will first verify that the dropped object is of type image and then set the PictureBox control to display the dropped image:



Private Sub PictureBox1_DragDrop( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms. _ DragEventArgs) _ Handles PictureBox1.DragDrop '---if the data to be dropped is a bitmap format--- If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then '---set the control to display ' the text being dropped--- PictureBox1.Image = e.Data.GetData( _ DataFormats.Bitmap) End If '---set the border style back to its original--- PictureBox1.BorderStyle = BorderStyle.FixedSingle End Sub

Lastly, for the DragLeave event, change the border style of the PictureBox back to its original:

Private Sub PictureBox1_DragLeave( _ ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles PictureBox1.DragLeave '---set the borderstyle back to ' its original--- PictureBox1.BorderStyle = _ BorderStyle.FixedSingle End Sub

Press F5 to test the application. You can drag an image from Microsoft Word and drop it onto the PictureBox control, which will then display the image. Interestingly, dragging and dropping an image from WordPad does not cause the PictureBox control to display the image—but you'll see more about that later.

Implementing Drag
You need to write some code so that you can drag the image displayed in the PictureBox control to someplace else, such as another control or Microsoft Word (or WordPad).

First, handle the MouseDown event handler. The control fires this event when the user clicks the image in the PictureBox control:

Private Sub PictureBox1_MouseDown( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms. _ MouseEventArgs) _ Handles PictureBox1.MouseDown If PictureBox1.Image IsNot _ Nothing Then PictureBox1.DoDragDrop( _ PictureBox1.Image, _ DragDropEffects.Move Or _ DragDropEffects.Copy) End If End Sub

Here, you use the DoDragDrop() method of the PictureBox control to copy the image displayed in the PictureBox control. The second parameter of this method indicates the type of drag operations that can occur (in this case it is either a move or copy operation).

You can call the DoDragDrop() method either in the MouseDown or the MouseMove event.
That's it! You can now drag and drop the image displayed in the PictureBox control to another control. But this time, you'll find that you can drag the image onto WordPad—but not into Microsoft Word.

Here's a recap of what you have so far:

  • You can drag a picture from Microsoft Word and drop it onto the PictureBox control. But you cannot drag and drop from WordPad.
  • When you try to drag the image from the PictureBox control onto Word, it does not accept the picture. But you can drag and drop the same image onto WordPad.
To understand why you can drop an image from Word but not from WordPad, you need to modify your application so that you can discover the exact data types that a drop operation passes to the PictureBox control.



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