Why when using a richTextBox the code is not working as it was with ListBox?
<p>This is the richTextBox code:</p>

<pre><code>  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 = richTextBox2.GetLineFromCharIndex(richTextBox2.TextLength) + 1;
                        if (LocalyKeyWords.ContainsKey(mainUrl))
                        {
                            line = richTextBox2.Text.IndexOf("Url: " + mainUrl + " --- " + "Localy KeyWord: " + LocalyKeyWords[mainUrl][0]);
                            richTextBox2.Text.Remove(line);
                            LocalyKeyWords[mainUrl].Clear();
                            LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
                        }
                        else
                        {
                            LocalyKeyWords[mainUrl] = new List&lt;string&gt;();
                            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&lt;string,List&lt;string&gt;&gt; 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.AppendText("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]+Environment.NewLine);
                    ColorText(richTextBox2, Color.Red,"Url: ");
                }
               } 
</code></pre>

<p>If the text file is on the hard disk and full with some text and the richTextBox2 also show the text from the text file there are no problems. The problems are when the text file on the hard disk is empty and i click the button to add a new Value to the Key. Once i click on OK im getting the error exception.</p>

<p>The problem is in a case like now when the text file on the hard disk is empty and im trying to click the button to add a value to the current key.</p>

<p>The error is on the line in the button click event:</p>

<pre><code>richTextBox2.Text.Insert(line, "Url: " + mainUrl + " --- " + "Localy KeyWord: " + LocalyKeyWords[mainUrl][0]);
</code></pre>

<p>The error is: ArguementOutOfRange Exception 
: Specified argument was out of the range of valid values. Parameter name: startIndex</p>

<p>After using a breakpoint when using a richTextBox2:</p>

<p>I was sure the problem is the: + Environment.NewLine</p>

<p>In the LoadKeys function. But that wasnt the problem. It was when using ListBox.</p>

<p>Now i tried with the richTextBox again and when im running ther program either the file is empty or with keys and values when i try to add a new/change value for a key first time nothing happens second time its throwing an exception on the line:</p>

<pre><code>richTextBox2.Text.Remove(line);
</code></pre>

<p>And i see that line is -1</p>

<p>Then when im running the program again richTextBox2 is empty since for some reason after the exception is deleting everything from the text file. And when im trying to add now a value for a key im getting exception on this line in button click event:</p>

<pre><code>richTextBox2.Text.Insert(line, "Url: " + mainUrl + " --- " + "Localy KeyWord: " + LocalyKeyWords[mainUrl][0]);
</code></pre>

<p>Specified argument was out of the range of valid values. Parameter name: startIndex</p>

<p>This time line is 1 .</p>

<p>The button click event now with the change wich is not working yet:</p>

<pre><code>private void button6_Click(object sender, EventArgs e)
        {
            tryingRichTextBox();
        }
</code></pre>

<p>And tryingRichTextBox:</p>

<pre><code>private void tryingRichTextBox()
        {
            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);
                        LocalyKeyWords[mainUrl].Clear();
                        LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
                    }
                    else
                    {
                        line = 0;
                        LocalyKeyWords[mainUrl] = new List&lt;string&gt;();
                        LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
                    }
                    Write(w);
                    richTextBox2.Text.Insert(line, "Url: " + mainUrl + " --- " + "Localy KeyWord: " + LocalyKeyWords[mainUrl][0]);
                }
                if (dr == DialogResult.Cancel)
                {
                    Write(w);
                }
            }
        }
</code></pre>
