[Gambas-user] Mouse X & Y reporting...

vuott at tutanota.com vuott at tutanota.com
Mon Nov 13 01:06:21 CET 2023


Ok Bruce,
ehmmm... if sbungay doesn't want to use the 'Drag&Drop' resources, perhaps he can take a look at the following code.(He has to click on a TreeView item first, then he can click in the 'TextArea' to insert the text of the chosen item).

[code]
Public Sub Form_Open()

  TreeView1.Add("a", "123")
  TreeView1.Add("b", "456")
  TreeView1.Add("c", "789")

  TextArea1.Tracking = True
  TextArea1.Text = "Arma virumque cano, Troiae qui primus ab oris\n" &
                   "Italiam fato profugus Laviniaque venit\n" &
                   "litora, multum ille et terris iactatus et alto,\n" &
                   "vi superum, saevae memorem Iunonis ob iram,\n" &
                   "multa quoque et bello passus, dum conderet urbem\n" &
                   "inferretque deos Latio, genus unde Latinum\n" &
                   "Albanique patres atque altae moenia Romae."

End

Public Sub TextArea1_MouseUp()

  If Not Object.IsValid(TreeView1.Current) Then
    Message.Warning("You have to click on a TreeView item first !")
    Return
  Endif

  Dim s1, s2 As String

  s1 = Left(TextArea1.Text, TextArea1.Pos)
  s2 = Right(TextArea1.Text, Len(TextArea1.Text) - TextArea1.Pos)
  TextArea1.Text = s1 & TreeView1.Current.Text & s2

End
[/code]





12 nov 2023, 23:44 da bsteers4 at gmail.com:

> Ahh  yes i did not try with an empty TextArea
> I would start the Drop method with something like this then....
>
> Public Sub TextArea1_Drop()
>
>  If Not Drag.Formats.Exist("text/plain") then Return
>
>   If Not TextArea1.Text Then 
>     TextArea1.Insert(Drag.Paste()
>     Return
>   Endif
>
> Respects
> BruceS
>
> On Sun, 12 Nov 2023 at 22:30, vuott--- via User <> user at lists.gambas-basic.org> > wrote:
>
>> Bruce,
>> this line:
>>    Dim sTxt As String = Split(TextArea1.Text, "\n")[iLine]
>> gives me the error «Out of Bound».
>>
>> However, sbungay didn't write whether the "TextArea" already contains text or not; and, if it does, whether this text is to be replaced or supplemented by text taken with the mouse from the "TreeView" Control and released into the "TextArea".
>>
>> regards
>>
>>
>> 12 nov 2023, 19:32 da >> bsteers4 at gmail.com>> :
>>
>>> aah yes.
>>> It has a CursorAt(Pos As Integer) As Point but that's the wrong way round
>>> there is no CursorFrom(X As Integer, Y As Integer) As Integer or equivalent.
>>>
>>> So we could do this..
>>> (there is possibly an easier or built in way)
>>>
>>> Public Sub TextArea1_Drop()
>>>
>>>   ' get line number and line text from Drag.Y
>>>   Dim iPos As Integer = TextArea1.ToPos(Drag.Y \ TextArea1.Font.Height, 0)
>>>   Dim iLine As Integer = TextArea1.ToLine(iPos)
>>>   Dim sTxt As String = Split(TextArea1.Text, "\n")[iLine]
>>>
>>>   ' add chars of the text to a string and measure it until
>>>   ' it is further than Drag.X
>>>   Dim sTo As String
>>>   Dim i, X, iLast As Integer
>>>   Repeat
>>>     Inc i
>>>     sTo = Mid(sTxt, 1, i)
>>>     X = TextArea1.Font.TextWidth(sTo)
>>>     If X > Drag.X Then Break
>>>     iLast = sTo.Len - 1
>>>   Until i > sTxt.Len
>>>
>>>   ' Set the TextArea1 cursor to line And column Pos and insert drag text
>>>   TextArea1.Pos = TextArea1.ToPos(iLine, iLast)
>>>   TextArea1.Insert(Drag.Paste())
>>>
>>> End
>>>
>>> That's only simple code and may need some more error handling but should get you started :)
>>>
>>> BruceS
>>>
>>> On Sun, 12 Nov 2023 at 17:02, vuott-
>>>
>>> -- via User <>>> user at lists.gambas-basic.org>>> > wrote:
>>>
>>>> I do not believe that he can position the text in the TextArea, "directly" according to the ".X" and ".Y" Properties, or ".ScreenX" and ".ScreenY" of the Mouse Class.
>>>>
>>>>
>>>>
>>>>
>>>> 12 nov 2023, 16:42 da >>>> bsteers4 at gmail.com>>>> :
>>>>
>>>>>
>>>>>
>>>>> On Sun, 12 Nov 2023 at 15:04, sbungay <>>>>> sbungay at smartsonsite.com>>>>> > wrote:
>>>>>
>>>>>>    I've been struggling with dragging data from a TreeView to a Text
>>>>>> Area, and I THINK I've found the source of the frustration.
>>>>>>
>>>>>>    Both the TreeView and TextArea have Tracking enabled. So everything
>>>>>> is OK there.
>>>>>>
>>>>>>    The Drag begins and the Mouse.X and Mouse.Y coordinates are given
>>>>>> relative to where it was in the TreeView when that event fires. Lets say
>>>>>> the values are, respectively, 21 and 88. Dragging the text from the
>>>>>> TreeView to the position in the TextArea where it needs to be inserted
>>>>>> it ALWAYS inserts it in the wrong spot, and the Mouse.X and Mouse.Y
>>>>>> remain set at 21 and 88, which explains why insertion did not take place
>>>>>> at the desired location.
>>>>>>
>>>>>>   Shouldn't the Mouse.X and Mouse.Y be updated to the coordinates of the
>>>>>> pointer relative to the control over which the drop occurred?
>>>>>>
>>>>>> --
>>>>>> Steve.
>>>>>>
>>>>>
>>>>> No because Mouse.X and Mouse.Y are object relative positions.so belong to the calling objects Drag event
>>>>>
>>>>>
>>>>> See Drag.X and Drag.Y
>>>>>
>>>>> Or use Mouse.ScreenX , Mouse.ScreenY with the dropped objects screen positions.
>>>>>
>>>>> Dim iDropRelativeMouseX = Mouse.ScreenX - MyDroppedControl.ScreenX
>>>>> Dim iDropRelativeMouseY = Mouse.ScreenY - MyDroppedControl.ScreenY
>>>>>
>>>>> BruceS
>>>>>
>>>>>
>>>>
>>>>
>>>> ----[ >>>> http://gambaswiki.org/wiki/doc/netiquette>>>>  ]----
>>>>
>>
>>
>> ----[ >> http://gambaswiki.org/wiki/doc/netiquette>>  ]----
>>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20231113/03e79a78/attachment.htm>


More information about the User mailing list