How to Drag & Drop from Spotify to Winforms app
Asked Answered
S

2

6

If someone drags and drops a spotify track from the spotify desktop app to Excel, than Excel shows the artist and title of a song.

I have a winforms application in which I want to do the same. If I drag and drop to a listbox like this....

Private Sub ListBox1_DragEnter(sender As Object, e As DragEventArgs) Handles ListBox1.DragEnter
    ListBox1.Items.Add(e.Data.GetData(DataFormats.Text))
End sub

.... all it ever does, is show the spotify track ID. Since Excel is not designed to read Spotify url's, the data must be in the drag-drop. But whatever dataformat I choose, I only get the ID.

Sidra answered 8/1, 2019 at 8:46 Comment(1)
Try the sample project you can find in this SO question. The project is a test for dragging composite elements from WebPages (images, text, links etc.) to see what DataFormats WinForms controls can detect and handle. It's written in C#, but you just need to run it and drop something in the controls that accept a Drop to see what comes out of it. Most of the times, it's a MemoryStream you have to read the standard way.Caernarvonshire
R
1

if you drag the track and paste it on text editor you will see it is a uri

So within VB.NET DragEventArgs holds the URI

So we need to know what is Spotify URI here is post about that

so Quoting From Spotify post you are dealing with 2 Types of URIs

Spotify URI Codes

What you need to do is register yourself as a developer get a token (your key as developer) to use their service

using that token to authorize yourself to them you can use their Rest API and give them that URI to get the data you would like to have in json

Here is the documentation to the Rest API Spotify Rest API Documentation

Here is another way you could do if that service costs you money to have it

Convert DesktopURI to Web URI using String.Split(":") or Regex both ways works

Thus creating your custom web uri and then paste that web uri to a crawler and fetch your data

Ofcourse you can use .NET URI builder Class to validate it before doing the extra step

Each platform in Spotify has 3 types of uri for so make sure to categorize it to Track , Album , PlayList because what your crawler would be fetching is different

I think their Rest API is free service however make sure you don't violate their SLA Spotify SLA

Funny I am telling you that when my second solution suggestion is not following their SLA

Ritualize answered 19/12, 2019 at 1:24 Comment(0)
B
0
Private Sub lstQueue_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles lstQueue.MouseDown

        If (e.Button = MouseButtons.Right) Then

            'clear previous selection
            lstQueue.SelectedItems.Clear()

            'highlight selected item
            lstQueue.SelectedIndex = lstQueue.IndexFromPoint(e.X, e.Y)

            'initiate movedown event for drag/drop
            Dim ix As Integer = lstQueue.IndexFromPoint(e.Location)
            If ix <> -1 Then
                lstQueue.DoDragDrop(ix.ToString, DragDropEffects.Move)
            End If

        End If

    End Sub

    Private Sub lstQueue_DragEnter(sender As Object, e As DragEventArgs) Handles lstQueue.DragEnter

        'make label visile for drag/drop
        lblMoveLine.Visible = True

    End Sub

    Private Sub lstQueue_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles lstQueue.DragOver

        'allow drag over for drag/drop
        If e.Data.GetDataPresent(DataFormats.Text) Then
            e.Effect = DragDropEffects.Move
            Dim index = lstQueue.IndexFromPoint(lstQueue.PointToClient(New Point(e.X, e.Y)))
            lblMoveLine.Top = (index - lstQueue.TopIndex) * lstQueue.ItemHeight
        End If

    End Sub

    Private Sub lstQueue_DragLeave(sender As Object, e As System.EventArgs) Handles lstQueue.DragLeave

        'hide label for drag/drop
        lblMoveLine.Visible = False

    End Sub

    Private Sub lstQueue_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles lstQueue.DragDrop

        'remove item and add in new location
        If e.Data.GetDataPresent(DataFormats.Text) Then
            Dim dix As Integer = CInt(e.Data.GetData(DataFormats.Text))
            Dim ix As Integer = lstQueue.IndexFromPoint(lstQueue.PointToClient(New Point(e.X, e.Y)))
            If ix <> -1 Then
                Dim obj As Object = lstQueue.Items(dix)
                lstQueue.Items.Remove(obj)
                lstQueue.Items.Insert(ix, obj)
                lstQueue.SelectedIndex = ix
            End If
        End If

        lblMoveLine.Visible = False

    End Sub
Blocker answered 17/12, 2019 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.