How to show address bar in WebBrowser control

只谈情不闲聊 提交于 2021-02-05 04:44:20

问题


How to show address bar in WebBrowser control in a Windows Form?


回答1:


I could be mistaken but I don't believe the WebBrowserControl includes the address bar, toolbar, etc. I believe you'll have to create your own address bar. You could use the Navigated or Navigating events to determine when the URL is changing and update the text box.

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();
    }
}

Edit: My form has a TextBox named textBox1, a Button named button1 and a WebBrowserControl named webBrowser1




回答2:


You could make a textbox and then fill it with the site property i think




回答3:


Drag and drop a text box into your form. Use the URL.ToString method to set the textbox .text value to that url string:

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

Here's 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(); 
    } 


来源:https://stackoverflow.com/questions/1923740/how-to-show-address-bar-in-webbrowser-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!