form1.vb

来自「vb 应用实例 简单应用 dddddddddddddddddddddddddd」· VB 代码 · 共 293 行

VB
293
字号
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form  "

    Public Sub New()
        MyBase.New()

        InitializeComponent()


    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private components As System.ComponentModel.IContainer

    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox
    Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
    Friend WithEvents TreeView2 As System.Windows.Forms.TreeView
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.PictureBox2 = New System.Windows.Forms.PictureBox()
        Me.TreeView1 = New System.Windows.Forms.TreeView()
        Me.TreeView2 = New System.Windows.Forms.TreeView()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.TextBox2 = New System.Windows.Forms.TextBox()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.BackColor = System.Drawing.SystemColors.Window
        Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.PictureBox1.Location = New System.Drawing.Point(31, 32)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(266, 96)
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(31, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(363, 14)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "图片拖拉:由左拖拉到右(或由右拖拉到左),按Ctrl键可以复制"
        '
        'PictureBox2
        '
        Me.PictureBox2.BackColor = System.Drawing.SystemColors.Window
        Me.PictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.PictureBox2.Location = New System.Drawing.Point(328, 32)
        Me.PictureBox2.Name = "PictureBox2"
        Me.PictureBox2.Size = New System.Drawing.Size(266, 96)
        Me.PictureBox2.TabIndex = 2
        Me.PictureBox2.TabStop = False
        '
        'TreeView1
        '
        Me.TreeView1.ImageIndex = -1
        Me.TreeView1.Location = New System.Drawing.Point(31, 152)
        Me.TreeView1.Name = "TreeView1"
        Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("活在当下", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Accept now", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("love now", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("enjoy now")})}), New System.Windows.Forms.TreeNode("活在自己的当下"), New System.Windows.Forms.TreeNode("是好是坏很难说"), New System.Windows.Forms.TreeNode("知足长乐")})})
        Me.TreeView1.SelectedImageIndex = -1
        Me.TreeView1.Size = New System.Drawing.Size(266, 112)
        Me.TreeView1.Sorted = True
        Me.TreeView1.TabIndex = 3
        '
        'TreeView2
        '
        Me.TreeView2.ImageIndex = -1
        Me.TreeView2.Location = New System.Drawing.Point(328, 152)
        Me.TreeView2.Name = "TreeView2"
        Me.TreeView2.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("产品", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("电脑", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Notebook"), New System.Windows.Forms.TreeNode("Tablet PC"), New System.Windows.Forms.TreeNode("台式电脑")}), New System.Windows.Forms.TreeNode("彩色照相手机")})})
        Me.TreeView2.SelectedImageIndex = -1
        Me.TreeView2.Size = New System.Drawing.Size(266, 112)
        Me.TreeView2.TabIndex = 4
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(31, 288)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(266, 21)
        Me.TextBox1.TabIndex = 5
        Me.TextBox1.Text = "活在当下"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(328, 288)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(266, 21)
        Me.TextBox2.TabIndex = 6
        Me.TextBox2.Text = "Accept now, love now, enjoy now."
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(31, 136)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(388, 14)
        Me.Label2.TabIndex = 7
        Me.Label2.Text = "TreeView拖拉:由左拖拉到右(或由右拖拉到左),按Ctrl键可以复制"
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.Location = New System.Drawing.Point(31, 272)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(363, 14)
        Me.Label3.TabIndex = 8
        Me.Label3.Text = "Text拖拉:由左拖拉到右(或由右拖拉到左),按Ctrl键可以复制"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(624, 334)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label3, Me.Label2, Me.TextBox2, Me.TextBox1, Me.TreeView2, Me.TreeView1, Me.PictureBox2, Me.Label1, Me.PictureBox1})
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Drag & Drop"
        Me.ResumeLayout(False)

    End Sub

#End Region


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
        Dim Image1 As New System.Drawing.Bitmap("..\..\..\car.jpg")
        PictureBox1.Image = Image1

       
        PictureBox1.AllowDrop = True
        PictureBox2.AllowDrop = True

        TreeView1.AllowDrop = True
        TreeView2.AllowDrop = True

       
        TreeView1.Nodes(0).ExpandAll()
        TreeView2.Nodes(0).ExpandAll()

        TextBox1.AllowDrop = True
        TextBox2.AllowDrop = True
    End Sub

    Private Sub PictureBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown, PictureBox2.MouseDown
        If e.Button = MouseButtons.Left Then
            Dim pic1 As PictureBox = CType(sender, PictureBox)
            If Not pic1.Image Is Nothing Then
                pic1.DoDragDrop(pic1.Image, DragDropEffects.Move Or DragDropEffects.Copy)
            End If
        End If
    End Sub

    Const CtrlMask As Byte = 8

    Private Sub PictureBox_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragEnter, PictureBox2.DragEnter
        If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then
            If (e.KeyState And CtrlMask) = CtrlMask Then
                e.Effect = DragDropEffects.Copy
            Else
                e.Effect = DragDropEffects.Move
            End If
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub PictureBox_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragDrop, PictureBox2.DragDrop
        Dim pic1 As PictureBox = CType(sender, PictureBox)
        
        pic1.Image = CType(e.Data.GetData(DataFormats.Bitmap), Bitmap)

        
        If (e.KeyState And CtrlMask) <> CtrlMask Then
            If pic1.Name = "PictureBox1" Then
                PictureBox2.Image = Nothing
            Else
                PictureBox1.Image = Nothing
            End If
        End If
    End Sub


    
   
    Private Sub TreeView_ItemDrag(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView1.ItemDrag, TreeView2.ItemDrag
        If e.Button = MouseButtons.Left Then
            DoDragDrop(e.Item, DragDropEffects.Move Or DragDropEffects.Copy)
        End If
    End Sub

   
    Private Sub TreeView_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragEnter, TreeView2.DragEnter
       
        If (e.Data.GetDataPresent("System.Windows.Forms.TreeNode")) Then
          
            If (e.KeyState And CtrlMask) = CtrlMask Then
                e.Effect = DragDropEffects.Copy
            Else
                e.Effect = DragDropEffects.Move
            End If
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

   
    Private Sub TreeView_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragDrop, TreeView2.DragDrop
        
        Dim OriginationNode As TreeNode = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), TreeNode)

       
        If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", False) Then

           
            Dim pt1 As Point = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))

            Dim DestinationNode As TreeNode = CType(sender, TreeView).GetNodeAt(pt1)

 

            DestinationNode.Nodes.Add(CType(OriginationNode.Clone, TreeNode))

            DestinationNode.Expand()

            If (e.KeyState And CtrlMask) <> CtrlMask Then
                OriginationNode.Remove()
            End If

            'End If
        End If
    End Sub



    Private Sub TextBox_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown, TextBox2.MouseDown
        
        If e.Button = MouseButtons.Left Then
            Dim tb1 As TextBox = CType(sender, TextBox)
            tb1.SelectAll()
            tb1.DoDragDrop(tb1.SelectedText, DragDropEffects.Move Or DragDropEffects.Copy)
        End If
    End Sub

    Private Sub TextBox_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter, TextBox2.DragEnter
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            If (e.KeyState And CtrlMask) = CtrlMask Then
                e.Effect = DragDropEffects.Copy
            Else
                e.Effect = DragDropEffects.Move
            End If
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub


    Private Sub TextBox_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop, TextBox2.DragDrop

        Dim tb1 As TextBox = CType(sender, TextBox)
        tb1.Text = e.Data.GetData(DataFormats.Text).ToString

        If (e.KeyState And CtrlMask) <> CtrlMask Then
            If tb1.Name = "TextBox1" Then
                TextBox2.Text = ""
            Else
                TextBox1.Text = ""
            End If
        End If
    End Sub

End Class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?