WebBrowser.Document プロパティ

定義

現在 HtmlDocument コントロールに表示されている Web ページを表す WebBrowser を取得します。

public:
 property System::Windows::Forms::HtmlDocument ^ Document { System::Windows::Forms::HtmlDocument ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.HtmlDocument Document { get; }
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.HtmlDocument? Document { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Document : System.Windows.Forms.HtmlDocument
Public ReadOnly Property Document As HtmlDocument

プロパティ値

現在のページを表す HtmlDocument。ページが読み込まれていない場合は、null

属性

例外

この WebBrowser インスタンスは無効になっています。

IWebBrowser2 インターフェイスの実装への参照を、基になる ActiveX WebBrowser コントロールから取得できませんでした。

次のコード例では、 イベントのハンドラーで プロパティを Document 使用して、 Navigating Web ページ フォームが入力されているかどうかを判断する方法を示します。 入力フィールドに値が含まれていない場合、ナビゲーションは取り消されます。

この例では、フォームに というコントロールwebBrowser1WebBrowser含まれている必要があります。

private void Form1_Load(object sender, EventArgs e)
{
    webBrowser1.DocumentText =
        "<html><body>Please enter your name:<br/>" +
        "<input type='text' name='userName'/><br/>" +
        "<a href='http://www.microsoft.com'>continue</a>" +
        "</body></html>";
    webBrowser1.Navigating += 
        new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
}

private void webBrowser1_Navigating(object sender, 
    WebBrowserNavigatingEventArgs e)
{
    System.Windows.Forms.HtmlDocument document =
        this.webBrowser1.Document;

    if (document != null && document.All["userName"] != null && 
        String.IsNullOrEmpty(
        document.All["userName"].GetAttribute("value")))
    {
        e.Cancel = true;
        System.Windows.Forms.MessageBox.Show(
            "You must enter your name before you can navigate to " +
            e.Url.ToString());
    }
}
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
    Handles Me.Load

    webBrowser1.DocumentText = _
        "<html><body>Please enter your name:<br/>" & _
        "<input type='text' name='userName'/><br/>" & _
        "<a href='http://www.microsoft.com'>continue</a>" & _
        "</body></html>"

End Sub

Private Sub webBrowser1_Navigating( _
    ByVal sender As Object, ByVal e As WebBrowserNavigatingEventArgs) _
    Handles webBrowser1.Navigating

    Dim document As System.Windows.Forms.HtmlDocument = _
        webBrowser1.Document
    If document IsNot Nothing And _
        document.All("userName") IsNot Nothing And _
        String.IsNullOrEmpty( _
        document.All("userName").GetAttribute("value")) Then

        e.Cancel = True
        MsgBox("You must enter your name before you can navigate to " & _
            e.Url.ToString())
    End If

End Sub

注釈

このプロパティは、HTML ドキュメント オブジェクト モデル (DOM) を介してコントロールに WebBrowser 表示される Web ページの内容にアクセスする場合に使用します。 これは、たとえば、Windows フォーム アプリケーションで Web ベースのコントロールを使用する場合に便利です。

このプロパティを プロパティと ObjectForScripting 組み合わせて使用すると、コントロールに表示される Web ページとアプリケーション間の双方向通信を WebBrowser 実装できます。 メソッドを HtmlDocument.InvokeScript 使用して、Web ページに実装されているスクリプト メソッドをクライアント アプリケーション コードから呼び出します。 スクリプト コードは、 オブジェクトを介して window.external アプリケーションにアクセスできます。これは、ホスト アクセス用に提供される組み込みの DOM オブジェクトであり、プロパティに指定 ObjectForScripting したオブジェクトにマップされます。

Web ページの内容に文字列としてアクセスするには、 プロパティを DocumentText 使用します。 として StreamWeb ページの内容にアクセスするには、 プロパティを DocumentStream 使用します。

適用対象

こちらもご覧ください