Copy and paste Object : Drag Drop « GUI « VB.Net Tutorial






Copy and paste Object
' Requires a reference to System.Xml.
Imports System.Xml.Serialization
Imports System.IO
Imports System.Windows.Forms

public class CopyPasteEmployee
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class



Public Class Form1
    <Serializable()> _
    Public Class Employee
        Public FirstName As String
        Public LastName As String
        Public Sub New()
        End Sub
        Public Sub New(ByVal first_name As String, ByVal last_name As String)
            FirstName = first_name
            LastName = last_name
        End Sub
    End Class

    ' Copy the Employee to the clipboard.
    Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
        Dim emp As New Employee(txtFirstName.Text, txtLastName.Text)
        Dim data_object As New DataObject
        data_object.SetData("Employee", emp)
        Clipboard.SetDataObject(data_object)
    End Sub

    ' Paste data from the clipboard.
    Private Sub btnPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPaste.Click
        Dim data_object As IDataObject = Clipboard.GetDataObject()
        If data_object.GetDataPresent("Employee") Then
            Dim emp As Employee = DirectCast(data_object.GetData("Employee"), Employee)
            txtPasteFirstName.Text = emp.FirstName
            txtPasteLastName.Text = emp.LastName
        End If
    End Sub
End Class
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.btnPaste = New System.Windows.Forms.Button
        Me.txtPasteLastName = New System.Windows.Forms.TextBox
        Me.txtPasteFirstName = New System.Windows.Forms.TextBox
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.btnCopy = New System.Windows.Forms.Button
        Me.txtLastName = New System.Windows.Forms.TextBox
        Me.txtFirstName = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label1 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'btnPaste
        '
        Me.btnPaste.Location = New System.Drawing.Point(232, 96)
        Me.btnPaste.Name = "btnPaste"
        Me.btnPaste.Size = New System.Drawing.Size(48, 24)
        Me.btnPaste.TabIndex = 22
        Me.btnPaste.Text = "Paste"
        '
        'txtPasteLastName
        '
        Me.txtPasteLastName.Location = New System.Drawing.Point(72, 112)
        Me.txtPasteLastName.Name = "txtPasteLastName"
        Me.txtPasteLastName.Size = New System.Drawing.Size(136, 20)
        Me.txtPasteLastName.TabIndex = 21
        '
        'txtPasteFirstName
        '
        Me.txtPasteFirstName.Location = New System.Drawing.Point(72, 88)
        Me.txtPasteFirstName.Name = "txtPasteFirstName"
        Me.txtPasteFirstName.Size = New System.Drawing.Size(136, 20)
        Me.txtPasteFirstName.TabIndex = 20
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.Location = New System.Drawing.Point(8, 112)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(54, 13)
        Me.Label3.TabIndex = 19
        Me.Label3.Text = "Last Name"
        '
        'Label4
        '
        Me.Label4.AutoSize = True
        Me.Label4.Location = New System.Drawing.Point(8, 88)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(53, 13)
        Me.Label4.TabIndex = 18
        Me.Label4.Text = "First Name"
        '
        'btnCopy
        '
        Me.btnCopy.Location = New System.Drawing.Point(232, 16)
        Me.btnCopy.Name = "btnCopy"
        Me.btnCopy.Size = New System.Drawing.Size(48, 24)
        Me.btnCopy.TabIndex = 17
        Me.btnCopy.Text = "Copy"
        '
        'txtLastName
        '
        Me.txtLastName.Location = New System.Drawing.Point(72, 32)
        Me.txtLastName.Name = "txtLastName"
        Me.txtLastName.Size = New System.Drawing.Size(136, 20)
        Me.txtLastName.TabIndex = 16
        Me.txtLastName.Text = "AAAAAA"
        '
        'txtFirstName
        '
        Me.txtFirstName.Location = New System.Drawing.Point(72, 8)
        Me.txtFirstName.Name = "txtFirstName"
        Me.txtFirstName.Size = New System.Drawing.Size(136, 20)
        Me.txtFirstName.TabIndex = 15
        Me.txtFirstName.Text = "BBBBB"
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(8, 32)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(54, 13)
        Me.Label2.TabIndex = 14
        Me.Label2.Text = "Last Name"
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(8, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(53, 13)
        Me.Label1.TabIndex = 13
        Me.Label1.Text = "First Name"
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(288, 139)
        Me.Controls.Add(Me.btnPaste)
        Me.Controls.Add(Me.txtPasteLastName)
        Me.Controls.Add(Me.txtPasteFirstName)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.btnCopy)
        Me.Controls.Add(Me.txtLastName)
        Me.Controls.Add(Me.txtFirstName)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Name = "Form1"
        Me.Text = "CopyPasteEmployee"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents btnPaste As System.Windows.Forms.Button
    Friend WithEvents txtPasteLastName As System.Windows.Forms.TextBox
    Friend WithEvents txtPasteFirstName As System.Windows.Forms.TextBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents btnCopy As System.Windows.Forms.Button
    Friend WithEvents txtLastName As System.Windows.Forms.TextBox
    Friend WithEvents txtFirstName As System.Windows.Forms.TextBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label

End Class








14.80.Drag Drop
14.80.1.Copy and paste ObjectCopy and paste Object
14.80.2.Drop a fileDrop a file