Monday, October 8, 2012

Paragraph shows as child of another element even after RichTextbox clear

Paragraph shows as child of another element even after RichTextbox clear

I have two RichTextboxes on my page. I am using them as a Book interface. There are two buttons to turn forward and back.

Traversing through the pages forward works fine. But once I hit back, VS2010 shoots an exception

Element is already the child of another element. 

This is for the paragraph I am adding in the RichTextbox. This happen even though I am clearing both the RichTextboxes before flooding them with new data.

My code below. What am I missing?

TxtBlobLeft.Blocks.Clear();         TxtBlobRight.Blocks.Clear();           foreach (Paragraph item in pagesStack[count].paras)         {             TxtBlobLeft.Blocks.Add(item); //Throws exception here         }           foreach (Paragraph item in pagesStack[count + 1].paras)         {             TxtBlobRight.Blocks.Add(item);         } 

Note: pagesStack is a custom Object with Paras (i.e List) as a property.

Answers & Comments...

Answer: 1

Can you check in debugger what is the Parent of item when you enter loop?

by : user1140705http://stackoverflow.com/users/1140705

Answer: 2

Seems like Clear doesn't remove the parent reference of Paragraphs to the RTB. I created a new method for Clear using the following forach loop to remove all items.

foreach (Paragraph item in paras)         {             Page1.Blocks.Remove(item);         } 

paras is my List of Paragraphs which I had added to the RTB previously.

by : Omkarhttp://stackoverflow.com/users/621494




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog