Option Explicit On Imports System Imports System.IO Imports System.Text Imports System.Windows.Forms Partial Public Class Form1 Sub Form1_Load(ByVal sender As System.Object, ByVal e As EventArgs) Handles MyBase.Load ListBox1.SelectionMode = SelectionMode.MultiSimple ListBox2.SelectionMode = SelectionMode.MultiSimple ListBox1.AllowDrop = True ListBox2.AllowDrop = True Call moulinette() Call litProduits() Call afficheCompteurs() End Sub Private Sub ListBox_MouseMove(ByVal source As ListBox, ByVal e As MouseEventArgs) Handles ListBox1.MouseMove, ListBox2.MouseMove If e.Button = MouseButtons.Left Then source.SelectedIndex = source.IndexFromPoint(e.Location) If source.SelectedIndex >= 0 Then '# Si un element est survollé source.DoDragDrop(source, DragDropEffects.Move) End If End If End Sub Sub ListBox_DragOver(ByVal destination As ListBox, ByVal e As DragEventArgs) Handles ListBox1.DragOver, ListBox2.DragOver If e.Data.GetDataPresent(GetType(ListBox)) Then Dim source As ListBox = e.Data.GetData(GetType(ListBox)) '# On quitte si on survolle le ListBox de départ If source Is destination Then e.Effect = DragDropEffects.None Else e.Effect = DragDropEffects.Move End If End If End Sub Sub ListBox_DragDrop(ByVal destination As ListBox, ByVal e As DragEventArgs) Handles ListBox1.DragDrop, ListBox2.DragDrop If e.Data.GetDataPresent(GetType(ListBox)) Then Dim source As ListBox = e.Data.GetData(GetType(ListBox)) For Each item In source.SelectedItems destination.Items.Add(item) Next Do Until (source.SelectedIndex < 0) source.Items.RemoveAt(source.SelectedIndex) Loop Call afficheCompteurs() End If End Sub Sub ListBox_GiveFeedback(ByVal sender As ListBox, ByVal e As GiveFeedbackEventArgs) Handles ListBox1.GiveFeedback, ListBox2.GiveFeedback e.UseDefaultCursors = False If sender Is ListBox1 Then Cursor.Current = New Cursor(My.Resources.vaIco.Handle) '# Depuis une icone Else Cursor.Current = New Cursor(My.Resources.viensBmp.GetHicon) '# Depuis un BMP (devrait fonctionner avec un PNG) End If End Sub Sub afficheCompteurs() Label1.Text = ListBox1.Items.Count Label2.Text = ListBox2.Items.Count Label5.Text = ListBox1.Items.Count + ListBox2.Items.Count End Sub Sub litProduits() ListBox1.Items.Clear() '# Ajoute directement chaque ligne du fichier texte en tant qu'Item de la ListBox ListBox1.Items.AddRange(File.ReadAllLines("produits.txt", Encoding.Default)) End Sub '# Version "classique" Sub ecritProduit() With New StreamWriter("produits.txt", False, Encoding.Default) For Each Item As String In ListBox1.Items .WriteLine(Item) Next .Close() End With End Sub '# Version Linq, pas ma favorite, bien qu'efficace 'Sub ecritProduit() ' Dim Items(Me.ListBox1.Items.Count - 1) As Object ' Me.ListBox1.Items.CopyTo(Items, 0) ' System.IO.File.WriteAllLines("produits.txt", Items.Select(Function(Item) Item.ToString()).ToArray(), Encoding.Default) 'End Sub Sub moulinette() For Each line As String In File.ReadAllLines("base.txt", Encoding.Default) '# Découpe les lignes qui seraient trop longues... If line.Length > 22 Then line = line.Substring(0, 22).Trim() End If '# Ajoute l'element dans la liste1 ListBox1.Items.Add(line.ToUpper) Next ecritProduit() End Sub End Class
' courses form1 Option Explicit On Public Class Form1 Dim lindex As Integer Dim lindexOrigine As Integer Dim concerne As Byte Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim i As Integer ListBox1.Items.Clear() ' 32 maxi ListBox2.Items.Clear() ListBox3.Items.Clear() ListBox1.AllowDrop = True ListBox2.AllowDrop = True For i = 65 To 90 ListBox3.Items.Add(Chr(i)) Next i Me.Left = 10 Me.Top = 10 concerne = 0 'Call moulinette() Call litProduits() End Sub
Option Explicit On Imports System.IO Imports System.Text Partial Public Class Form1 Private Structure DDS Public ListBox As ListBox Public Item As String Public Index As Integer End Structure Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load AddHandler ListBox1.MouseDown, AddressOf ListBox_MouseDown AddHandler ListBox2.MouseDown, AddressOf ListBox_MouseDown AddHandler ListBox1.DragOver, AddressOf ListBox_DragOver AddHandler ListBox2.DragOver, AddressOf ListBox_DragOver AddHandler ListBox1.DragDrop, AddressOf ListBox_DragDrop AddHandler ListBox2.DragDrop, AddressOf ListBox_DragDrop ListBox1.Items.Clear() ' 32 maxi ListBox2.Items.Clear() ListBox3.Items.Clear() ListBox1.AllowDrop = True ListBox2.AllowDrop = True For Each c As Char In "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ListBox3.Items.Add(c) Next c Me.Left = 10 Me.Top = 10 Call moulinette() Call litProduits() End Sub Sub ListBox_MouseDown(ByVal sender As ListBox, ByVal e As System.Windows.Forms.MouseEventArgs) If sender.Items.Count > 1 Then Dim data As DDS data.Index = sender.IndexFromPoint(e.X, e.Y) If data.Index >= 0 Then sender.SelectedIndex = data.Index data.ListBox = sender data.Item = sender.SelectedItem.ToString() Try sender.DoDragDrop(data, DragDropEffects.Move) Catch End Try End If End If End Sub Sub ListBox_DragOver(ByVal sender As ListBox, ByVal e As System.Windows.Forms.DragEventArgs) If e.Data.GetDataPresent(GetType(DDS)) Then Dim data As DDS = e.Data.GetData(GetType(DDS)) If data.ListBox Is sender Then e.Effect = DragDropEffects.None Exit Sub End If e.Effect = DragDropEffects.Move End If End Sub Sub ListBox_DragDrop(ByVal sender As ListBox, ByVal e As System.Windows.Forms.DragEventArgs) If e.Data.GetDataPresent(GetType(DDS)) Then Dim data As DDS = e.Data.GetData(GetType(DDS)) sender.Items.Add(data.Item) data.ListBox.Items.RemoveAt(data.Index) Call afficheCompteurs() End If End Sub [...] End Class
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
X-K-2 - Exemple N° 2 (plus complexe)
Glisser déplacer une ligne d'une listBox 'ListBox1' vers une listBox 'ListBox2'.
Créer une ListBox1
Créer une listBox2 avec sa propriété AllowDrop=True 'listBox2 accepte le 'lâcher'
Dans l'en-tête du module ajouter:
Public IndexdInsertion As Integer ' Variable contenant l'index ou sera inséré la ligne
'Eventuellement pour l'exemple charger les 2 ListBox avec des chiffres pour pouvoir tester.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase
Dim i As Integer
For i = 1 To 100
ListBox1.Items.Add(i.ToString)
Next
For i = 1 To 100
ListBox2.Items.Add(i.ToString)
Next
End Sub
'Dans le listBox 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
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.
Cours VB.NET par Philippe Lasserre
- 685 -
Les sources présentées sur cette pages sont libres de droits, et vous pouvez les utiliser à votre convenance. Par contre la page de présentation
de ces sources constitue une oeuvre intellectuelle protégée par les droits d'auteurs. Copyright © - . Aucune reproduction, même partielle, ne peut
être faite de ce site et de l'ensemble de son contenu : textes, documents, images, etc sans l'autorisation expresse de l'auteur. Sinon vous encourez
selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts.
http://plasserre.developpez.com/cours/vb-net/
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)
End Sub
Sub ListBox1_GiveFeedback(sender As Object, e As System.Windows.Forms.GiveFeedbackEventArgs) Handles ListBox1.GiveFeedback e.UseDefaultCursors = False Cursor.Current = New Cursor(My.Resources.vaBmp.GetHicon) End Sub
For Each c As Char In "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Je n'ai jamais appris, mais j'en ai pas besoin puisque la preuve je sais faire autrement?
question, pourquoi ne pas poursuivre le codage en VB6, au final ?
il est toujours dispo au téléchargement sur le site de Microsoft...