Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown On Error Resume Next ListBox1.DoDragDrop(ListBox1.Items(ListBox1.IndexFromPoint(e.X, e.Y)), DragDropEffects.Copy Or DragDropEffects.Move) End Sub Private Sub ListBox4_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox4.DragOver If Not (e.Data.GetDataPresent(GetType(System.String))) Then e.Effect = DragDropEffects.None Else IndexdInsertion = ListBox4.IndexFromPoint(ListBox4.PointToClient(New Point(e.X, e.Y))) e.Effect = DragDropEffects.Copy End If End Sub Private Sub ListBox4_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox4.DragDrop Dim item As Object = CType(e.Data.GetData(GetType(System.String)), System.Object) ListBox4.Items.Add(item) End Sub
Private Sub Programme_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.SeanceDébutTableAdapter.Fill(Me.CoachingDataSet.SeanceDébut) ListBox1.DataSource = CoachingDataSet.Tables("SeanceDébut") ListBox1.DisplayMember = "Nom" End sub
Public IndexdInsertion As Integer Const CtrlMask As Byte = 8 'exemple ListBox 'Dans le listBox1 de départ, l'évènement MouseDown déclenche le glisser déplacer par DoDragDrop. Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown On Error Resume Next 'si la listBox est vide ListBox1.DoDragDrop(ListBox1.Items(ListBox1.IndexFromPoint(e.X, e.Y)), DragDropEffects.Copy Or DragDropEffects.Move) End Sub 'ListBox1.IndexFromPoint(e.X, e.Y) retourne l'Index de l'item ou se trouve la souris à partir des coordonnées e.x et e.y du pointeur) 'DoDragDrop a 2 arguments: l'élément à draguer et le mode 'DragOver qui survient quand la souris se balade sur le contrôle d'arrivé, vérifie si le Drop reçoit bien du texte et met dans IndexdInsertion le listItem qui est sous la souris. 'Noter que e.x et e.y sont les coordonnées écran , il faut les transformer en coordonnées client (du contrôle) par PointToClient afin d'obtenir l'index de l'item ou se trouve la souris (en utilisant IndexFromPoint. Private Sub ListBox2_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragOver If Not (e.Data.GetDataPresent(GetType(System.String))) Then e.Effect = DragDropEffects.None Else IndexdInsertion = ListBox2.IndexFromPoint(ListBox2.PointToClient(New Point(e.X, e.Y))) e.Effect = DragDropEffects.Copy End If End Sub 'Enfin dans DragDrop, on récupère le texte dans Item et on ajoute un item après l'item pointé. Private Sub ListBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragDrop Dim item As Object = CType(e.Data.GetData(GetType(System.String)), System.Object) ListBox2.Items.Insert(IndexdInsertion + 1, item) 'Si la touche Ctrl n'a pas été appuyée, enlever le texte source pour effectuer le 'mouvement DragDrop. If (e.KeyState And CtrlMask) <> CtrlMask Then ListBox1.Items.Remove(ListBox1.SelectedItem) End If End Sub
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPrivate Sub Programme_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.SeanceDébutTableAdapter.Fill(Me.CoachingDataSet.SeanceDébut) ListBox1.DataSource = CoachingDataSet.Tables("SeanceDébut") ListBox1.DisplayMember = "Nom" End Sub