Drop & Drag within a Treeview

Ask about the individual Gambas components here.
Post Reply
User avatar
Quincunxian
Posts: 171
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Drop & Drag within a Treeview

Post by Quincunxian »

I'm trying to determine how to find the key ( or any other identifying data) when you drag and drop inside the same Treeview.

The problem is that the Drop event does not seem to be activating for the treeview ?
I've test this with some listboxes and can successfully drop and drag between them - the drop event works.
I can get the key of the treeview item that is selected to move but when releasing the mouse, there is nothing triggered.


Attached is a project to see if someone can find what I'm doing wrong with the treeview

My purpose is to be able to select a treeview item and move it to another parent or change the sequence.

Any advice appreciated.
Attachments
TreeViewDropAndDrag.tar.gz
Treeview example code
(16.22 KiB) Downloaded 418 times
Cheers - Quin.
I code therefore I am
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: Drop & Drag within a Treeview

Post by sjsepan »

Quin,
Started to take a look at this today. In order to see what events are firing and in what sequence, I made some mods to part of the code; using the event name so my slow brain can pick out the info easier, and outputting the sequence to the Console window so I can see the history of the events:
Public Sub Trv_Main_Click()

    FunctionKey = "_Click"
    FromKey = Trv_Main.Current.Key
    UpdateDisplay

End

Public Sub Trv_Main_Drop()

    FunctionKey = "_Drop"

    UpdateDisplay

End

Public Sub Trv_Main_MouseDrag()

    If Trv_Main.Current.Text <> "" Then
        Fromkey = Trv_Main.Current.Key

        Trv_Main.Drag(Fromkey, "text/html")
    Endif

    Trv_Main.MoveCurrent
    FunctionKey = "_MouseDrag"

    UpdateDisplay

End

Public Sub Trv_Main_Drag()

    FunctionKey = "_Drag"

    UpdateDisplay

End

Private Sub UpdateDisplay()

    Txt_Current.text = Trv_Main.Current.Text
    Txt_Item.text = Trv_Main.Item.Text
    Txt_Function.text = FunctionKey

    Print Subst("Current='&1', Item='&2', Event='&3'\r\n", Trv_Main.Current.Text, Trv_Main.Item.Text, FunctionKey)

End
Running the code, and dragging item20 over item19, I see:
Current='Item20', Item='Item20', Event='_Click'

Current='Item20', Item='Home', Event='_Drag'

Current='Item20', Item='Home', Event='_Drop'

Current='Item20', Item='Item20', Event='_MouseDrag'
Examining results to see what actually occurs...

Steve
User avatar
Quincunxian
Posts: 171
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Drop & Drag within a Treeview

Post by Quincunxian »

Thanks Steve - let me know what you find out.
It is always possible that it's a "me and my computer" problem.
Cheers - Quin.
I code therefore I am
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: Drop & Drag within a Treeview

Post by sjsepan »

Just so that I understand, you indicated initially that the Drop event did not seem to be firing, right?
The problem is that the Drop event does not seem to be activating for the treeview ?
I do see what looks like a Drop event activation, in the 3rd event in the sequence caused by dragging item20 onto item19 (see output, previous reply).
In the ListBoxes form I see that in the Drop event you are explicitly moving the item:
    ListBox1.Add(Drag.data)
    ListBox2.Remove(ListIndex)
However, in the main form the Drop event handler doesn't do anything explicit with the TreeView yet (Note - I previously modified the value stored in FunctionKey but the code is otherwise the same):
Public Sub Trv_Main_Drop()

    FunctionKey = "_Drop"

    UpdateDisplay

End
I'll admit, though, that I don't see any info in the output about the destination of the Drop (Item19 in this case). But then the listboxes example does not do so either, but places the dragged item at the end. Here, though, It seems like you would want to identify which item you hovered over, and I have not found a way to obtain than info (yet). Still looking around...
User avatar
Quincunxian
Posts: 171
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Drop & Drag within a Treeview

Post by Quincunxian »

Thanks Steve - it looks like it may be a bug that has crept in as I'm sure(?) it used to work a few gambas versions ago.
I'll post on the user email list and see what can be done.
Cheers - Quin.
I code therefore I am
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: Drop & Drag within a Treeview

Post by sjsepan »

OK, never used it yet myself, and after trying, I was starting to come to the conclusion that Drag/Drop would work between controls (like you did with the listboxes), but not within one treeview control. I just could not see a way to get the destination item. Anyway, I had to admit defeat.
Hope you find out more; let us know if you get a response.
Steve S
User avatar
Quincunxian
Posts: 171
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Drop & Drag within a Treeview - Solved.

Post by Quincunxian »

Hi Steve,
I got a reply from the mailing list group from an Italian guy called Gianluigi.
He updated my test project code with the following and it now works.
the "Trv_Main.FindAt(Drag.X, Drag.Y)" is the key bit of code i think.

The only thing that I want to do that is different in the attached example project is maintain the original key of the item,
remove the duplicate from the original position and update the data in the database that the Treeview detail comes from.
That should be easy enough now that I understand the core process.
Public Sub Trv_Main_MouseDrag()

  If Not Mouse.Left Then Return

  With Trv_Main
    If .FindAt(Mouse.X, Mouse.Y) Then Return
    If Not .Key Then Return
    Drag.Icon = .Current.Picture
    .Drag(.Key)
  End With

End

Public Sub Trv_Main_DragMove()

  'IF Drag.Type <> Drag.Image THEN STOP EVENT

  With Trv_Main
    If Not .FindAt(Drag.X, Drag.Y) Then
      Drag.Show(Trv_Main, .Item.X, .Item.Y, .Item.W, .Item.H)
    Else 
      Drag.Show(Trv_Main)
    Endif
  End With

End

Public Sub Trv_Main_Drop()

  Dim sKey As String

  With Trv_Main

    If Not .FindAt(Drag.X, Drag.Y) Then
      sKey = .Item.Key
    Endif
    
    Inc $iKey
  
    If Drag.Type = Drag.Text Then 
      .Add($iKey, Drag.Data,, sKey).EnsureVisible
      TextBox1.Text = $iKey & ", " & sKey & ", " & Drag.Data
      FunctionKey = "Drop Event"
      UpdateDisplay
    Endif

  End With

End
Attachments
TreeViewDropAndDrag-2-0.0.1.tar.gz
From - Gianluigi. V2
(13.75 KiB) Downloaded 422 times
Cheers - Quin.
I code therefore I am
Post Reply