Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
The DocumentStream and DocumentText properties on the WebBrowser control return the HTML of the current document as it existed when it was first displayed. However, if you modify the page using method and property calls such as AppendChild and InnerHtml, these changes will not appear when you call DocumentStream and DocumentText. To obtain the most up-to-date HTML source for the DOM, you must call the OuterHtml property on the HTML element.
The following procedure shows how to retrieve the dynamic source and display it in a separate shortcut menu.
Create a new Windows Forms application. Start with a single Form, and call it Form1
.
Host the WebBrowser control in your Windows Forms application, and name it WebBrowser1
. For more information, see How to: Add Web Browser Capabilities to a Windows Forms Application.
Create a second Form in your application called CodeForm
.
Add a RichTextBox control to CodeForm
and set its Dock property to Fill
.
Create a public property on CodeForm
called Code
.
public string Code
{
get
{
if (richTextBox1.Text != null)
{
return (richTextBox1.Text);
}
else
{
return ("");
}
}
set
{
richTextBox1.Text = value;
}
}
Public Property Code() As String
Get
If (RichTextBox1.Text IsNot Nothing) Then
Code = RichTextBox1.Text
Else
Code = ""
End If
End Get
Set(ByVal value As String)
RichTextBox1.Text = value
End Set
End Property
Add a Button control named Button1
to your Form, and monitor for the Click event. For details on monitoring events, see Events.
Add the following code to the Click event handler.
private void button1_Click(object sender, EventArgs e)
{
HtmlElement elem;
if (webBrowser1.Document != null)
{
CodeForm cf = new CodeForm();
HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("HTML");
if (elems.Count == 1)
{
elem = elems[0];
cf.Code = elem.OuterHtml;
cf.Show();
}
}
}
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim elem As HtmlElement
If (WebBrowser1.Document IsNot Nothing) Then
Dim cf As New CodeForm()
Dim elems As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("HTML")
If (elems.Count = 1) Then
elem = elems(0)
cf.Code = elem.OuterHtml
cf.Show()
End If
End If
End Sub
Always test the value of Document before attempting to retrieve it. If the current page is not finished loading, Document or one or more of its child objects may not be initialized.
.NET Desktop feedback feedback
.NET Desktop feedback is an open source project. Select a link to provide feedback:
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Build your first HTML webpage - Training
Illustrates basic programming techniques using Visual Studio Code to write HTML and CSS.