views:

408

answers:

2

I'm using a webbrowser control. How can I move the insert position for an execCommand to the end of the word, that is currently selected?

Example:

| <- current carret position

Som|eword -> move -> Someword| -> execCommand executes after current word

What I want to do is insert a line without braking the word. What happens now is:

Somew|ord -> line

Somew


ord

What should happen is:

Somew|ord -> line


Someword

Thank you.

A: 

Hi BFree,

Your solution does not work. The Command is still executed where the cursor was from the beginning.

I've added your comment as a comment. Please delete this 'answer'.
Jonathan Leffler
A: 

This is so hacky that I'm almost embarassed to post it, but ... you can accomplish "Inserting a line without breaking the word" "using a webbrowser control" by doing something like

webBrowser1.Url = 
            new Uri("javascript:" +
                "var tr=document.selection.createRange();" +
                "tr.expand('word');" +
                "tr.collapse(false);" +
                // "tr.select();" // Necessary to actually move the caret
                "tr.pasteHTML('<hr>');");

after the webbrowser has loaded the document that you want to manipulate, and the user has selected the text that they'd like to insert a line after. If you really need the caret moved as well, you'd need a tr.select() after the tr.collapse().

It doesn't use the execCommand though, so it may not be suitable for your purposes. Maybe someone else can figure out a way to make this a little cleaner...

Daniel LeCheminant