VB.Net Tutorial/GUI/Drag Drop — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:57, 26 мая 2010

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.rupilerServices.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.ruponentModel.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

Drop a file

Imports System.Data
Imports System.Data.OleDb
Imports System.Windows.Forms
public class FileDrop
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    " Allow Copy if there is FileDrop data.
    Private Sub lblDropTarget_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lblDropTarget.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
    " Display the dropped file names.
    Private Sub lblDropTarget_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lblDropTarget.DragDrop
        lstFiles.Items.Clear()
        Dim file_names As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
        For Each file_name As String In file_names
            lstFiles.Items.Add(file_name)
        Next file_name
    End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.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.ruponentModel.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.lstFiles = New System.Windows.Forms.ListBox
        Me.lblDropTarget = New System.Windows.Forms.Label
        Me.SuspendLayout()
        "
        "lstFiles
        "
        Me.lstFiles.Dock = System.Windows.Forms.DockStyle.Fill
        Me.lstFiles.FormattingEnabled = True
        Me.lstFiles.Location = New System.Drawing.Point(0, 48)
        Me.lstFiles.Name = "lstFiles"
        Me.lstFiles.Size = New System.Drawing.Size(274, 160)
        Me.lstFiles.TabIndex = 3
        "
        "lblDropTarget
        "
        Me.lblDropTarget.AllowDrop = True
        Me.lblDropTarget.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.lblDropTarget.Dock = System.Windows.Forms.DockStyle.Top
        Me.lblDropTarget.Location = New System.Drawing.Point(0, 0)
        Me.lblDropTarget.Name = "lblDropTarget"
        Me.lblDropTarget.Size = New System.Drawing.Size(274, 48)
        Me.lblDropTarget.TabIndex = 2
        Me.lblDropTarget.Text = "Drop Target"
        Me.lblDropTarget.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(274, 210)
        Me.Controls.Add(Me.lstFiles)
        Me.Controls.Add(Me.lblDropTarget)
        Me.Name = "Form1"
        Me.Text = "AcceptFiles"
        Me.ResumeLayout(False)
    End Sub
    Friend WithEvents lstFiles As System.Windows.Forms.ListBox
    Friend WithEvents lblDropTarget As System.Windows.Forms.Label
End Class