private void Search(object parametersObject) { string[] parameters = parametersObject as string[]; string searchText = parameters[0]; string searchLanguage = parameters[1]; string searchLocation = parameters[2]; string uriString = String.Format(@"{0}/cgi-bin/advsearch_db.cgi?perPage=100&listStart={2}&r1=V1&title1={0}&author1=&year1=&year2=&subject1=Any&language1={1}&scentre=Any&search=Search", searchLocation, searchText, searchLanguage, (pageNumberUpDown.Value - 1) * 100 ); try { HttpWebRequest request = WebRequest.Create(uriString) as HttpWebRequest; HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); string htmlString = reader.ReadToEnd(); int index = htmlString.IndexOf(@"Search matched"); if (index == -1) { this.Invoke(new ShowMessageDelegate(ShowMessage), "No results found"); return; } htmlString = htmlString.Substring(0, index); Regex hrefRegex = new Regex("<a href=\"([^\"]*)\""); MatchCollection matches = hrefRegex.Matches(htmlString); int i = ((int)pageNumberUpDown.Value - 1) * 100 + 1; EmptyDelegate beginUpdateDelegate = delegate { this.listViewSearch.BeginUpdate(); }; this.listViewSearch.Invoke(beginUpdateDelegate); foreach (Match m in matches) { string link = searchLocation + m.Groups[1].Value; DLISearchItem item = new DLISearchItem(link, i++); this.listViewSearch.Invoke(new AddLinkItemDelegate(AddLinkItem), item); } EmptyDelegate endUpdateDelegate = delegate { this.listViewSearch.EndUpdate(); }; this.listViewSearch.Invoke(endUpdateDelegate); this.listViewSearch.EndUpdate(); if (listViewSearch.Items.Count == 100) { EmptyDelegate updatePageNumber = delegate() { this.pageNumberUpDown.Value++; }; this.pageNumberUpDown.Invoke(updatePageNumber); } } catch (Exception e) { MessageBox.Show(e.Message); } finally { this.Invoke(new SetCursorDelegate(SetCursor), Cursors.Default); } }
private void AddLinkItem(DLISearchItem item) { this.listViewSearch.Items.Add(item); }