SelectedText in Textarea on double-click

I need to get the selectedText in Textarea by double-click.
I tried to use MouseDown event and check the data but it doesn’t show the selected text.

  if doubleClick = False Then
    doubleClick = True
    DoubleClickX = X
    DoubleClickY =Y
    doubleClickTimer.reset
    
  elseif IsContextualClick Then   // prevent right click from activating double click
    DoubleClick = False
    
  elseif doubleClick = True Then
    if x = DoubleClickX and y = DoubleClickY Then // check that the mouse is still in the same location
      
      MsgBox " Me.SelText : " + Me.SelText
      
    end if
  end if
  

Is there something wrong?

Perhaps you need to look at the SelChange event instead. http://documentation.xojo.com/index.php/TextArea.SelChange

You have multiple ELSEIF statements where the first one sets you DoubleClick Flag, where is this code being placed?
Unless if turns right around and calls it again, you will never get to the last level where your msgbox is.

SelChange fires when SELSTART and/or SELLENGTH change, but that doesn’t mean that is the moment you want to extract the text

[quote=202380:@changwon lee]if doubleClick = False Then
doubleClick = True
DoubleClickX = X
DoubleClickY =Y
doubleClickTimer.reset[/quote]
This click will also clear the selection. You need to save the selection here for use in double click.

I got bored… Here is a subclassed TextArea complete with DoubleClick and all the support to do what you specified :slight_smile:
Custom TextArea

What about that :

Sub SelChange() Label1.Text = me.seltext End Sub

Double click selects a word. This simply gets whatever has been selected, whatever the way : double click, triple click, selection with the mouse, shift-arrow key.

That said, you may want to do that only when Double Click actually happens. To do so :

  • Add a ClickTimer to the window, set to off with a period of 500
  • In the TextArea MouseDown :

[code]Function MouseDown(X As Integer, Y As Integer) As Boolean
static oldticks as double = Ticks
if Ticks-oldticks <=30 then
// What you would do in DoubleClick
Label1.Text = me.seltext
system.debuglog “double click”
oldticks = ticks-120 // To prevent activation by triple click
ClickTimer.Enabled = False
Return False
else
ClickTimer.Mode = Timer.ModeSingle
ClickTimer.Enabled = False
ClickTimer.Period = 520
ClickTimer.Enabled = True
end if
oldticks = Ticks

End Function
[/code]

That way you got the equivalent of Double Click event in MouseDown, and the timer will fire for a single click with a delay of 520 milliseconds.

there is no need for a timer… OSX and Windows will tell you what the delay for doublclick is based on system settings, if the time between clicks <= that time then a doubleclick occured…

The timer is required so you know that no double click occurred. But really, this is something that the framework should be handling for us. I’m sure there’s a feature request in there somewhere.

I will have to disagree, please look at the code I posted, which by the way was derived from code in the LangRef, no timers are involved, TIME is involved, but no TIMER

you measure the time BETWEEN mousedowns, not the number of mousedowns during a period of time

But what if the second mousedown never occurs? You are still left hanging waiting to process the first one.

I guess we have different assumptions here. I assume you do not want to process a single click if a double click occurs. That seems to be in line with what the OP wanted, since processing a single click changes the state that the double click expects.

show me a native doubleclick that doesn’t execute the single click event also…
all the XOJO controls that have a native doubleclick all fire the MOUSEDOWN (and MouseUp if Down returns True) as well as the doubleclick event if the time between multiple clicks is short enough

buy I’ve made my contribution, and there is no benefit from arguing semantics… what I offered works as I think it should in regards to existing controls, and if it meets the OP requirements, so be it, if not he is no worse off that he was a few hours ago.

[quote=202435:@Dave S]show me a native doubleclick that doesn’t execute the single click event also…
all the XOJO controls that have a native doubleclick all fire the MOUSEDOWN (and MouseUp if Down returns True) as well as the doubleclick event if the time between multiple clicks is short enough
buy I’ve made my contribution, and there is no benefit from arguing semantics… what I offered works as I think it should in regards to existing controls, and if it meets the OP requirements, so be it, if not he is no worse off that he was a few hours ago.[/quote]

Indeed once could argue that DoubleClick should not supersede MouseDown. Then measuring time between clicks does the job perfectly without a timer. For the same price, the user gets two clicks and a double click.That is exactly the way it works in Canvas where Doubleclick is implemented.

In fact, what I posted is in no way in contradiction with what you suggest. I simply implements access to DoubleClick, and simple Click, without hindering access to MouseDown if one chooses to employ it.

As a matter of a fact, it is not without similarities with the mandatory return in Swift versus implicit in Xojo. Click and DoubleClick assume an human operator. MouseDown is still a low level feature.

Both work well! Thank you.

Michel Bujardet:

Do I have to use SelChange event as well?
With MouseDown and SelChange both, I can see the desired text when I double clicked, but with only MouseDown, it seems to be weird. At times, it just shows the first text.

If you notice, the custom control I posted, overrides SELCHANGE and MOUSE events both, but still exposes them to the app if necessary

Dave S:

I found out that when I double click the text, the previous one I double clicked is displayed in Msgbox.
Do you see that?

[quote=202463:@changwon lee]Michel Bujardet:

Do I have to use SelChange event as well?
With MouseDown and SelChange both, I can see the desired text when I double clicked, but with only MouseDown, it seems to be weird. At times, it just shows the first text.[/quote]

Are you on Mac or Windows ?

Windows.

OK. I just tested under Windows. SelChange takes place after the double click is detected.

If you want to get the selected text anytime it changes,would that be by double click or any other way, simply use SelChange by itself.

If you want to get the text only when DoubleClick took place, here is what you need to do :

  • Add the following code in SelChange (that is where you get the selected text) :

Sub SelChange() If Ticks <= LastDoubleClick+1 then system.DebugLog "changed text" Label1.Text = me.SelText end if End Sub

  • Add a ClickTimer to the window, set to off with a period of 500
  • Add a property LastDoubleClick As Double to the window (new)
  • In the TextArea MouseDown :

Function MouseDown(X As Integer, Y As Integer) As Boolean static oldticks as double = Ticks if Ticks-oldticks <=30 then // What you would do in DoubleClick LastDoubleClick = Ticks oldticks = ticks-120 // To prevent activation by triple click ClickTimer.Enabled = False Return False else ClickTimer.Mode = Timer.ModeSingle ClickTimer.Enabled = False ClickTimer.Period = 520 ClickTimer.Enabled = True end if oldticks = Ticks End Function

The way it works is that in doubleclick we set LastDoubleClick to the Ticks value. Then in Selchange, we verify if no more than one tick (1/30th a second) has elapsed, so it means it is indeed a double click that selected the text, before using SelText.

I tested that before posting.

Thank you so much ! It really works well.