views:

320

answers:

1

How can use the WebBrowser control in WPF to navigate using Search engine uri and input key?

For Example if I have the following function

private void Search( Uri uri, string keyword )
{
}

How can I concatnate the Uri and keyword sucha as Uri = www.google.com and Keyword = WPF. I want the search result of 'WPF' in window?

+10  A: 

Righto.

What you will need to do is get the "search string" from the main providers you want to use, for instance with google, it'd be this:

string.Format("http://www.google.com/search?q={0}", "GoogleMe");

And for Bing, this would work:

string.Format("http://www.bing.com/search?q={0}", "BingMe");

Yahoo:

string.Format("http://search.yahoo.com/search?p={0}", "YahooMe");

Following the same pattern for other search engines. Example as follows:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    Search(SearchProvider.Google, "StackOverflow");
}

private void Search(SearchProvider provider, string keyword)
{
    Uri UriToNavigate = null;

    switch (provider)
    {
        case SearchProvider.Google:
            {
                UriToNavigate = new Uri(
                    string.Format("http://www.google.com/search?q={0}", keyword));                        
                break;
            }
        case SearchProvider.Bing:
            {
                UriToNavigate = new Uri(
                    string.Format("http://www.bing.com/search?q={0}", keyword));
                break;
            }
        case SearchProvider.Yahoo:
            {
                UriToNavigate = new Uri(
                    string.Format("http://search.yahoo.com/search?p={0}", keyword));
                break;
            }
    }

    Browser.Navigate(UriToNavigate);
}


enum SearchProvider
{
  Google = 0,
  Bing = 1,
  Yahoo = 2,
}
Kyle Rozendo
Keep in mind that you'd probably need to URL encode the search string as well.
rossisdead