I'm trying to get this code from the microsoft msdn site working and having problems.

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

If I type the address in and click the button, it displays the correct page. It doesn't work if I just type the address and press enter. The textbox also never updates when I click links. It stays whatever I type in. I must be doing something wrong. I created webBrowser1, a toolstrip with TextBoxAddress and ButtonGo. Any ideas?

are you catching the key press event and checking for enter?

if ((int)e.KeyChar == (int)Keys.Enter) 
{
Navigate(toolStripTextBox1.Text);
}

It's not catching the key press. What do I need to put in? I have created the from and have added this code. I'm new to C# and just trying to get use to it. What am I leaving out?

capture keypress event from your form

ok. I found the keypress event for TextBoxAddress. I clicked it and it put TextBoxAddress_KeyPress in the field next to it. Now what?

in that method run your check that i gave you from before

if ((int)e.KeyChar == (int)Keys.Enter) 
{
//not sure what box has your url, but put it here
Navigate(toolStripTextBox1.Text);
}

Typing in an address and pressing enter now works, but the TextBoxAddress is still not updating and displaying the current page.

what are you wanting the TextBoxAddress to be?

i thought you were typing in a url and hitting enter

ok, I must be really messed up then. Going by the code and from what is on , I have to create a webBrowser and call it webBrowser1, and textBox and call it TextBoxAddress, and a button called ButtonGo. The toolStrip is where I am confused. I created a toolstrip and put a textbox in it called toolStripTextBox1 and also created a new button called ButtonGo, so it replace the first ButtonGo I made.

When I type in an URL address, that would go in toolStripTextBox1 right? Then I should be able to press enter and the page would show up in webBrowser1. "http://" would be added to my address if I left it out and toolStripTextBox1 would be updated. Any link that I click on that page would change toolStripTextBox1.

Is that correct? Sorry for making this so hard. I assume that this is something easy and I'm just tryin to do the wrong thing.

I think you have an extra TextBox here, the only one you need is the textbox in the toolstrip

for the toolstrip textbox to change as you click on a url, make sure have this event in there

private void webBrowser1_Navigated(object sender,
    WebBrowserNavigatedEventArgs e)
{
    toolStripTextBox1.Text = webBrowser1.Url.ToString();
}

I deleted my TextBoxAddress. Here is my complete code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace linkchecker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

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

    }
}

Now it doesn't do anything. Apparently nothing is being called. I still have my toolStrip1, toolStripTextBox1, and ButtonGo. I'm probly leaving something real important out.

for sure your events are registered?

Nope, they weren't registered!! Thank you so much! I registered some of the events and apparently there were on the textbox that I deleted. It works great now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.