WebBrowser.Url 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定目前文件的網址。
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
屬性值
A Uri 代表目前文件的網址。
- 屬性
例外狀況
這個 WebBrowser 例子已經不再有效。
無法從底層的 ActiveX WebBrowser 控制項中取得介面實作IWebBrowser2的參考。
設定此屬性時指定的值並非絕對 URI。 如需詳細資訊,請參閱IsAbsoluteUri。
範例
以下程式碼範例示範如何利用該 Url 屬性實作控制項的位址列 WebBrowser 。 此範例要求你的形式包含WebBrowser一個名為 webBrowser1、一個TextBox稱為 TextBoxAddress的控制項,以及Button一個稱為 的控制項。ButtonGo 當你在文字框輸入網址並按下 Enter 或點擊 Go 按鈕時, WebBrowser 控制鍵會導向指定的網址。 當你點擊超連結瀏覽時,文字框會自動更新為目前的網址。
// 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 會維護瀏覽過程中所有瀏覽網頁的歷史清單。 當你設定屬性時 Url , WebBrowser 控制項會導覽到指定的網址,並將其加入歷史清單的最後。
控制項 WebBrowser 會將最近造訪網站的網頁儲存在本地硬碟的快取中。 每個頁面都可以指定一個到期日,表示它在快取中會停留多久。 當控制項導覽到頁面時,會顯示快取版本(若有快取版本),而非重複下載該頁面,從而節省時間。 使用RefreshWebBrowser下載控制項強制重新載入當前頁面的方法,確保控制項顯示最新版本。
備註
此屬性包含當前文件的網址,即使已請求其他文件。 如果你設定了這個屬性的值,然後立刻再取回,取回的值可能會和控制項還沒載入新文件時 WebBrowser 設定的值不同。 你可以在事件處理程序 DocumentCompleted 中取得新值。