WebBrowser.Url 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定目前文件的 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。
- 屬性
例外狀況
這個 WebBrowser 執行個體已不再有效。
無法從基礎 ActiveX IWebBrowser2
控制項中擷取 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 會維護流覽會話期間所流覽之所有網頁的歷程記錄清單。 當您設定 Url 屬性時, WebBrowser 控制項會巡覽至指定的 URL,並將它新增至歷程記錄清單的結尾。
控制項 WebBrowser 會將最近流覽的網站網頁儲存在本機硬碟上的快取中。 每個頁面都可以指定到期日,指出其會保留在快取中的時間長度。 當控制項巡覽至頁面時,如果有快取的版本可用,它可節省時間,而不是再次下載頁面。 Refresh使用 方法來強制 WebBrowser 控制項藉由下載它來重載目前的頁面,確保控制項顯示最新版本。
注意
這個屬性包含目前檔的 URL,即使已要求另一份檔也一樣。 如果您設定這個屬性的值,然後再次擷取它,如果 WebBrowser 控制項沒有時間載入新檔,擷取的值可能會與設定的值不同。 您可以在事件處理常式中 DocumentCompleted 擷取新值。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應