如果我选择多个项目,如何为listBox中的每个项目创建任务?

时间:2013-03-17 14:49:23

标签: c#

首先这是listBox选择的索引改变事件:

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;
            }
        }

1 个答案:

答案 0 :(得分: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);
            }
        }