|
|
Title | Drag and drop data in multiple formats in VB .NET |
Description | This example shows how to drag and drop data in multiple formats in VB .NET. It uses a DataObject's SetData method to load data in different formats and then saves the DataObject in the Clipboard. |
Keywords | drag and drop, formats, multiple formats, VB.NET |
Categories | VB.NET, Controls |
|
|
This example displays text in a RichTextBox. When you click and drag, it drags data in Rich Text, text, and HTML formats. If you drop the data on the drop target, the program displays each of these formats. It also displays the Rich Text data as text so you can see the Rich Text codes.
When the user presses the mouse down over the drag source control, the lblDragSource_MouseDown event handler creates a new DataObject. It calls the object's SetData method to save the data as Rich Text and as text. It then composes an HTML document string and calls SetData to store it in the DataObject. The program then calls the drag source control's DoDragDrop method passing it the DataObject.
|
|
Private Sub lblDragSource_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
lblDragSource.MouseDown
' Make a DataObject.
Dim data_object As New DataObject
' Add the data in various formats.
data_object.SetData(DataFormats.Rtf, rchSource.Rtf)
data_object.SetData(DataFormats.Text, rchSource.Text)
Dim html_text As String
html_text = "<HTML>" & vbCrLf
html_text &= " <HEAD>The Quick Brown Fox</HEAD>" & _
vbCrLf
html_text &= " <BODY>" & vbCrLf
html_text &= rchSource.Text & vbCrLf
html_text &= " </BODY>" & vbCrLf & "</HTML>"
data_object.SetData(DataFormats.Html, html_text)
' Start the drag.
lblDragSource.DoDragDrop(data_object, _
DragDropEffects.Copy)
End Sub
|
|
When the user drags over the drop target, the lblDropTarget_DragEnter event handler uses the GetDataPresent method to see if the data contains Rich Text, text, or HTML data. If it contains any of those types of data, the code allows a Copy operation. Note that this means you can drag text or Rich Text from another application and the program will display it.
|
|
' Allow drop of Text, Integer, and Image.
Private Sub lblDropTarget_DragEnter(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DragEventArgs) Handles _
lblDropTarget.DragEnter
If e.Data.GetDataPresent(DataFormats.Rtf) Or _
e.Data.GetDataPresent(DataFormats.Text) Or _
e.Data.GetDataPresent(DataFormats.Html) _
Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
|
|
When the user drops the data, the lblDropTarget_DragDrop event handler uses GetDataPresent to see which kinds of data are present. For the data types present, it uses GetData to get the data and displays it.
|
|
' Display whatever data we can.
Private Sub lblDropTarget_DragDrop(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DragEventArgs) Handles _
lblDropTarget.DragDrop
If e.Data.GetDataPresent(DataFormats.Rtf) Then
rchTarget.Rtf = _
e.Data.GetData(DataFormats.Rtf).ToString
lblRtf.Text = _
e.Data.GetData(DataFormats.Rtf).ToString
Else
rchTarget.Text = ""
lblRtf.text = ""
End If
If e.Data.GetDataPresent(DataFormats.Text) Then
lblTarget.Text = _
e.Data.GetData(DataFormats.Text).ToString
Else
lblTarget.Text = ""
End If
If e.Data.GetDataPresent(DataFormats.Html) Then
lblHtml.Text = _
e.Data.GetData(DataFormats.Html).ToString
Else
lblHtml.Text = ""
End If
End Sub
|
|
|
|
|
|