Language

WebBrowser.Url プロパティ

定義

現在のドキュメントの URL を取得または設定します。

public:
 property Uri ^ Url { Uri ^ get(); void set(Uri ^ value); };
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.WebBrowserUriTypeConverter))]
public Uri Url { get; set; }
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.WebBrowserUriTypeConverter))]
public Uri? Url { get; set; }
[<System.ComponentModel.Bindable(true)>]
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.WebBrowserUriTypeConverter))>]
member this.Url : Uri with get, set
Public Property Url As Uri

プロパティ値

Uri

現在のドキュメントの URL を表す Uri 。

属性

例外

この WebBrowser インスタンスは無効です。

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

このプロパティを設定するときに指定した値は、絶対 URI ではありません。 詳細については、IsAbsoluteUriを参照してください。

例

次のコード例では、 Url プロパティを使用して、 WebBrowser コントロールのアドレス バーを実装する方法を示します。 この例では、フォームに WebBrowser と呼ばれるwebBrowser1 コントロール、TextBox と呼ばれるTextBoxAddress コントロール、および Button というButtonGo コントロールが含まれている必要があります。 テキスト ボックスに URL を入力して Enter キーを押すか 、[移動 ] ボタンをクリックすると、 WebBrowser コントロールは指定された URL に移動します。 ハイパーリンクをクリックして移動すると、テキスト ボックスが自動的に更新され、現在の URL が表示されます。

// Navigates to the URL in the address text box when 
// the ENTER key is pressed while the text box has focus.
void TextBoxAddress_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e )
{
   if ( e->KeyCode == System::Windows::Forms::Keys::Enter &&  !this->TextBoxAddress->Text->Equals( "" ) )
   {
      this->WebBrowser1->Navigate( this->TextBoxAddress->Text );
   }
}

// Navigates to the URL in the address text box when 
// the Go button is clicked.
void ButtonGo_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
   if (  !this->TextBoxAddress->Text->Equals( "" ) )
   {
      this->WebBrowser1->Navigate( this->TextBoxAddress->Text );
   }
}

// Updates the URL in TextBoxAddress upon navigation.
void WebBrowser1_Navigated( Object^ /*sender*/, System::Windows::Forms::WebBrowserNavigatedEventArgs^ /*e*/ )
{
   this->TextBoxAddress->Text = this->WebBrowser1->Url->ToString();
}
// Navigates to the URL in the address box when 
// the ENTER key is pressed while the ToolStripTextBox has focus.
private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        Navigate(toolStripTextBox1.Text);
    }
}

// Navigates to the URL in the address box when 
// the Go button is clicked.
private void goButton_Click(object sender, EventArgs e)
{
    Navigate(toolStripTextBox1.Text);
}

// Navigates to the given URL if it is valid.
private void Navigate(String address)
{
    if (String.IsNullOrEmpty(address)) return;
    if (address.Equals("about:blank")) return;
    if (!address.StartsWith("http://") &&
        !address.StartsWith("https://"))
    {
        address = "http://" + address;
    }
    try
    {
        webBrowser1.Navigate(new Uri(address));
    }
    catch (System.UriFormatException)
    {
        return;
    }
}

// Updates the URL in TextBoxAddress upon navigation.
private void webBrowser1_Navigated(object sender,
    WebBrowserNavigatedEventArgs e)
{
    toolStripTextBox1.Text = webBrowser1.Url.ToString();
}

' Navigates to the URL in the address box when 
' the ENTER key is pressed while the ToolStripTextBox has focus.
Private Sub toolStripTextBox1_KeyDown( _
    ByVal sender As Object, ByVal e As KeyEventArgs) _
    Handles toolStripTextBox1.KeyDown

    If (e.KeyCode = Keys.Enter) Then
        Navigate(toolStripTextBox1.Text)
    End If

End Sub

' Navigates to the URL in the address box when 
' the Go button is clicked.
Private Sub goButton_Click( _
    ByVal sender As Object, ByVal e As EventArgs) _
    Handles goButton.Click

    Navigate(toolStripTextBox1.Text)

End Sub

' Navigates to the given URL if it is valid.
Private Sub Navigate(ByVal address As String)

    If String.IsNullOrEmpty(address) Then Return
    If address.Equals("about:blank") Then Return
    If Not address.StartsWith("http://") And _
        Not address.StartsWith("https://") Then
        address = "http://" & address
    End If

    Try
        webBrowser1.Navigate(New Uri(address))
    Catch ex As System.UriFormatException
        Return
    End Try

End Sub

' Updates the URL in TextBoxAddress upon navigation.
Private Sub webBrowser1_Navigated(ByVal sender As Object, _
    ByVal e As WebBrowserNavigatedEventArgs) _
    Handles webBrowser1.Navigated

    toolStripTextBox1.Text = webBrowser1.Url.ToString()

End Sub

注釈

このプロパティの設定は、 Navigate メソッドを呼び出し、指定した URL を渡すことと同じです。

WebBrowser コントロールは、閲覧セッション中にアクセスしたすべての Web ページの履歴リストを保持します。 Url プロパティを設定すると、WebBrowser コントロールは指定した URL に移動し、履歴リストの末尾に追加します。

WebBrowser コントロールは、最近アクセスしたサイトの Web ページをローカル ハード ディスク上のキャッシュに格納します。 各ページでは、キャッシュに残る期間を示す有効期限を指定できます。 コントロールがページに移動すると、キャッシュされたバージョン (使用可能な場合) を表示することで、ページを再度ダウンロードするのではなく、時間を節約できます。 Refreshメソッドを使用して、WebBrowser コントロールをダウンロードして現在のページを強制的に再読み込みし、コントロールに最新バージョンが表示されるようにします。

Note

このプロパティには、別のドキュメントが要求された場合でも、現在のドキュメントの URL が含まれます。 このプロパティの値を設定してからすぐに再度取得した場合、 WebBrowser コントロールに新しいドキュメントを読み込む時間がない場合、取得した値は設定された値と異なる場合があります。 DocumentCompleted イベント ハンドラーで新しい値を取得できます。

適用対象

こちらもご覧ください