Visual Studio Developer Center > Visual C# Forums > Visual C# General > How do I get selected text in web browser control

Answered How do I get selected text in web browser control

  • Thursday, August 20, 2009 4:15 PM
     
     

    I have a winform with a web browser control. The default context menu has several options that I don't want such as "View Source", "Add to favorite", etc... All I need are the copy and print function.

    My questions are:
    1. WebBrowser control has 2 context menus
        a. Context menu with "View Source", etc...
        b. Context menu when a text string is selected
        I'd like to disable the first context menu (a) and keep the second one (b) so that I can utilize the copy and print function. By setting "IsWebBrowserContextMenuEnabled = false", all 2 menus are disabled

    2. If we cannot disable one of the 2 menus, I'll create a new context menu with the 2 functions as mentioned above. How do I get the selected text in web browser control?

Answers

  • Friday, August 21, 2009 10:07 AM
    Moderator
     
     Answered

    Hello CodesCrawler,

     

    Customize Contextmenu is easy, we just need to listen to Document.ContextMenuShowing event. Details steps are,
    1.We need to add a contextmenustrip onto the form designer named contextMenuStrip1.
    2.Select the webbrowser control and in the properties window, we set the ContextMenuStrip property to contextMenuStrip1
    3.In the codes, we write the following like this,
    -----------------------------------------------------------------------------------------------------
            private void Form1_Load(object sender, EventArgs e)
            {
                this.webBrowser1.Navigate("www.bing.com");
                this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
            }

            void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                this.webBrowser1.Document.ContextMenuShowing += new HtmlElementEventHandler(Document_ContextMenuShowing);
               
            }

            void Document_ContextMenuShowing(object sender, HtmlElementEventArgs e)
            {
                this.webBrowser1.ContextMenuStrip.Show(Cursor.Position);
                e.ReturnValue = false;
            }

    -----------------------------------------------------------------------------------------------

    2. Get the selected text is a little complicated. We have to use mshtml component.
    We can create a item on the ContextMenuStrip and double click it to generate the click event handler,

    -----------------------------------------------------------------------------
            private void getSelectedTextToolStripMenuItem_Click(object sender, EventArgs e)
            {
                IHTMLDocument2 htmlDocument = webBrowser1.Document.DomDocument as IHTMLDocument2;
                IHTMLSelectionObject currentSelection = htmlDocument.selection;
                if (currentSelection != null)
                {
                    IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
                    if (range != null)
                    {
                        MessageBox.Show(range.text);
                    }
                }
            }
    ------------------------------------------------------------------------------

     

    Ji Zhou

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked As Answer by CodesCrawler Friday, August 21, 2009 4:12 PM
    •  

All Replies

  • Friday, August 21, 2009 10:07 AM
    Moderator
     
     Answered

    Hello CodesCrawler,

     

    Customize Contextmenu is easy, we just need to listen to Document.ContextMenuShowing event. Details steps are,
    1.We need to add a contextmenustrip onto the form designer named contextMenuStrip1.
    2.Select the webbrowser control and in the properties window, we set the ContextMenuStrip property to contextMenuStrip1
    3.In the codes, we write the following like this,
    -----------------------------------------------------------------------------------------------------
            private void Form1_Load(object sender, EventArgs e)
            {
                this.webBrowser1.Navigate("www.bing.com");
                this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
            }

            void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                this.webBrowser1.Document.ContextMenuShowing += new HtmlElementEventHandler(Document_ContextMenuShowing);
               
            }

            void Document_ContextMenuShowing(object sender, HtmlElementEventArgs e)
            {
                this.webBrowser1.ContextMenuStrip.Show(Cursor.Position);
                e.ReturnValue = false;
            }

    -----------------------------------------------------------------------------------------------

    2. Get the selected text is a little complicated. We have to use mshtml component.
    We can create a item on the ContextMenuStrip and double click it to generate the click event handler,

    -----------------------------------------------------------------------------
            private void getSelectedTextToolStripMenuItem_Click(object sender, EventArgs e)
            {
                IHTMLDocument2 htmlDocument = webBrowser1.Document.DomDocument as IHTMLDocument2;
                IHTMLSelectionObject currentSelection = htmlDocument.selection;
                if (currentSelection != null)
                {
                    IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
                    if (range != null)
                    {
                        MessageBox.Show(range.text);
                    }
                }
            }
    ------------------------------------------------------------------------------

     

    Ji Zhou

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked As Answer by CodesCrawler Friday, August 21, 2009 4:12 PM
    •  
  • Friday, August 21, 2009 4:11 PM
     
     

    Thanks Ji.

    Perfect.