Showing posts with label drag control. Show all posts
Showing posts with label drag control. Show all posts

Friday, 3 February 2012

Drag Text from another Program


Need 1 TextBox  and set AllowDrop property of TextBox1  to True.

Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            e.Effect = DragDropEffects.Copy
        End If
    End Sub

Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
        TextBox1.Text = e.Data.GetData(DataFormats.Text).ToString
    End Sub

Drag Content from one TextBox and Drop to another


Need 2 TextBox for doing this program and Set AllowDrop property of TextBox2  to True.
       
Private Sub TextBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
        TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Move)
    End Sub

Private Sub TextBox2_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
        TextBox2.Text &= e.Data.GetData(DataFormats.Text).ToString
        TextBox1.Text = ""
    End Sub

Private Sub TextBox2_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            e.Effect = DragDropEffects.Move
        End If
    End Sub

Friday, 20 January 2012

Drag Control in Window Form


For this, just need 1 Button.

Public Dragging As Boolean                                                   
Public mousex, mousey As Integer                                             

Private Sub Button1_MouseUp(ByVal sender As System.Object, ByVal e As        System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp                 
        If Dragging Then                                                     
            'end the dragging                                                
            Dragging = False                                                 
            'Me.Capture = False                                              
            Cursor.Clip = Nothing                                            
            Button1.Invalidate()                                             
        End If                                                               
End Sub                                                                      

Private Sub Button1_MouseDown(ByVal sender As System.Object, ByVal e As      System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown               
        If e.Button = MouseButtons.Left Then                                 
            Dragging = True                                                  
            mousex = -e.X                                                    
            mousey = -e.Y                                                    
            Dim clipleft As Integer = Me.PointToClient(MousePosition).X -    Button1.Location.X                                                           
            Dim cliptop As Integer = Me.PointToClient(MousePosition).Y -     Button1.Location.Y                                                           
            Dim clipwidth As Integer = Me.ClientSize.Width - (Button1.Width  
- clipleft)                                                                  
            Dim clipheight As Integer = Me.ClientSize.Height - (Button1.Height
 - cliptop)                                                                   
            Cursor.Clip = Me.RectangleToScreen(New Rectangle(clipleft, cliptop, clipwidth, clipheight))                                                         
            Button1.Invalidate()                                                
        End If                                                                  
End Sub                                                                         

Private Sub Button1_MouseMove(ByVal sender As System.Object, ByVal e As         System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove                  
         If Dragging Then                                                       
            'move control to new position                                       
            Dim MPosition As New Point()                                        
            MPosition = Me.PointToClient(MousePosition)                         
            MPosition.Offset(mousex, mousey)                                    
            'ensure control cannot leave container                              
            Button1.Location = MPosition                                        
        End If                                                                  
End Sub