我已经构建了一个简单的应用程序,该应用程序链接到与浏览器窗口右侧列表框内容相关的图像搜索网址,如下所示:
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)
{
}
}
答案 0 :(得分:1)
您是否只想从所选项目中获取文字?如果是这样,这样做:
string searchText = this.listBox1.SelectedItem.ToString();
然后只搜索searchText的值。
我可能会错过你在这里想要达到的目标。