WebBrowser.Document プロパティ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文
現在のページを表す HtmlDocument。ページが読み込まれていない場合は、null 参照 (Visual Basic では Nothing)。
HTML ドキュメント オブジェクト モデル (DOM: Document Object Model) によって、WebBrowser コントロールに表示されている Web ページのコンテンツにアクセスするには、このプロパティを使用します。これは、たとえば Windows フォーム アプリケーションで Web ベースのコントロールを使用する場合に役立ちます。
このプロパティを ObjectForScripting プロパティと組み合わせて使用することで、WebBrowser コントロールに表示される Web ページとアプリケーションとの双方向通信を実装できます。HtmlDocument.InvokeScript メソッドを使用して、Web ページに実装されているスクリプト メソッドをクライアント アプリケーション コードから呼び出します。スクリプト コードにより、window.external オブジェクトを介してアプリケーションにアクセスできます。このオブジェクトはホスト アクセス用に提供される組み込み DOM オブジェクトであり、ObjectForScripting プロパティで指定されたオブジェクトに割り当てられます。
Web ページのコンテンツの文字列にアクセスするには、DocumentText プロパティを使用します。Web ページのコンテンツの Stream にアクセスするには、DocumentStream プロパティを使用します。
Navigating イベントのハンドラの Document プロパティを使用して、Web ページのフォームに値が入力されたかどうかを判断する方法を次のコード例に示します。入力フィールドに値が入力されていない場合、ナビゲーションはキャンセルされます。
この例は、フォームに webBrowser1 という WebBrowser コントロールが存在することを前提にしています。
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
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()); } }
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
WebBrowser クラス
WebBrowser メンバ
System.Windows.Forms 名前空間
DocumentStream
DocumentText
HtmlDocument クラス
HtmlDocument.InvokeScript
ObjectForScripting
その他の技術情報
IWebBrowser2 インターフェイス
Weblioに収録されているすべての辞書からWebBrowser.Document プロパティを検索する場合は、下記のリンクをクリックしてください。
全ての辞書からWebBrowser.Document プロパティを検索
- WebBrowser.Document プロパティのページへのリンク