TextRange
TextRange
TextRange
TextRange
TextRange
TextRange TextRange TextRange TextRange TextRange TextRange TextRange TextRange
TextRange TextRange
TextRange
Go Back  Xtreme Visual Basic Talk > > > TextRange


Reply
 
Thread Tools Display Modes
  #1  
Old 01-18-2014, 09:36 AM
JeremyBenson11 JeremyBenson11 is offline
Regular
 
Join Date: Dec 2013
Posts: 50
Default TextRange


Hey,

I'm trying to modify some code that I found to make it end a range at a certain character. Right now the code creates a text range around a list of words based on a loop. It's working pretty good. I'm trying to adapt it to color tags, and leave text between them black.

This is a couple of lines of code for the start and end of the word range
Code:
Dim startPointer As TextPointer = position.GetPositionAtOffset(indexInRun)
                    Dim endPointer As TextPointer = startPointer.GetPositionAtOffset(word.Length)
I'd like to adapt this to create ranges between > < - text between tags as in

<p>This text here...</p>

so I'd modify my search to search for >

and do something like...
Code:
Dim startPointer As TextPointer = position.GetPositionAtOffset(indexInRun + 1)

                    Dim endPointer As TextPointer = startPointer.GetPositionAtOffset(go until '<' is found)
have any idea how I could fix that last line of pseudo code?

Code:
 Dim endPointer As TextPointer = startPointer.GetPositionAtOffset(go until '<' is found)
Reply With Quote
  #2  
Old 01-18-2014, 10:17 AM
JeremyBenson11 JeremyBenson11 is offline
Regular
 
Join Date: Dec 2013
Posts: 50
Default

hmm,

I've got more code here...

Code:
Dim blackBrush As New SolidColorBrush
    blackBrush.Color = Color.FromRgb(22, 22, 22)

    Dim startCharacter As Char = ">"

    Dim betweeenTagRange As TextRange = FindBetweenTag(WordProcessor.Document.ContentStart, startCharacter)

    betweeenTagRange.ApplyPropertyValue(ForegroundProperty, blackBrush)
Code:
Public Function FindBetweenTag(ByVal position As TextPointer, ByVal startCharacter As Char) As TextRange

    While position IsNot Nothing

        If position.GetPointerContext(LogicalDirection.Forward) = TextPointerContext.Text Then

            Dim textRun As String = position.GetTextInRun(LogicalDirection.Forward)

            ''find starting index of matching word
            Dim indexInRun As Long = textRun.IndexOf(startCharacter)

            If indexInRun >= 0 Then

                Dim startPointer As TextPointer = position.GetPositionAtOffset(indexInRun + 1)
            ''Get number of characters from startPointer to first instance of '<'
                Dim endPointer As TextPointer = 
                Return New TextRange(startPointer, endPointer)

            End If

        End If

        position = position.GetNextContextPosition(LogicalDirection.Forward)

    End While

    Return Nothing


End Function

Last edited by JeremyBenson11; 01-18-2014 at 10:44 AM.
Reply With Quote
  #3  
Old 01-23-2014, 10:32 AM
JeremyBenson11 JeremyBenson11 is offline
Regular
 
Join Date: Dec 2013
Posts: 50
Default

I worked on this code and got it working a bit, but I'm kinda messing with the functions and code like a blind man in a dark room haha... I'm using my spider senses to guide me through the stuff that I understand a bit...

When I type

<p>This text is black</p>

On the first line the tags are red and the middle sentence is black like I wanted. But if I hit enter and go down to the next line and type a paragraph tag and a sentence the whole thing is red.

Also if I restart the program and type something like below.

<p><

the program crashes right there because I think it's looking for a text range in the middle, but it doesn’t exist...not sure.

Another thing I noticed is that this will only work on the first tag typed if typed outright. If I turn all the text after that black and erase it then write another tag it doesn't turn red for some reason. It only seems to work on the very first instance of a tag or text between.

can anyone help me figure out how to get all my tags red and sentences between black?

The updated code will be below.

Code:
 Public Function FindBetweenTag(ByVal position As TextPointer, ByVal startCharacter As String) As TextRange

        While position IsNot Nothing

            If position.GetPointerContext(LogicalDirection.Forward) = TextPointerContext.Text Then

                Dim textRun As String = position.GetTextInRun(LogicalDirection.Forward)

                ''find starting index of matching word
                Dim indexInRun As Long = textRun.IndexOf(startCharacter)
                Dim indexOfEndTag As Long = textRun.IndexOf("</p>")

                If indexInRun >= 0 Then

                    Dim startPointer As TextPointer = position.GetPositionAtOffset(indexInRun + startCharacter.Length + 1)

                    Dim endPointer As TextPointer = startPointer.GetPositionAtOffset(indexOfEndTag)

                    Return New TextRange(startPointer, endPointer)

                End If

            End If

            position = position.GetNextContextPosition(LogicalDirection.Forward)

        End While

        Return Nothing


    End Function
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
TextRange
TextRange
TextRange TextRange
TextRange
TextRange
TextRange TextRange TextRange TextRange TextRange TextRange TextRange
TextRange
TextRange
 
TextRange
TextRange
 
-->