Question

Backstory,

So I am working on a personal assistant program and all my voice commands are translated into strings for parsing.

I have set up the ability to search Google and display the results in a text block as hyperlinks.

Now I want to be able to set up the ability to open these links with speech(string commands). So far I have the following.

This bit allows me to search using the Google Custom Search API with a custom "GoogleSearch" class.

public void search_google(string query) //Google Searching
    {
        #region link strings
        string result_1 = "";
        string result_2 = "";
        string result_3 = "";
        string result_4 = "";
        string result_5 = "";
        string result_6 = "";
        string result_7 = "";
        string result_8 = "";
        string result_9 = "";
        string result_10 = "";
        #endregion

        GoogleSearch search = new GoogleSearch()
        {
            Key = "{apikey}",
            CX = "{cxkey}"
        };
        search.SearchCompleted += (a, b) =>
        {
            tab_control.SelectedIndex = 2;
            int p = 1;
            search_results.Text = String.Empty;
            foreach (Item i in b.Response.Items)
            {

                Hyperlink hyperLink = new Hyperlink()
                {
                    NavigateUri = new Uri(i.Link)
                };
                hyperLink.Inlines.Add(i.Title);
                hyperLink.RequestNavigate += Hyperlink_RequestNavigate;
                hyperLink.Name = "result_" + p;
                //search_results.Inlines.Add(hyperLink.Name);
                search_results.Inlines.Add(Environment.NewLine);
                search_results.Inlines.Add(hyperLink);

                search_results.Inlines.Add(Environment.NewLine);
                search_results.Inlines.Add(i.Snippet);
                search_results.Inlines.Add(Environment.NewLine);
                search_results.Inlines.Add(Environment.NewLine);
                p++;

            };
        };
        search.Search(query);
    }

It outputs my results in a series of hyperlinks and text snippets into a text block that I set up on the main window. The search process is triggered by my input parser which looks for the keywords "search" or "Google".

The next step would be the input parser checking for keyword "result" to look for the hyperlink to open. Here is the unfinished code for that.

if ((Input.Contains("result") || Input.Contains("Result")) && tab_control.TabIndex == 2)
        {
            int result_number = 0;
            switch(result_number)
            {
                case 1:
                    if (Input.Contains("first") || Input.Contains("1st"))
                    {
                        // open hyperlink with name property result_1
                    }
                    break;
                case 2:
                        // additional cases added up to 10 with similar syntax for parsing.
            }
        }
Was it helpful?

Solution

You can open a hyperlink in the default browser using:

Process.Start(myHyperlink);

EDIT

Based on your comments, it seems you are having trouble accessing result_1 (etc.).

You define result_1 as a variable local to the method search_google()

public void search_google(string query) //Google Searching
{
    #region link strings
    string result_1 = "";

That means result_1 is only visible within that method.

Your if and switch statements do not appear to be part of search_google(), so they can never see result_1. If those statements are in a different method, you can work around that issue by moving result_1 to the class level (outside of search_google()).

ON a site note, rather than defining ten individual result strings, you probably want to use an array of strings or a list of strings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top