2017-10-05 38 views
-1

我已經建立了一個簡單的應用程序鏈接到圖片搜索的有關列表框的瀏覽器窗口右側的內容的URL如下:與WInodws.Forms GUI C#Perfom圖片搜索

https://1drv.ms/i/s!Ar02lrNB2lmbm0g7RF3RY6-T6NHz

我已經完成了這幾個if語句引用我的列表框的索引,我所希望的是對這個問題更有說服力的解決方案。截至目前,每個新的搜索詞必須是硬編碼的,並且指的是相同的圖像搜索,是否偶然可以讀取列表框中的文本並執行圖像搜索而不是我擁有的解決方案?我的意思是它的作品,但必須有更好的方法。

public partial class frmCatSearch : Form 
    { 
     public frmCatSearch() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      // Event Handler for the button 
      // Pop up a message box with the current time/date 
      MessageBox.Show(DateTime.Now.ToString()); 
     } 
     /// <summary> 
     /// Navigates to a google image search depending on the selected 
     /// listbox item. 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      if (lblArtimus.SelectedIndex == 0) 
      { 
       artimusBrowser.Navigate("https://www.google.com/search"); 
      } 
      if (lblArtimus.SelectedIndex == 1) 
      { 
       artimusBrowser.Navigate("https://www.google.com/search"); 
      } 
      if (lblArtimus.SelectedIndex == 2) 
      { 
       artimusBrowser.Navigate("https://www.google.com/search"); 
      } 

      //else 
      //{ 
      // artimusBrowser.Navigate("https://images.google.com/?gws_rd=ssl"); 
      //} 
     } 

     //Code purposefully omitted 

     /// <summary> 
     /// Loads objects into listbox 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     private void frm_Load(object sender, EventArgs e) 
     { 
      //Add some cats 
      lblArtimus.Items.Add("Alley Cat"); 
      lblArtimus.Items.Add("American Bobtail"); 
      lblArtimus.Items.Add("Burmilla"); 
      lblArtimus.Items.Add("Cornish Rex"); 
      lblArtimus.Items.Add("Devon Rex"); 
      lblArtimus.Items.Add("Maine Coon"); 
      lblArtimus.Items.Add("Chesire"); 

     } 

     private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
     { 

     } 
    } 

回答

1

你只是試圖從選定的項目中獲取文本?如果是這樣,這樣做:

string searchText = this.listBox1.SelectedItem.ToString(); 

然後只是搜索searchText的值。

雖然我可能會想念你想要達到的目標。

+0

這將是答案的一部分,爲了進行圖像搜索,我需要listBox中的文本,但是如果可能,我想要做的是在每次單擊某個項目時執行新的搜索,而不是引用舊的搜索。 – shockemc