"Help please" - add text at mousedown in TextArea

Hello,

I have a TextArea, TextArea1, which has unstyled text already.
I Have a BevelButton that I can select a string of my choice.
I want that string to be inserted at a point where I will now click with the mouse.

I can do that with the use of the clipboard but I don’t want to use the clipboard.

I want something like this…

When I click in the already existing text, my app calculates the position at that point then add the selected text there.

How can I achieve that?

Thanks.

Lennox

Look at TextEdit.SelStart and TextEdit.SelLength in the docs. They tell you what is currently hightlighted in a TextField or TextArea control.

Thanks Eli, but I will have to select the text, I do not want to select text, I want it at mouseDown.
Any other suggestion?
Thanks.
Lennox

SelStart is a good way to check where the cursor is after the user clicked the TextArea…

Try adding the following code to your TextArea1.SelChange event:

Sub SelChange()
  if System.MouseDown then
    Me.Text = Left(Me.Text, Me.SelStart) + "SomeText" + Right(Me.Text, Len(Me.Text) - Me.SelStart)
  end if
End Sub

Even though it doesn’t run in the MouseDown event, when you click anywhere on TextArea, it inserts “SomeText” at the clicked location.

Might not be exactly what you are looking for, but perhaps it could be used as a workaround?

In the MouseDown event of your TextArea1 do the following:

Sub MouseDown(x As Integer, y As Integer) As Boolean Dim pos As Integer = Me.InsertionPosAtXY(x, y) Me.Text = Me.Text.Mid(1, pos) + "text to add" + Me.Text.Mid(pos) ... End Sub

Nice, a much more elegant way to do it…

Thanks Eli and Alwyn,

Sub MouseDown(x As Integer, y As Integer) As Boolean
Dim pos As Integer = Me.InsertionPosAtXY(x, y)
Me.Text = Me.Text.Mid(1, pos) + “text to add” + Me.Text.Mid(pos)

End Sub

Works great

Lennox

If I get your problem right you simply want to insert text into another string in a text area? But that is a piece of cake you’ll see! You don’t have to calculate anything thanks to general Windows textbox control properties which also apply here. Look at this example:
textfield1.text=“this is a simple text”

Here you want to insert "very " right before “simple”, and let this insertion text be part of the method behind your bevel button. Now all you need is to click into the text right before “simple”. Three properties dealing with selection will be set at this moment:
textfield1.SelStart = 10
textfield1.SelLength=0
textfield1.SelText=’’ (empty string)
You can overwrite all three properties none of them is readonly. So set SelText to "very " and schwupps! Xojo does all the work for you the text is correctly inserted without you having to calculate anything. How do you like that?