Jump to content

Recommended Posts

Posted

Boas Pessoal,

sou novo por aqui, depois de muita pesquisa, não consigo encontrar informação relevante ao DRAG DROP em ListBox ou ListView que permita escolher a linha onde querem fazer o DROP.

Vou tentar explicar melhor, no programa que estou a desenvolver, pretendo mover dados de uma lista para outra com a função DRAG DROP, mas pretendo que o item fique na linha onde deixo ficar o rato no DROP.

Exemplo:

A1 A2

B1 B2

C1 C2

D1 D2

E1 E2

F1 F2

G1 G2

Se "pegar" no item C1 e o largar entre C2 e D2 a list mude para:

A1 A2

B1 B2

D1 C2

E1 C1

F1 D2

G1 E2

F2

G2

Código para o OLE DRAG DROP já sei fazer.

Só pretendo mesmo saber se é possível fazer o que pretendo no listview ou listbox.

---

Cumprimentos

Posted

Boas pessoal, consegui desenvolver um pouco mais desde ultima vez, gostaria de saber se alguém me pode ajudar a modificar o código para poder fazer o DRAG N DROP de MultiSelect em vez de mover item a item.

Este código basicamente recebe os itens da linha seleccionada e cola as colunas a frente.

Ex: Encomenda0|Ordem0|...|...|...

Depois do DROP, os item são colocados na linha anterior onde for efectuado o DROP.

Private mobjFromList As MSForms.ListBox
Private mlFrom As Long
Private Sub CommandButton2_Click()
   Dim L As Long
   For L = 0 To 10
    ListBox1.AddItem "Encomenda " & L
    ListBox1.List(L, 1) = "Ordem de Fabrico" & L
   Next L
End Sub
Private Sub ListBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
   Dim objData As DataObject
   Dim lEffect As Long, lngIndex As Long
   Dim strData As String
   Const lLEFTMOUSEBUTTON As Long = 1
   If Button = lLEFTMOUSEBUTTON Then
  Set objData = New DataObject
  Set mobjFromList = Me.ListBox1

  With Me.ListBox1
    For lItem = 0 To ListBox1.ListCount - 1
	    If ListBox1.Selected(lItem) = True Then
    For lngIndex = 0 To .ColumnCount - 1
	    strData = strData & "|" & .List(lItem, lngIndex)
    Next lngIndex
	    End If
    Next lItem

	 objData.SetText Mid$(strData, 2)
	 mlFrom = .ListIndex
  End With

  lEffect = objData.StartDrag
   End If


End Sub
Private Sub ListBox1_BeforeDragOver(ByVal Cancel As MSForms.ReturnBoolean, _
ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As Single, _
ByVal DragState As MSForms.fmDragState, ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer)
   Cancel = True
   Effect = fmDropEffectMove
End Sub

Private Sub ListBox1_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, ByVal Action As MSForms.fmAction, _
ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As Single, ByVal Effect As MSForms.ReturnEffect, _
ByVal Shift As Integer)
   Dim varData, lngIndex As Long
   Dim lTo As Long
   With Me.ListBox1
    lTo = .TopIndex + Int(Y * 0.85 / .Font.Size)
    If lTo >= .ListCount Then lTo = .ListCount
    Cancel = True
    Effect = fmDropEffectMove
    varData = Split(Data.GetText, "|")
    .AddItem , lTo
    For lngIndex = LBound(varData) To UBound(varData)
	 .List(lTo, lngIndex) = varData(lngIndex)
  Next lngIndex
    If mobjFromList = Me.ListBox1 And lTo < mlFrom Then
	    mobjFromList.RemoveItem (mlFrom + 1)
    Else
	    mobjFromList.RemoveItem mlFrom
    End If
    Set mobjFromList = Nothing
   End With
End Sub
Private Sub ListBox2_BeforeDragOver(ByVal Cancel As MSForms.ReturnBoolean, _
ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As Single, _
ByVal DragState As MSForms.fmDragState, ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer)
   Cancel = True
   Effect = fmDropEffectMove
End Sub

Private Sub ListBox2_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, ByVal Action As MSForms.fmAction, _
ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As Single, ByVal Effect As MSForms.ReturnEffect, _
ByVal Shift As Integer)
On Error Resume Next
   Dim varData, lngIndex As Long
   Dim lTo As Long
   With Me.ListBox2
    lTo = .TopIndex + Int(Y * 0.85 / .Font.Size)
    If lTo >= .ListCount Then lTo = .ListCount
    Cancel = True
    Effect = fmDropEffectMove
    varData = Split(Data.GetText, "|")
    .AddItem , lTo
    For lngIndex = LBound(varData) To UBound(varData)
	 .List(lTo, lngIndex) = varData(lngIndex)
  Next lngIndex
    If mobjFromList = Me.ListBox2 And lTo < mlFrom Then
	    mobjFromList.RemoveItem (mlFrom + 1)
    Else
	    mobjFromList.RemoveItem mlFrom
    End If
    Set mobjFromList = Nothing
   End With
End Sub

---

Cumprimentos

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.