0

I am using a qt5 TextEdit in Gambas3 for rich text.

Please consider the code:

    Dim cursorpos As Integer

  If Key.Code = Key.Left Or Key.Code = Key.Up Or Key.code = Key.Right Or Key.Code = Key.Down Or Key.Code = Key.Delete Or Key.Code = Key.Backspace Then
    
    cursorpos = TextEdit1.Pos ' just pick the position
    Print cursorpos
    
  Else 
    cursorpos = TextEdit1.Pos
    Print cursorpos
    TextEdit1.RichText = "<font color = \"#224444\">" & Replace(TextEdit1.Text, gb.NewLine, "<br>") & "</font>" ' this preserves the newlines, and replaces them with a <br> for the rich text
    Print "setting : ", cursorpos ' prints the correct value
    TextEdit1.Pos = cursorpos ' does not work
    Print "got : ", TextEdit1.Pos ' jumps to the end of the string
  Endif

Now, I write :

This si a line
this is a second line

I have a typo on the first line. I use my arrow key to get there. I hit backspace twice, and remove the word si. All good. Now I expect to type in the character i, and the cursor should stay just after the character i. But as soon as the i is typed in the correct position, the cursor jumps to the end of the text.

Please help. How can I keep the cursor position in the correct place? Thank you.

Sean
  • 789
  • 6
  • 26

1 Answers1

0

your error is this line...

TextEdit1.RichText = "<font color = \"#224444\">" & Replace(TextEdit1.Text, gb.NewLine, "<br>") & "</font>" 
' this preserves the newlines, and replaces them with a <br> for the rich text

actually it removes the hidden original RichText formatting

The best way to change color of the text inline without changing the internal RichText formatting is to do something like this...

TextEdit1.Format.Color = Color.Red

then text typed at that position will be in red.

So you could monitor for Delete / Backspace then set the format color to red at the current position.

gambaswiki.org/wiki/comp/gb.qt4.ext/textedit