如何在Windows表单中的WebBrowser控件中显示地址栏?
我可能错了,但我不认为WebBrowserControl包括地址栏、工具栏等。我认为你需要创建自己的地址栏。你可以使用 Navigated
或 Navigating
事件来确定 URL 正在更改并更新文本框。
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text))
{
webBrowser1.Navigate(textBox1.Text);
}
}
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
if (textBox1.Text != e.Url.ToString())
{
textBox1.Text = e.Url.ToString();
}
}
编辑:我的表单有一个名为textBox1的TextBox
,一个名为button1的Button
和一个名为webBrowser1的WebBrowserControl
Dim strURL As String
strURL = ""
If Me.TextBox1.Text.Length = 0 Then
Me.TextBox1.Focus()
Me.TextBox1.BackColor = Color.Red
Else
If InStr(Me.TextBox1.Text, "http://") = 0 Then
strURL = "http://" & Me.TextBox1.Text.ToString()
Else
strURL = Me.TextBox1.Text.ToString()
End If
Me.WebBrowser1.Navigate(New System.Uri(strURL))
Me.TextBox1.Text = Me.WebBrowser1.Url.ToString()
End If
这是C#:
string strURL = null;
strURL = "";
if (this.TextBox1.Text.Length == 0) {
this.TextBox1.Focus();
this.TextBox1.BackColor = Color.Red;
}
else {
if (Strings.InStr(this.TextBox1.Text, "http://") == 0) {
strURL = "http://" + this.TextBox1.Text.ToString();
}
else {
strURL = this.TextBox1.Text.ToString();
}
this.WebBrowser1.Navigate(new System.Uri(strURL));
this.TextBox1.Text = this.WebBrowser1.Url.ToString();
}