0
votes

I have created a web browser app for windows phone 7 using web browser control. I want to add a default search engine(i.e, a textBox that is used for Google search or Bing search). And also if the user type anything(words like technology or so on), the search should be redirected to the above default search engine. Can anyone help me with this??? The textBox that I used for entering URL is named as "UrlTextBox" and my web browser control is named as "browsers". The textBox is used for search engine is named as "SearchTextBox". Thanks in advance for your hard work!!!

    public void browsers_Navigating(object sender, NavigatingEventArgs e)
    {
        UrlTextBox.Text = e.Uri.ToString();
        if (navigationcancelled)
        { e.Cancel = true; }

        SearchEngine[] availableSearchEngines = new SearchEngine[]
        {new SearchEngine(){ Name = "Google", URLPattern = "http://www.google.com/search?q={0}" }};
         new SearchEngine(){ Name = "Yahoo", URLPattern = "http://search.yahoo.com/search?p={0}" };
         new SearchEngine(){ Name = "Bing", URLPattern = "http://www.bing.com/search?q={0}" };
    }

UrlTextBox-:

    private void UrlTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            Uri url;
            if (Uri.TryCreate(UrlTextBox.Text, UriKind.Absolute, out url))
            {
                this.urls[this.currentIndex] = UrlTextBox.Text;
                this.browsers[this.currentIndex].Navigate(url);
            }

            if (!Uri.TryCreate(UrlTextBox.Text, UriKind.Absolute, out url))
            {
                SearchEngine defaultSearchEngine = availableSeachEngines[0];
                String URL = String.Format(defaultSearchEngine.URLPattern, UrlTextBox.Text);
            }

            else
            {
                Navigate(UrlTextBox.Text);
            }
        }
    }

But there is an error says "availableSeachEngines" ---> The name availableSeachEngines does not exist in the current context.

Now i have added my codes above that i have used in my program and also added Muaz Othman codes into it. But its not working for me and also shows the error. I think am making some mistakes in it. Can anyone correct it??? Thanks in advance!!!

1

1 Answers

3
votes

You can create a class like this:

public class SearchEngine {

    public string Name {set; get}
    public string URLPattern { get; set;}

    public override string ToString(){
        return Name;
    }
}

and in your code you can have this array:

SearchEngine[] availableSearchEngines = new SearchEngine[]{
    new SearchEngine(){ Name = "Google", URLPattern = "http://www.google.com/search?q={0}" };
    new SearchEngine(){ Name = "Yahoo", URLPattern = "http://search.yahoo.com/search?p={0}" };
    new SearchEngine(){ Name = "Bing", URLPattern = "http://www.bing.com/search?q={0}" };
}

but in your code you should have only one SearchEngine object:

SearchEngine defaultSearchEngine;

so when the user enters text and chooses "Go" you check if the entered text is a valid URL (maybe using a regular expression), and if not you do this:

String url = String.Format(defaultSearchEngine.URLPattern, SearchTextBox.Text);