simple editor using webBrowser control

Hi,

I am working on an editor using the htmldocument in the webbrowser control. This is my code for testing out its capability:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
htmldoc = webBrowser1.Document;
htmldoc.ExecCommand("EditMode", true, null);
}
private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
// process key pressed
switch ( e.KeyCode ) {
case Keys.I:
if ( e.Alt ) {
htmldoc.ExecCommand("Indent", false, null); } // end if
break;
case Keys.O:
if ( e.Alt ) {
htmldoc.ExecCommand("Outdent", false, null);
} // end if
break;
case Keys.J:
if ( e.Alt ) {
htmldoc.ExecCommand("JustifyRight", false, null);
} // end if
break;
case Keys.K:
if ( e.Alt ) {
htmldoc.ExecCommand("Bold", false, null);
} // end if
break;
case Keys.L:
if ( e.Alt ) {
htmldoc.ExecCommand("Italic", false, null);
} // end if
break;
} // end switch
return;
}

There is only 1 form, in which there is only 1 control, i.e. the webbrowser control. I am using .Net 2.0. I handle the keys with the previewkeydown event.

What I do not understand is that the ExecCommand works for Indent / Outdent / JustifyRight but not for Bold and Italic and also there is no exception generated. Any idea



Answer this question

simple editor using webBrowser control

  • Nishad.A.R

    This is very ugly but i found it. The PreviewKeyDown event is called twice, so you Set the bold flag and then Unset it because it is called for the seconds time.

    Here is a little quick-N-dirty solution:


    private bool _skipOnce = false;

    private void webBrowser1_PreviewKeyDown( object sender, PreviewKeyDownEventArgs e )
    {
    if( _skipOnce )
    {
    _skipOnce = false;
    return;
    }

    // process key pressed
    switch( e.KeyCode )
    {
    case Keys.Q:
    if( e.Control )
    {
    htmldoc.ExecCommand( "Bold", false, null );
    _skipOnce = true;
    }
    break;
    }
    }




  • AlarconGarcia

    I did highlight some words before performing the action. Apparently all actions without having to make a selection will work. Those that requires a selection do not work. Is there anything tricky with making selections in a webbrowser control

  • J/-\Y

    You can first execute the SelectAll command to select all text and then execute the font style command, like Bold. At last you select the UnSelect command to deselect the text.


  • shware

    Never mind. Thanks again. I can just continue playing with other features of the mshtml.

  • Frogblast.utfoo

    Just did a quick test. The SelectAll & Unselect are successful but not the Bold action. It's just quite a puzzle.....

  • Vita

    I guess it is a bug. I tested with using just the INS key. It was fired twice also. So it was not due to pressing two keys in the previous cases.

    I am sure I will have further questions for you when I work along with mshtml. There are so much functionality still unexposed to .net !!!

    Thank you again for the assistance. It's very much appreciated.


  • RaviKanthReddy

    This should work, but only for the current selection. Bold is a valid command as you can see in the Command Identifier list.


  • Eric Harmon

    It's just great !! Thank you so much for sorting out the exact reason & thank you for the code, which should be the easiest get-around.

    Though strangely named as PreviewKeyDown, there should be no logical reasons to fire twice. A bug what do you think

    It is also strange that I cannot find any mentioning of this in the internet. As this is the only key-event of the webbrowser control, it should be hit easily.


  • BayerWhite

    Yes, the SelectAll was successful in selecting all text but the Bold action just still did not work.

    Many thanks for your assistance in advance.


  • Pencilcheck

    Sorry Francis Shum, yesterday was a long day at the office. Lot of release-stress

    I was home very late, i is hard to see nobody else checked it. My excusses, but i will take a look tonight!

    Thank you for your patient!


  • fparc

    I am glad to hear it is solved now, but it is still a strange thing. I geus it is a bug, but you press two keys; a control key and a normal key. Meybe this causes the event fires twice, i didn't fully tested it in different situations.

    There are some default shortcut's, when you press ctrl+b you selected text will get Bold for example. So meybe you can set the shortcut's somewhere, didn't looked to that point.

    When you get more strange behaviour or you find a better solution, please let us know!


  • stronghold

    I repeated the case with a new project and the result was just the same.

    PJ, any findings at your side I just don't know which is better : your being able to reproduce the case or not.

  • jabdip

    When you fist select the text and then change the font style it won't work

    I don't have Visual Studio 2005 here to test, but i have tonight when i am home. But as far as i can see i don't see any problems why it shouldn't work.


  • R. Brian Lindahl

    Strange, i will take a look when i am home. Meybe someone else can take a look at this strange behaviour before i can. But if nobody can i will take a look and i will try to reproduce your strange behaviour.

    Thanks for you fast replies!


  • simple editor using webBrowser control