0

我有这个代码我正在使用 listBox:

private void button6_Click(object sender, EventArgs e)
        {

            using (var w = new StreamWriter(keywords))
            {
                crawlLocaly1 = new CrawlLocaly();
                crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
                DialogResult dr = crawlLocaly1.ShowDialog(this);
                if (dr == DialogResult.OK)
                {
                    int line = listBox1.Items.Count;
                    if (LocalyKeyWords.ContainsKey(mainUrl))
                    {
                        line = listBox1.Items.IndexOf("Url: " + mainUrl + " --- " + "Localy KeyWord: " + LocalyKeyWords[mainUrl][0]);
                        listBox1.Items.Remove("Url: " + mainUrl + " --- " + "Localy KeyWord: " + LocalyKeyWords[mainUrl][0]);
                        LocalyKeyWords[mainUrl].Clear();
                        LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
                    }
                    else
                    {
                        LocalyKeyWords[mainUrl] = new List<string>();
                        LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
                    }
                    Write(w);
                    listBox1.Items.Insert(line, "Url: " + mainUrl + " --- " + "Localy KeyWord: " + LocalyKeyWords[mainUrl][0]);
                }
                if (dr == DialogResult.Cancel)
                {
                    Write(w);
                }
            } 
        }


        private void LoadKeys(Dictionary<string,List<string>> dictionary, string FileName)
        {
           string line = System.String.Empty;
           using (StreamReader sr = new StreamReader(keywords))
           {
            while ((line = sr.ReadLine()) != null)
            {
                string[] tokens = line.Split(',');
                dictionary.Add(tokens[0], tokens.Skip(1).ToList());
                listBox1.Items.Add("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]);
            }
           } 
        }

        private void Write(StreamWriter writer)
        {
            foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)
            {
                writer.WriteLine(kvp.Key + "," + string.Join(",", kvp.Value));
            }
        }

然后我想使用richTextBox而不是listBox,所以我尝试了这段代码,但它不像listBox那样工作。它只向我显示一个链接,而不是像 listBox 那样。如何使用richTextBox 修复它?

私人无效按钮6_Click(对象发送者,EventArgs e){

        using (var w = new StreamWriter(keywords))
        {
            crawlLocaly1 = new CrawlLocaly();
            crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
            DialogResult dr = crawlLocaly1.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                int line = richTextBox2.GetLineFromCharIndex(richTextBox2.TextLength) + 1;
                if (LocalyKeyWords.ContainsKey(mainUrl))
                {
                    line = richTextBox2.Text.IndexOf("Url: " + mainUrl + " --- " + "Localy KeyWord: " + LocalyKeyWords[mainUrl][0]);
                    richTextBox2.Text.Remove(line); //"Url: " + mainUrl + " --- " + "Localy KeyWord: " + LocalyKeyWords[mainUrl][0]);
                    LocalyKeyWords[mainUrl].Clear();
                    LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
                }
                else
                {
                    LocalyKeyWords[mainUrl] = new List<string>();
                    LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
                }
                Write(w);
                richTextBox2.Text.Insert(line, "Url: " + mainUrl + " --- " + "Localy KeyWord: " + LocalyKeyWords[mainUrl][0]);
            }
            if (dr == DialogResult.Cancel)
            {
                Write(w);
            }
        } 
    }


    private void LoadKeys(Dictionary<string,List<string>> dictionary, string FileName)
    {
       string line = System.String.Empty;
       using (StreamReader sr = new StreamReader(keywords))
       {
        while ((line = sr.ReadLine()) != null)
        {
            string[] tokens = line.Split(',');
            dictionary.Add(tokens[0], tokens.Skip(1).ToList());
            richTextBox2.Text = "Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1];
        }
       } 
    }

    private void Write(StreamWriter writer)
    {
        foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)
        {
            writer.WriteLine(kvp.Key + "," + string.Join(",", kvp.Value));
        }
    }
4

1 回答 1

0

解决方案是:

richTextBox2.AppendText("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]+Environment.NewLine);

谢谢。

于 2012-10-12T07:10:35.117 回答