2

首先这是 listBox selected index changed 事件:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (listBox1.SelectedItem != null)
            {
                label4.Text = listBox1.SelectedItem.ToString();
                string startTag = "Url: ";
                string endTag = " ---";
                int startTagWidth = startTag.Length;
                int endTagWidth = endTag.Length;
                int index = 0;
                index = label4.Text.IndexOf(startTag, index);
                int start = index + startTagWidth;
                index = label4.Text.IndexOf(endTag, start + 1);
                string g = label4.Text.Substring(start, index - start);
                label4.Text = g;
                mainUrl = g;
            }
        }
4

1 回答 1

0

通过添加此方法解决它:

private string GetUrl(object obj)
        {
            string startTag = "Url: ";
            string endTag = " ---";
            int startTagWidth = startTag.Length;
            int endTagWidth = endTag.Length;
            int index = 0;
            index = obj.ToString().IndexOf(startTag, index);
            int start = index + startTagWidth;
            index = obj.ToString().IndexOf(endTag, start + 1);
            string g = obj.ToString().Substring(start, index - start);
            mainUrl = g;
            return mainUrl;
        }

并在 DoWork 事件中使用它,如下所示:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            object input = e.Argument;
            string f = GetUrl(input);
            this.Invoke(new MethodInvoker(delegate { label2.Text = "Website To Crawl: "; }));
            this.Invoke(new MethodInvoker(delegate { label4.Text = f; }));
            if (offlineOnline == true)
            {
                offlinecrawling(f, levelsToCrawl, e);
            }
            else
            {
                webCrawler(f, levelsToCrawl, e);
            }
        }
于 2013-03-17T23:16:59.293 回答