Question

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?

Was it helpful?

Solution

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,
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top