Richtext Cursor position
Posted
#1
(In Topic #859)
Regular

Please consider the code:
Code (gambas)
- cursorpos = TextEdit1.Pos ' just pick the position
- Print cursorpos
- cursorpos = TextEdit1.Pos
- Print cursorpos
- TextEdit1.RichText = "<font color = \"#224444\">" & Replace(TextEdit1.Text, gb.NewLine, "<br>") & "</font>" ' this updates the HTML color of the newly added character, preserves the newlines, and replaces them with a <br> for the rich text
- TextEdit1.Pos = cursorpos ' does not work
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
. 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.si
Please help. How can I keep the cursor position in the correct place? Thank you.
Posted
Guru

also you must note the following…
You are doing the processing in the KeyPress event handler, this is an intercept to the controls own Keypress event that inputs characters, the TextEdit1_Keypress you enter happens "BEFORE" the code is inserted.
so all your functions happen then after that the key you pressed is inserted.
Consider the following changes, in this event handler I manually input the key text then use Stop Event to stop the TextEdit control doing it after…
Code (gambas)
- cursorpos = TextEdit1.Pos ' just pick the position
- Print cursorpos
- cursorpos = TextEdit1.Pos + 1 ' Record cursor pos and advance it as text will be interted.
- Print cursorpos
- TextEdit1.RichText = "<font color = \"#FF4444\">" & Replace(TextEdit1.Text, gb.NewLine, "<br>") & "</font>" ' this updates the HTML color of the newly added character, preserves the newlines, and replaces them with a <br> for the rich text
I don't understand the control fully but having a Pos and an Index probably is to do with how there is hidden html code so the visible position in colored text will not be the same as the actual position.
Hope that helps.
Posted
Guru

you TextEdit.RichText line starts with the colour setting so ALL text will be that color
Posted
Guru

Seems the magic property is TextEdit1.Selection.RichText
Note that TextEdit.Text has NO html formatting so any time you use
TextEdit.RichText = Replace(TextEdit.Text,"\n", "<br>") you remove all other html code.
Setting TextEdit.Selection.RichText property works like a .InsertRichText()
Consider this Keypress event…
Code (gambas)
How that works…
If delete or backspace is pressed it sets a global variable $bRemoving boolean to true
then when a Text key is pressed if the $bRemoving boolean is true it inserts the font color code and the key (using TextEdit.Selection.RichText) and sets $bRemoving to false again.
This puts the text you are typing inside the <font> declaration so all text typed there will be red.
Hope that helps.
Posted
Guru

I also set the forms Arrangement property to Arrange.Vertical and .TextEdit1.Expand removed the need for your manual arrangement in the Resize event (Like Cogier said you should experiment with form layouts Panel.Arrangement properties/.Expand/.Margin/.Spacing)
I also added a color button that will change the color of the selected text to show you how to use some features like how to convert a gambas integer color value into R,G,B hex strings for html.
Code (gambas)
- ' setting Selection will overwrite current selection so no need to clear it.
- TextEdit1.Selection.RichText = Subst("<font color=#&1&2&3>&4</font>", R, G, B, TextEdit1.Selection.Text)
PS.
I'm pretty sure i was wrong about .Pos and .Index relating to the Text/RichText stuff.
If you use the following code somewhere…
You will see that Text and RichText properties are very different and the Pos .Index etc don't have much to do with the RichText.
PPS. I found setting .Background property worked just fine. (the app sets it to white on Form_Open() )
Hopefully this will help you
Posted
Regular

This si absolutely fantastic. Thank you
1 guest and 0 members have just viewed this.


