views:

52

answers:

0
Dim lPropertyInfo As PropertyInfo
Dim lSecondLevelPropertyInfo As PropertyInfo

Dim lPropertyValue As String = String.Empty
Dim lSecondLevelPropertyValue As String = String.Empty
Dim lMainParagraph As Paragraph

Dim lSubParagraph As Paragraph

Dim lFormatAvailable 'As CustomStyle


Dim lCollection As New Collection(Of String)






     'lordered list is a list of custom objects


    For x As Integer = 0 To lOrderedList.Count - 1

        'CREATE A NEW PARAGRAPH FOR LINE FEED AFTER EACH ELEMENT

        lMainParagraph = New Paragraph
        lMainParagraph.LineHeight = mGroupSpacing


        'TRAVERSE ALL THE PROPERTIES OF THE CLASS
        For Each [Property] In lOrderedList(x).GetType.GetProperties

            lPropertyInfo = lOrderedList(x).GetType.GetProperty([Property].Name)



            If we require this field  Then


                'CHECK IF THE PROPERTY IS AN ARRAY OR A COLLECTION
                If lPropertyInfo.ToString.Contains("EntitySet") Or lPropertyInfo.PropertyType.IsArray Then



                    For y As Integer = 0 To lPropertyInfo.GetValue(lOrderedList(x), Nothing).length - 1

                        ''''''''''''''''''''''FOR CHANGING THE DISTANCE BETWEEN LINES
                        lSubParagraph = New Paragraph
                        lSubParagraph.LineHeight = mChildLineSpacing
                        ''''''''''''''''''''''END FORMATTING

                        For Each LevelTwoProperty In lPropertyInfo.GetValue(lOrderedList(x), Nothing)(y).GetType.GetProperties

                            lSecondLevelPropertyInfo = lPropertyInfo.GetValue(lOrderedList(x), Nothing)(y).GetType.GetProperty([LevelTwoProperty].Name)


                            If we require this field IsNot Nothing Then

                                lSecondLevelPropertyValue = lSecondLevelPropertyInfo.GetValue(lPropertyInfo.GetValue(lOrderedList(x), Nothing)(y), Nothing)
                                Format(lSubParagraph, [LevelTwoProperty].Name, lSecondLevelPropertyValue)
                            End If
                        Next
                    Next

                Else

                    lPropertyValue = lPropertyInfo.GetValue(lOrderedList(x), Nothing)
                    Format(lMainParagraph, [Property].Name, lPropertyValue)
                End If



            End If
        Next

    Next

I have this custom function which I have simplified to ask my question. I am basically creating a user control in which I traverse the multi-level list of my custom objects. I get the data from these lists and apply some formatting to it and add it to a rich text box control. The format function is given below

Private Sub Format(ByRef pParagraph As Paragraph, _ ByVal pFieldName As String, _ ByVal pData As String)

    Dim lFontStyle = Nothing
    Dim lDelimiter As String = String.Empty
    Dim lDocument As New XmlDocument
    Dim objtextRange As TextRange
    Dim objWordStartPoint As TextPointer
    Dim objWordEndPoint As TextPointer
    Dim strText As String
    Dim lstMatchGroup As MatchCollection
    Dim objStart As TextPointer
    Dim lCustomStyle As CustomStyle

    Try

        lCustomStyle = (From lValue In mFormat _
                 Where lValue.FieldName.ToUpper.Equals(pFieldName.ToUpper) _
                 Select lValue).SingleOrDefault


        lDelimiter = lCustomStyle.Delimiter


        Select Case lCustomStyle.Font
            Case WpfApplication2.CustomStyle.position.Bold
                lFontStyle = New Bold
                lFontStyle.Inlines.Add(pData & lDelimiter)
                pParagraph.Inlines.Add(lFontStyle)
            Case WpfApplication2.CustomStyle.position.Italic
                lFontStyle = New Italic
                lFontStyle.Inlines.Add(pData & lDelimiter)
                pParagraph.Inlines.Add(lFontStyle)
            Case WpfApplication2.CustomStyle.position.UnderLine
                lFontStyle = New Underline
                lFontStyle.Inlines.Add(pData & lDelimiter)
                pParagraph.Inlines.Add(lFontStyle)

            Case WpfApplication2.CustomStyle.position.Normal
                pParagraph.Inlines.Add(New Run(pData & lDelimiter))
        End Select


        'If lFontStyle IsNot Nothing Then
        '    lFontStyle.Inlines.Add(pData & lDelimiter)
        '    pParagraph.Inlines.Add(lFontStyle)
        'Else
        '    pParagraph.Inlines.Add(New Run(pData & lDelimiter))
        'End If

        mFlowDocument.Blocks.Add(pParagraph)
        ucRichTextbox.Document = mFlowDocument



        'COLOR PART STARTS HERE

        'objtextRange = New TextRange(ucRichTextbox.Document.ContentStart, ucRichTextbox.Document.ContentEnd)

        'strText = objtextRange.Text
        'objStart = ucRichTextbox.Document.ContentStart

        'lstMatchGroup = Regex.Matches(strText, pData)

        'objWordStartPoint = GoToPoint(objStart, lstMatchGroup.Item(lstMatchGroup.Count - 1).Index)
        'objWordEndPoint = GoToPoint(objStart, lstMatchGroup.Item(lstMatchGroup.Count - 1).Index + lstMatchGroup.Item(lstMatchGroup.Count - 1).Length + 1)
        'mCounter += 2
        'objtextRange = New TextRange(objWordStartPoint, objWordEndPoint)
        'objtextRange.ApplyPropertyValue(TextElement.ForegroundProperty, New System.Windows.Media.SolidColorBrush(lCustomStyle.Color)) 


        If lDelimiter <> "" Then
            mAdder = 1
            'mCounter += 2
        Else
            mAdder = 0
            mCounter -= 2
        End If

        objtextRange = New TextRange(ucRichTextbox.Document.ContentStart, ucRichTextbox.Document.ContentEnd)

        strText = objtextRange.Text
        objStart = ucRichTextbox.Document.ContentStart

        lstMatchGroup = Regex.Matches(strText, pData)

        objWordStartPoint = GoToPoint(objStart, lstMatchGroup.Item(lstMatchGroup.Count - 1).Index)
        objWordEndPoint = GoToPoint(objStart, lstMatchGroup.Item(lstMatchGroup.Count - 1).Index + lstMatchGroup.Item(lstMatchGroup.Count - 1).Length + mAdder)
        mCounter += 2
        objtextRange = New TextRange(objWordStartPoint, objWordEndPoint)
        objtextRange.ApplyPropertyValue(TextElement.ForegroundProperty, New System.Windows.Media.SolidColorBrush(lCustomStyle.Color))

    Catch ex As Exception

    End Try
End Sub

Everything is working how I expect it to. However when later on I try to use this line

RichTextbox.Document.blocks.clear()

I get an error Exception of type 'System.ExecutionEngineException' was thrown. Any one has any idea what this might refer to? I am on the verge of pulling out all my remaining hairs! :(