Meilleur auteur de réponses
Comment faire une gestion fichier et liste

Question
-
Réponses
-
Bonjour,
Joe, ce que je trouve étrange est que vos postes sont toujours marquées comme ‘Utile’ par les mêmes utilisateurs. Chose encore plus étrange, la seule activité de ces utilisateurs sur le forum est de marquer vos postes.
Un de ces utilisateurs est irolog, dont le premier poste sur le forum on peut le voir ici.
Je vous prie d’expliquer ces coïncidences.
Cordialement,
Alex
Alex Petrescu - MSFT- Marqué comme réponse Alex Petrescu lundi 29 mars 2010 14:44
Toutes les réponses
-
Bonjour,
Tiens, j'ai adapté du code que j'avais, testé a priori ça marche, t'as plus qu'à copier/coller dans un form1 et y rajouter une listebox1 et un label1, et chat marche !' OBJETS = form1 + ListBox1 + label1 = 3 Option Explicit On Public Class Form1 Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Label1.Text = "Depuis la liste, pour ajouter = clique droit et pour retirer = double-clique sur la ligne" Me.Text = " Gestion fichier" Select Case System.IO.File.Exists(CStr(My.Application.Info.DirectoryPath & "\monFichier.txt")) Case True : Call litFichier() Case False : Call primoChargeFichier() End Select Me.Show() End Sub Sub primoChargeFichier() Try Dim p As New System.IO.StreamWriter(CStr(CStr(My.Application.Info.DirectoryPath & "\monFichier.txt"))) Dim i As Long = 0 Dim ligne As String = "" For i = 1 To 5 Select Case i Case 1 : ligne = "un" Case 2 : ligne = "deux" Case 3 : ligne = "trois" Case 4 : ligne = "quatre" Case 5 : ligne = "cinq" End Select p.WriteLine(ligne) Next i p.Close() p.Dispose() p = Nothing Call litFichier() Exit Try Catch ex As Exception Dim m As String = "" m = m & "(primoChargeFichier) : Erreur sur le fichier : " & vbLf & vbLf m = m & Err.Number & vbLf & vbLf m = m & Err.Description & vbLf & vbLf m = m & Err.Source & vbLf & vbLf MsgBox(m, vbExclamation) Reset() End Try End Sub Sub litFichier() Try Dim p As New System.IO.StreamReader(CStr(My.Application.Info.DirectoryPath & "\monFichier.txt")) Dim ligne As String = "" Dim i As Long = 0 ListBox1.Items.Clear() Do While p.Peek() >= 0 ligne = p.ReadLine() ' ligne blanche séparatrice ListBox1.Items.Add(ligne) Loop p.Close() p.Dispose() p = Nothing Exit Try Catch ex As Exception Dim m As String = "" m = m & "(litFichier) : Erreur sur le fichier : " & vbLf & vbLf m = m & "Erreur N° : " & Err.Number & vbLf & vbLf m = m & Err.Description & vbLf & vbLf MsgBox(m, vbExclamation) Reset() End Try End Sub Sub ecritFichier() If ListBox1.Items.Count < 1 Then Exit Sub Try Dim p As New System.IO.StreamWriter(CStr(CStr(My.Application.Info.DirectoryPath & "\monFichier.txt"))) Dim i As Long = 0 Dim ligne As String = "" For i = 0 To ListBox1.Items.Count - 1 p.WriteLine(ListBox1.Items(i)) Next i p.Close() p.Dispose() p = Nothing Call litFichier() Exit Try Catch ex As Exception Dim m As String = "" m = m & "(ecritFichier) : Erreur sur le fichier : " & vbLf & vbLf m = m & Err.Number & vbLf & vbLf m = m & Err.Description & vbLf & vbLf m = m & Err.Source & vbLf & vbLf MsgBox(m, vbExclamation) Reset() End Try End Sub Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick Dim r As String = "" If ListBox1.Items.Count < 1 Then MsgBox("La liste est vide ", vbExclamation) Exit Sub End If r = MsgBox("Supprimer cette ligne" & vbLf & vbLf & ListBox1.Items(ListBox1.SelectedIndex), vbQuestion + vbYesNo + vbDefaultButton2) If r <> vbYes Then Exit Sub ListBox1.Items.Remove(ListBox1.Items(ListBox1.SelectedIndex)) If ListBox1.Items.Count < 1 Then Exit Sub Try Dim p As New System.IO.StreamWriter(CStr(CStr(My.Application.Info.DirectoryPath & "\monFichier.txt"))) Dim i As Long = 0 For i = 0 To ListBox1.Items.Count - 1 p.WriteLine(ListBox1.Items(i)) Next i p.Close() p.Dispose() p = Nothing Call litFichier() Exit Try Catch ex As Exception Dim m As String = "(list1=dbl-clc) : Erreur sur le fichier : " & vbLf & vbLf m = m & "Erreur N° : " & Err.Number & vbLf & vbLf m = m & Err.Description & vbLf & vbLf MsgBox(m, vbExclamation) Reset() End Try End Sub Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown If e.Button <> Windows.Forms.MouseButtons.Right Then Exit Sub Dim ligne As String = "" Dim i As Long = 0 ligne = InputBox("Saisir la donnée à insérer ? ") If Len(ligne) < 1 Or ligne = "" Then Exit Sub For i = 0 To ListBox1.Items.Count - 1 If Trim(ligne) = ListBox1.Items(i) Then MsgBox("Cette enregistrement existe déjà ", vbExclamation) Exit Sub End If Next ListBox1.Items.Add(Trim(ligne)) Call ecritFichier() End Sub Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Reset() End End Sub End Class
Cordialement.
Joe ___ Romans et Logiciels ___ http://irolog.free.fr- Proposé comme réponse EhJoe vendredi 5 mars 2010 03:49
- Marqué comme réponse irolog vendredi 5 mars 2010 09:45
- Non marqué comme réponse Alex Petrescu lundi 29 mars 2010 14:44
-
Bonjour,
C'est bien Joe mais un peu long
Voici une solution plus simple:
crée un bouton et colle le code suivant:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Dim str_lignes(ListBox1.Items.Count + 1) As String
For i = 0 To ListBox1.Items.Count - 1
str_lignes(i + 1) = ListBox1.Items(i)
Next
File.WriteAllLines("c:\fichier1.txt", str_lignes)
End Sub
End Class
fred -
Bonjour,
Il me semble qu'il existe une façon plus simple
Dim oArrayList As New ArrayList Dim oFile As New StreamReader("c:\InstallHelper.log") ' ' Methode 1 ' Do Until oFile.Peek = -1 oArrayList.Add(oFile.ReadLine) Loop ListBox1.DataSource = oArrayList ' ' Methode 2 ' Do Until oFile.Peek = -1 ListBox1.Items.Add(oFile.ReadLine) Loop
La méthode 1 est beacoup plus rapide quand on a un gros fichier
La methode 2 est ideal pour les petits fichiers avec tres peu de lignes, au niveau performance la methode 1 est la meilleurs
Cordialement, Troxsa -
Bonjour,
Joe, ce que je trouve étrange est que vos postes sont toujours marquées comme ‘Utile’ par les mêmes utilisateurs. Chose encore plus étrange, la seule activité de ces utilisateurs sur le forum est de marquer vos postes.
Un de ces utilisateurs est irolog, dont le premier poste sur le forum on peut le voir ici.
Je vous prie d’expliquer ces coïncidences.
Cordialement,
Alex
Alex Petrescu - MSFT- Marqué comme réponse Alex Petrescu lundi 29 mars 2010 14:44