WebBrowser - Search using uri search engine and keyword input?
How do I use the WebBrowser control in WPF to navigate with a search engine url and enter key?
For example, if I have the following function
private void Search( Uri uri, string keyword )
{
}
How can I reconcile the Uri and keyword like Uri = www.google.com and Keyword = WPF. I want a WPF search result in a window?
source to share
Righto.
What you need to do is get the "search bar" from the main providers you want to use, for example google , this will be:
string.Format("http://www.google.com/search?q={0}", "GoogleMe");
And for Bing this will 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,
}
source to share