none
Accessing/Setting WebBrowser Control value at run-time RRS feed

  • Question

  • Hi,

    I'm developing a program in which I'm generating TabPages during run-time, and adding a WebBrowser control to each TabPage. I need to set the DocumentText value of the WebBrowser control, and am trying to do it using the following code:


                            foreach (Control c in tabs[tb].Controls)
                            {
                                if (c is WebBrowser)
                                {
                                    //MessageBox.Show("Tab " + tabs[tb].Name + " has a WebBrowser sub-control...");
                                    WebBrowser temp = (WebBrowser)c;
                                    temp.DocumentText = "<html>Tab <i>" + tabs[tb].Name + "</i> contains a WebBrowser control...</html>";
                                }
                            }
    

    This code does not seem to work as-is, however if I uncomment the the MessageBox.Show() line, I see the text I'm expecting to...

    Can anyone help me explain why, and let me know if I'm doing something fundamentally wrong...?

    Thanks,


    Monday, March 26, 2018 9:49 PM

Answers

  • Sorry if the question was not placed in the correct forum - it was not obvious exactly what the issue entailed, so I asked it in a more general forum.

    Also, it turns out the problem was due to the form not being updated properly - using a delegate and the Invoke() method on the control solved the properly.

    Finally, the DocumentText property of the WebBrowser control accepts html-formatted data, so inserting the <i></i> tags results in properly formatted italicized text (I'm not certain exactly what you were referring to, here...)

    Thank you for your feedback.

    Regards,

    Steve

    • Marked as answer by StevenD_1 Wednesday, March 28, 2018 1:49 PM
    Wednesday, March 28, 2018 1:48 PM

All replies

  • Hello StevenD_1,

    Try the below sample. I assume something wrong with tabPages-generating process.

       TabControl tabControl = new TabControl();
                tabControl.TabPages.Add("tab1", "tabPage1");
                tabControl.TabPages.Add("tab2", "tabPage2");
                this.Controls.Add(tabControl);
    
                WebBrowser webBrowser = new WebBrowser();
                tabControl.TabPages["tab1"].Controls.Add(webBrowser);
    
                foreach (Control c in tabControl.TabPages["tab1"].Controls)
                {
                    if (c is WebBrowser)
                    {
                        //MessageBox.Show("Tab " + tabs[tb].Name + " has a WebBrowser sub-control...");
                        WebBrowser temp = (WebBrowser)c;
                        temp.DocumentText = "<html>Tab <i>" + this.Name + "</i> contains a WebBrowser control...</html>";
                    }
                }

    The result

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 27, 2018 2:12 AM
    Moderator
  • Neil,

    Thank you for taking the time to respond. I appreciate your feedback and am trying to make sure I understand your answer and how it pertains to what I am trying to do (I admit that my knowledge of C# and of object-oriented programming is far from advanced, making some of these concepts a bit nebulous...). I suspect you are right that my initial generation of the tabPages may be at fault.  I am providing a bit more information, in the hopes that it might be a bit more clear:

    On my form, I have placed a panel, on which I want to place the TabControl. The TabControl is created during run-time, along with a variable number of TabPages corresponding to the number of items I am processing. Each TabPage then has a WebBrowser window attached to it.

    The basic algorithm being followed is the following:

    // Previous defines...

    List<TabPage> tabPage = new List<TabPage>(); List<WebBrowser> webBrowser = new List<WebBrowser>(); ... //Inside the tab creation routine for ( int i = 0 ; i < NbElements2Process ; i++ ) { // Create a new TabPage tabPage.Add(new TabPage()); tabPage[i].Name = "tabPage_" + PCBList[i].ToString(); tabPage[i].Text = PCBList[i].ToString(); tabControl.TabPages.Add(tabPage[i]); // Create a new WebBrowser and add it to the TabPage webBrowser.Add(new WebBrowser()); webBrowser[i].DocumentText = "<html>Page " + i.ToString() + "</i></html>"; webBrowser[i].Dock = DockStyle.Fill; tabPage[i].Controls.Add(webBrowser[i]); } // Once finished creating all tabPages for TabControl, // Add TabControl to panel on main form. panel1.Controls.Add(tabControl);

    I am fully aware this is probably not the most elegant way to do this, but it seems to work (at least for the initial generation of the controls).

    My problem then comes when I try to modify the DocumentText property of the webBrowser controls (on which my initial questions was based).

    I'm certain there's a more efficient way of doing this, but finding the right resources has proven a little difficult. If you can provide any hints it would be appreciated - I continue to research in the meantime...

    Regards,

    Steve

    Tuesday, March 27, 2018 4:40 PM
  • First, in the future, please put questions about the WebBrowser control in the appropraite forum. This should be either in the Windows Forms forum or the WPF forum.

    Look at WebBrowserBase.Text Property. You can't use that. Also note that you cannot put HTML into the text property if you want to get the formatting of the HTML. In other words, If you put "<i>italics</i>" into the Text property then that is what the user will see in the browser. You probably want to get the WebBrowser.Document Property then the HtmlDocument.Body Property of that and then set the HtmlElement.InnerHtml Property of that. Note that you might have the problem that the WebBrowser.Document property is null. If so then that means you have not set the URL to something and you have not navigated to an URL. You can use "about:blank". Note that you can't use the document immediately after navigating; the WebBrowser executes asynchronously. You can use the DocumentComplete event if you need to do something to the document after navigating.

    I don't know if that is everything but you need to do at least that much.



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, March 27, 2018 9:08 PM
  • Sorry if the question was not placed in the correct forum - it was not obvious exactly what the issue entailed, so I asked it in a more general forum.

    Also, it turns out the problem was due to the form not being updated properly - using a delegate and the Invoke() method on the control solved the properly.

    Finally, the DocumentText property of the WebBrowser control accepts html-formatted data, so inserting the <i></i> tags results in properly formatted italicized text (I'm not certain exactly what you were referring to, here...)

    Thank you for your feedback.

    Regards,

    Steve

    • Marked as answer by StevenD_1 Wednesday, March 28, 2018 1:49 PM
    Wednesday, March 28, 2018 1:48 PM