1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
Imports System.IO
Public Class Form2
Dim pathFile As String = Directory.GetCurrentDirectory() + "\textfile1.txt"
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListView2.View = View.Details
FillListViews()
End Sub
Private Sub FillListViews()
Dim Lines() As String = File.ReadAllLines(pathFile)
Dim line As String
Dim List As New List(Of String)
Dim fruit As String = Nothing
Dim couleur As String = Nothing
Dim strQuantite As String = Nothing
For j As Integer = 0 To Lines.Length - 1
line = Lines(j)
Dim subList As New List(Of String)
If Not String.IsNullOrWhiteSpace(line) Then
Dim arrStr() As String = line.Split(";")
fruit = arrStr(0) : couleur = arrStr(1) : strQuantite = "1"
List.Add(fruit)
List.Add(couleur)
List.Add(strQuantite)
End If
Next
FillListViewSimple(List) 'listview1
'traitemebt des fruits doublons Fruit de meme couleur
'et cumul des quantites des fruits doublons dans
'liste de travail
Dim TempList As New List(Of String) 'liste de travail
For j As Integer = 0 To List.Count - 1 Step 3
fruit = List(j)
couleur = List(j + 1)
strQuantite = List(j + 2)
If Not TempList.Contains(fruit) Then 'Fruit et Couleur inexistants.AJOUT
TempList.Add(fruit)
TempList.Add(couleur)
TempList.Add(strQuantite)
ElseIf TempList.Contains(fruit) And Not TempList.Contains(couleur) Then
'Fruit existant et Couleur inexistant.AJOUT
TempList.Add(fruit) : TempList.Add(couleur) : TempList.Add(strQuantite)
ElseIf TempList.Contains(fruit) And TempList.Contains(couleur) Then
'Fruit & Couleur existants.MAJ Quantites
Dim indexFruit As Integer = TempList.IndexOf(fruit) 'retrouve index de fruit
indexFruit += 2 '
strQuantite = TempList(indexFruit)
Dim qty As Integer = Convert.ToInt32(strQuantite)
qty += 1 'cumul
strQuantite = qty.ToString
TempList(indexFruit) = strQuantite
End If
Next
'libere la memoire
List = Nothing
' Fill du ListView2
FillListViewTraite(TempList) 'listeview2
End Sub
Private Sub FillListViewSimple(ByVal maListe As List(Of String)) 'listview1
Dim fruit As String = Nothing
Dim couleur As String = Nothing
Dim strQuantite As String = Nothing
For j As Integer = 0 To maListe.Count - 1 Step 3
fruit = maListe(j) : couleur = maListe(j + 1) : strQuantite = maListe(j + 2)
Dim lvItem As New ListViewItem(fruit)
lvItem.SubItems.Add(couleur)
lvItem.SubItems.Add(strQuantite)
ListView1.Items.Add(lvItem)
Next
ListView1.Columns.Add("Fruit", 100, HorizontalAlignment.Left)
ListView1.Columns.Add("Couleur", 100, HorizontalAlignment.Left)
ListView1.Columns.Add("Quantite", 100, HorizontalAlignment.Left)
End Sub
Private Sub FillListViewTraite(ByVal monTempListe As List(Of String)) 'listview2
Dim fruit As String = Nothing
Dim couleur As String = Nothing
Dim strQuantite As String = Nothing
For j As Integer = 0 To monTempListe.Count - 1 Step 3
fruit = monTempListe(j) : couleur = monTempListe(j + 1) : strQuantite = monTempListe(j + 2)
Dim lvItem As New ListViewItem(fruit)
lvItem.SubItems.Add(couleur)
lvItem.SubItems.Add(strQuantite)
ListView2.Items.Add(lvItem)
Next
ListView2.Columns.Add("Fruit", 100, HorizontalAlignment.Left)
ListView2.Columns.Add("Couleur", 100, HorizontalAlignment.Left)
ListView2.Columns.Add("Quantite", 100, HorizontalAlignment.Left)
End Sub
End Class |
Partager