Macro instruction to move a cursor to the end of string within a text box

Hello everyone,

Version: 25.2.6.2 (X86_64) / LibreOffice Community
Build ID: 729c5bfe710f5eb71ed3bbde9e06a6065e9c6c5d
CPU threads: 16; OS: Windows 11 X86_64 (10.0 build 26200); UI render: Skia/Raster; VCL: win
Locale: en-CA (en_CA); UI: en-US
Calc: threaded

Long story short, my objective is to be able to control position of the cursor within a textbox created on a Base form. Is it possible, and if so, how?

I have a function invoked by a key released event assigned to a textbox on a form. This textbox is acting as a filter, and the function is meant to compare the typed in text to the current resultset, and if the newly typed letter renders no results left, the letter is to be erased. Problem is that when the textbox value gets reset back to previous state, the cursor sits at the very beginning of the string, not the end. I’ve spent several hours trying to control this with no luck.

It would seam that it should be the com.sun.star.text.XTextViewCoursor that would be responsible for this, but I cannot seem to understand as to how to associate it with the particular textbox control or model. Any help would be greatly appreciated.

Why not simply get the whole string and remove the last letter, then set value again and refresh form?

1 Like

Let’s try blocking the input of the lowercase letter w.
After entering w, we’ll erase this character.

Sub OnKeyReleased(Byval keyEvent as Object)

  ' If "w" then send BackSpace
  With keyEvent
    If .keyCode = com.sun.star.awt.Key.W And .modifiers = 0 Then
      .source = .source.Peer  ' current xWindow 
      .keyCode = com.sun.star.awt.Key.BACKSPACE  ' or 1283
      With .source.Toolkit
       .keyPress(keyEvent)	
       .keyRelease(keyEvent)
      End With
    End If  
  End With 
End Sub
1 Like

@sokol92

Your solution worked perfectly, thank you very much. Here is an excerpt of my code (still buggy) with your suggestion:
textbox cursor issue.odb (15.3 KB)

Could you please point me in the right direction as to where do I read about this? To be more precise:

  • What does the reassignment of the keyEvent.Source property do in this case? And how did you come to know about this?

I am more of a hobby programming enthusiast than anything else, but I love to learn more about this. The deeper I dig into this UNO API it seems to me like it would be an asset to start learning both Java and C++. It seems that a lot of the components are related to (or based on) those two programming languages. As much as I’d love to do so, time constraints will probably never allow me to follow that path.

I’ve slightly corrected your example (see attachment) – it now runs without errors.
When dynamically creating a control, be sure to specify a name for its model – don’t rely on the default value, as it depends on the locale (English-speaking writers sometimes forget this).

I’m simply reusing a previously created instance of the structure (out of habit, preserving nature :slight_smile: ). A new instance could also be created.

It’s a rare case when I remember exactly.

And I’m slowly drifting towards Python.
textbox cursor issue 2.odb (15.1 KB)

1 Like

Once again thank you for all your help and guidance. Folks like your and many others on this forum make a huge difference.

1 Like