views:

671

answers:

3

How to show address bar in webbrowser control in a C# winform?

Thanks, Karthick

A: 

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

jose
Thanks, pls. give me some more idea..may be with sample code on how to approach.
Karthick
+1  A: 

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

Cory Charlton
Thanks Cory, looks perfect!!!
Karthick
Awesome, glad I could help. Don't forget to select an answer if your question is solved.
Cory Charlton
Also am looking an option on how to add status bar and show the same for webbrowser control, any thoughts on that.
Karthick
Ask a second question.
SLaks
A: 

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(); 
    }
JonH
I know it's translatable but the OP is asking for C#.
Cory Charlton
http://www.developerfusion.com/tools/convert/vb-to-csharp/
JonH
Thanks JonH, looks very helpful.
Karthick