BASIC+Calc: How to exit edit mode after double click on cell to toggle text

Code:

Sub ActivateDoubleClickEventToActiveSheetAndChangeContentInCell
            Dim Prop(1) As New com.sun.star.beans.PropertyValue
            Prop(0).name 	= "EventType"
            Prop(0).value 	= "Script"
            Prop(1).name 	= "Script"
            Prop(1).value 	= "vnd.sun.star.script:" & "Library1.Module1.ChangeContentInCell" & "?language=Basic&location=document" 
            ThisComponent.CurrentController.ActiveSheet.Events.replaceByName("OnDoubleClick", Prop())
    End Sub

        
        
Sub	ChangeContentInCell
    Dim theSelection As Object : theSelection = ThisComponent.CurrentSelection
    If NOT theSelection.SupportsService("com.sun.star.sheet.SheetCell")  Then :  Exit Sub : End If
    With theSelection
        If .String = ""			Then : .String = "Hello"	: Exit Sub : End If
        If .String = "Hello"	Then : .String = "Bye"		: Exit Sub : End If
        If .String = "Bye"		Then : .String = "Hello"	: Exit Sub : End If
    End With
End Sub

After finish, the cell is in edit mode, the cursor is at the end of the cell content . How could I exit that edit mode ?

Is there a plausible reason for relying on that complicated construct?

You can change the content of any cell, whether selected or not, whether having the keyboard focus or not, by a simple assignment to the respective content property: .Value, .String or .Formula.
The (numeric) values and the strings are also set for any SheetCellRange by .setDataArray(newContent), and the Formulas by .setFormulaArray(newContent).
newContent only needs to have the specialized DataArray structure.


Try change Sub to Function and return True or False:

Function ChangeContentInCell(oEvent As Variant) As Boolean 
Dim sCurrentString As String 
    ChangeContentInCell = False ' Continue Edit Mode
    If NOT oEvent.SupportsService("com.sun.star.sheet.SheetCell")  Then Exit Function
    sCurrentString = oEvent.getString()
    If sCurrentString = "" Then 
    	oEvent.setString("Hello")
    ElseIf sCurrentString = "Hello" Then 
    	oEvent.setString("Bye")
    ElseIf sCurrentString = "Bye" Then 
    	oEvent.setString("Hello")
    Else
    	oEvent.setString("")	' Nor "Hello", not "Bye" - clear old content
    EndIf
    ChangeContentInCell = True ' Cancel Edit Mode
End Function

For another example on the same topic, see here

@JohnSUN: Did you actually get the rationale?

Why not, @Lupp, it’s simple - a person learns to program the events of the sheet, as a first try, simply changes the text in the cell “back and forth”.

It’s just that the feature of the event handler “continue processing by other handlers” or “process now and stop further processing” is not well documented, it is rather difficult to find this information.

Well, as a teacher (retired) I can, of course, appreciate every attempt to learn something by “example and research”. It’s also my preferred way of learning, though sometimes there are more efficient ways.
In this case, however, the chosen example looked very strange to me.