Control cursor position within a field in Base

Ratslinger has provided code to perform actions on a string in a text control and to determine the position of the cursor following these actions. The essential was as follows (where ‘temp’ was the modified string):

Rem Save position of entry
		iSelMin = oEvent.Source.getSelection().Min
		iSelMax = oEvent.Source.getSelection().Max
Rem Load modified text back out
Rem Need to set cursor at last position of data entered
		oSelection.Min = iSelMin
		oSelection.Max = iSelMax
		oEvent.Source.Selection = oSelection

I have to admit that I have not fully understood what is going on here, and I have not succeeded in using it in other contexts. (I have not found documentation on the ‘Selection’ property.) What I want to do now, having used a macro (intercepting a keystroke) to add to the text in a control, is simply to move the cursor (within the same control/field) to the end of the text. Advice please!


In your noting of “provided code” oSelection is defined as:

Dim oSelection As New

This is a structure to define a numerical range. See → Selection Struct Reference

The existing min and max values can be gotten using the controls view:

Dim oForm As Object
Dim oField As Object
Dim oDocCtl As Object
Dim oCtlView As Object
Dim iSelMin As Integer
Dim iSelMax As Integer
oDocCtl = ThisComponent.getCurrentController()
oForm = ThisComponent.Drawpage.Forms.getByName("YOUR_INTERNAL_FORM")
oField = oForm.getByName("YOUR_FIELD")
oCtlView = oDocCtl.GetControl(oField)
iSelMin = oCtlView.getSelection().Min
iSelMax = oCtlView.getSelection().Max

Setting a selection:

Dim oSelection As New
oSelection.Min = VALUE_HERE
oSelection.Max = VALUE_HERE
oCtlView.Selection = oSelection

So if you want Selected text of the first five characters in the field, Min = 0 and Max = 5. To set the cursor at the beginning, both values are set to 0. To set at the end, use length of the data to determine existing number of characters.

Thank you! I was banking on a quick and straightforward answer: this does the trick (and I understand it a little better).