JayBeeOH

TxtEditor - Drag and Drop.

Apr 24th, 2015
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.41 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2015. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Text Editor
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   Feb 2015
  16. '
  17. ' Description:    Simple plain text editor program with printing ability.
  18. '
  19. '                 Documentation is at:
  20. '                   App's screen image is at: http://imgur.com/TzWs9kO
  21. '                   App's screen design time specs at: http://pastebin.com/bhinRQ5d
  22. '                   App's Visual Basic .NET Drag and Drop is at
  23. '                       http://pastebin.com/yWB22inT
  24. '                   App's Visual Basic .NET code is at http://pastebin.com/u/jaybeeoh
  25. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  26. '
  27. '                 See Also, MSDN's article "Implementing Drag and Drop in Visual Basic .NET"
  28. '                   at http://msdn.microsoft.com/en-us/library/aa289508(v=vs.71).aspx .
  29. '-------------------------------------------------------------------------------------------
  30.  
  31. #Region "  Declare class level variables"
  32.  
  33.     ' NOTE: Additional code not shown.
  34.  
  35.     ' For Drag and Drop.
  36.     Private mouseIsDown As Boolean = False
  37.     Private textToMove As String = String.Empty
  38.     Private textToMoveStart As Integer
  39. #End Region
  40.  
  41. #Region " Drag and Drop"
  42.  
  43.     ' Note: Set at design time, MyTextBox.AllowDrop = True
  44.  
  45.     ' Mouse Down key has been pressed.
  46.     Private Sub MyTextBox_MouseDown(sender As Object, e As MouseEventArgs) _
  47.         Handles MyTextBox.MouseDown
  48.  
  49.         ' Internal drag and drop.
  50.         With MyTextBox
  51.             If (.SelectionLength > 0) Then
  52.                 textToMove = .SelectedText
  53.                 textToMoveStart = .SelectionStart
  54.                 mouseIsDown = True
  55.             End If
  56.         End With
  57.     End Sub
  58.  
  59.     ' Initiate internal dragging.
  60.     Private Sub MyTextBox_MouseMove(sender As Object, e As MouseEventArgs) _
  61.         Handles MyTextBox.MouseMove
  62.  
  63.         ' If dragging, set DragDropEffects.
  64.         With MyTextBox
  65.             If mouseIsDown AndAlso (.SelectionLength > 0) Then
  66.                 .DoDragDrop(.SelectedText, DragDropEffects.Copy Or DragDropEffects.Move)
  67.             End If
  68.         End With
  69.         mouseIsDown = False
  70.     End Sub
  71.  
  72.     'Scroll to insertion point position.
  73.     Private Sub MyTextBox_DragOver(sender As Object, e As DragEventArgs) _
  74.         Handles MyTextBox.DragOver
  75.  
  76.         ' Determine cursor location, then scroll to position.
  77.         With MyTextBox
  78.             .Select((.GetCharIndexFromPosition(.PointToClient(Cursor.Position))), 0)
  79.             .ScrollToCaret()
  80.             .Focus()
  81.         End With
  82.     End Sub
  83.  
  84.     ' Show DragDropEffect.
  85.     Private Sub MyTextBox_DragEnter(sender As Object, e As DragEventArgs) _
  86.         Handles MyTextBox.DragEnter
  87.  
  88.         ' Check the format of the data being dropped.
  89.         If (e.Data.GetDataPresent(DataFormats.Text)) Then
  90.             If (e.KeyState And 8) = 8 AndAlso (e.AllowedEffect And DragDropEffects.Copy) = DragDropEffects.Copy Then
  91.                 ' CTL KeyState for copy.
  92.                 e.Effect = DragDropEffects.Copy
  93.             Else
  94.                 e.Effect = DragDropEffects.Move
  95.             End If
  96.         Else
  97.             e.Effect = DragDropEffects.None
  98.         End If
  99.     End Sub
  100.  
  101.     ' Drop text at current cursor location.
  102.     Private Sub MyTextBox_DragDrop(sender As Object, e As DragEventArgs) _
  103.         Handles MyTextBox.DragDrop
  104.  
  105.         With MyTextBox
  106.             ' Determine cursor location, then insert data.
  107.             Dim index As Integer = .GetCharIndexFromPosition(.PointToClient(Cursor.Position))
  108.             .Text = .Text.Insert(index, e.Data.GetData(DataFormats.Text).ToString)
  109.  
  110.             ' If internal move operation, then delete data from previous location.
  111.             If (e.Effect = DragDropEffects.Move) AndAlso (textToMove <> String.Empty) Then
  112.                 Dim foundPositon As Integer = .Text.IndexOf(textToMove, textToMoveStart, StringComparison.OrdinalIgnoreCase)
  113.                 If foundPositon > -1 Then
  114.                     .Select(foundPositon, textToMove.Length)
  115.                     .SelectedText = String.Empty
  116.                     ' Reposition index.
  117.                     If foundPositon <= index Then
  118.                         index -= textToMove.Length
  119.                     End If
  120.                     If index < 0 Then
  121.                         index = 0
  122.                     End If
  123.                 End If
  124.             End If
  125.  
  126.             ' Highlight new or moved data.
  127.             .Select(index, e.Data.GetData(DataFormats.Text).ToString.Length)
  128.             .ScrollToCaret()
  129.             .Focus()
  130.             .Modified = True
  131.  
  132.             textToMove = String.Empty
  133.             textToMoveStart = 0
  134.         End With
  135.     End Sub
  136.  
  137. #End Region
Advertisement
Add Comment
Please, Sign In to add comment