article

Dragging and Dropping Text in another location in the same richtextbox

Email
Submitted on: 1/3/2015 1:05:00 PM
By: Nicholas Afxentiou (from psc cd)  
Level: Advanced
User Rating: By 5 Users
Compatibility: VB.NET
Views: 1337
 
     For those who want to create their own text editor and want to move text around with drag and drop just like MS Word does.


 
				'Add a richtextbox control in ur form

Dim sel1, sel2 as Integer

Sub Richtextbox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Richtextbox1.MouseDown
If Richtextbox1.SelectedText <> "" And e.Clicks <> 2 Then
sel1 = Richtextbox1.SelectionStart
sel2 = Richtextbox1.SelectionLength
Richtextbox1.DoDragDrop(Richtextbox1.SelectedRtf, DragDropEffects.Move)
End If
End Sub


Private Sub Richtextbox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Richtextbox1.DragEnter
e.Effect = DragDropEffects.Move
End Sub

Private Sub Richtextbox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Richtextbox1.DragDrop
If Richtextbox1.SelectionStart < sel1 Then
Dim selStart As Int16 = Richtextbox1.SelectionStart
Richtextbox1.SelectedRtf = e.Data.GetData(DataFormats.Text).ToString()
Richtextbox1.SelectionStart = sel1 + sel2
Richtextbox1.SelectionLength = sel2
Richtextbox1.SelectedText = ""
Richtextbox1.SelectionStart = selStart
End If
If Richtextbox1.SelectionStart > sel1 + sel2 Then
Richtextbox1.SelectedRtf = e.Data.GetData(DataFormats.Text).ToString()
Richtextbox1.SelectionStart = sel1
Richtextbox1.SelectionLength = sel2
Richtextbox1.SelectedText = ""
End If
End Sub



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
RichTextBox1.AllowDrop = True
End Sub


Other 7 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.