如何在WebBrowser控件中显示地址栏

6
如何在Windows表单中的WebBrowser控件中显示地址栏?
3个回答

8

我可能错了,但我不认为WebBrowserControl包括地址栏、工具栏等。我认为你需要创建自己的地址栏。你可以使用 NavigatedNavigating 事件来确定 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


太棒了,很高兴能帮到你。如果你的问题得到解决,请不要忘记选择一个答案。 - Cory Charlton
我也在寻找一种方法来添加状态栏并在WebBrowser控件中显示它,你有什么想法吗? - Karthick

0
将一个文本框拖放到你的表单中。 使用URL.ToString方法将文本框的文本值设置为该URL字符串:
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(); 
    } 

1
我知道它是可翻译的,但是OP正在要求使用C#。 - Cory Charlton
谢谢JonH,看起来非常有帮助。 - Karthick

0
你可以创建一个文本框,然后用站点属性填充它。

谢谢,请给我更多的想法,也许附带一些示例代码,告诉我如何着手处理。 - Karthick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接