I have a RichTextBox on my GUI and I Have two buttons, one to display data and one to clear data. On button DISPLAY, I use this routine to display some data into the RichTextBox:
Block firstBlock = richTextBox_Console.Document.Blocks.FirstOrDefault();
richTextBox_Console.Document.Blocks.InsertBefore(firstBlock, new Paragraph(new Run("MyFirstLine= " + MyVariable + "\n" )));
string message;
for (int i = 0; i < 6; i++)
{
message = "";
message += "Line:" + (i + 1).ToString() + "\n";
richTextBox_Console.Document.Blocks.InsertBefore(firstBlock, new Paragraph(new Run(message)));
}
And on CLEAR click, I do this:
richTextBox_Console.SelectAll();
richTextBox_Console.Selection.Text = "";
Now when I click the DISPLAY button again, and on line:
richTextBox_Console.Document.Blocks.InsertBefore(firstBlock, new Paragraph(new Run("MyFirstLine= " + MyVariable + "\n" )));
firstBlock
is null
so the line throws this error:
value can not be null. Parameter name: nextSibling
Any idea what is going on? It works fine to display data the first time then never works.
I also tried doing richTextBox_Console.Document.Blocks.Clear();
to clear but I get the same error still.
You could implement the event handler for the delete button like this:
richTextBox_Console.Document.Blocks.Clear();
richTextBox_Console.Document.Blocks.Add(new Paragraph());