Tuesday, November 3, 2009

Richtextbox - Drag And Drop

So I'm still working on that "letter editor" which is going alright ... I haven't hit and real road blocks. I did however come across this little issue .... I have a treeview control on the left hand side of my project which contains a list of all the objects I want them to be able to work with. I wanted to allow them to drag and drop those over to the richtextbox and format those so my program could parse it and turn it in to a actual document.

I found some drag and drop code (its everywhere ... google it) impletmented that and things seemed to be working, the only problem was I couldn't firgure out how to insert the text where the cursor was ... the code that I found used the appendtext command which as we all know appends the text to the end of the text box. So I'm not sure if this is the best answer but what I did was add a 2nd richtextbox to my project (I hide it under the first one so its out of the way) ... then when I dropped the text in I actually added the text to the 2nd textbox used the selectall command ... then the copy command and the paste command

ie

richtextbox2.text = Directcast(e.data.getdata(gettype(string),string)
richtextbox2.selectall
richtextbox2.cut
richtextbox1.paste

this let me insert the command where the cursor was used to drop the command at...

there might be a better way ... but it works for me