2 Replies - 368 Views - Last Post: 08 December 2017 - 09:36 AM Rate Topic: -----

#1 Y00PER   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 08-November 17

Find the next superscript and set the caret there

Posted 12 November 2017 - 05:45 PM

In a richtextbox, I need to find and select the next superscripted character past the current caret position, not to edit anything, just to highlight it for the user. Currently I'm individually checking each letter's formatting and stepping my way forward until I find the next one. That works but it's too slow.
I'd like to find the superscript quickly with a regex: "\\super" applied to rtb1.rtf, and then a binary search to find the one that comes next, but I think I'd need to convert the current caret position to rtf, apply the search and then convert the result back to plain text? Is that possible? I'm new at both working with richtextboxes and regex (and binary searches for that matter), so any hints would be appreciated.
I don't know that my current non-working code is worth posting, but in snippets it's something like this:
Private superlocator As New Regex("\\super")
Private nextsuper() As Int64

Dim matchsuper As MatchCollection = superlocator.Matches(RTB1.Rtf)
Dim nextsuper(matchsuper.Count)
For Each matched As Match In matchsuper
    i += 1
    nextsuper(i) = matched.Index
Next 

Dim position As Int64 = RTB1.Selectionstart + RTB1.SelectionLength
Dim foundsuper As Int64 = Array.BinarySearch(nextsuper, position)



Is This A Good Question/Topic? 0
  • +

Replies To: Find the next superscript and set the caret there

#2 Y00PER   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 08-November 17

Re: Find the next superscript and set the caret there

Posted 13 November 2017 - 06:16 AM

I figured out a solution, but it's a bit of a kludge.
I can momentarily insert a string or character that should never appear in the text, search for it in the other version (either rtf or plaintext) and then delete it once I know where I'm at in both systems. But if there's a better way, I'd still like to know (since my program will be doing quite a bit of searching for hidden rtf codes and associating them with visible text).
Was This Post Helpful? 0
  • +
  • -

#3 Y00PER   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 08-November 17

Re: Find the next superscript and set the caret there

Posted 08 December 2017 - 09:36 AM

No responses. But just in case anyone else needs to search for superscripts in similar circumstances (or any other rtf codes that cause grief), I came up with a better solution.
On loading an rtf into the rtb, I insert a zero width space at every superscript

RTB.Rtf = Regex.Replace(someRtffile, "\\super", "\u8203?\super")

Then searches in rtb.text for Convert.ToChar(8203) will locate any superscript.
It had the added advantage for me in that I was using superscripts as a pseudo-footnote, and putting a zero width space in between the word and the footnote allows me to do whole word searches without the footnote letter being treated as part of the word.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1