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
| Option Explicit
'contiendra tout les non de repertoire connues
'ce trouvant dans le fichier "MesRepertoires.txt"
Dim MeLstRep As New Collection
Private Sub Form_Load()
Dim CheminNomFichier As String
Dim NumFich As Integer
Dim T As Integer
Dim MsgTxt As String 'recuperation global des repertoire depuis le fichier .txt
Dim TblMsgTxt() As String 'tableau intermediaire
'contenu type du fichier adapté à mon cas perso
'C:\essais VB6,C:\Dossier pour images divers,A:\FormulsXls,repertoire4,C:\WINNT
CheminNomFichier = "C:/MesRepertoires.txt" 'a adapter pour ton propre chemin
NumFich = FreeFile
'recuperation global
Open CheminNomFichier For Input As #NumFich
MsgTxt = Input(FileLen(CheminNomFichier), NumFich)
Close #NumFich
If MsgTxt = "" Then
MsgBox "le fichier est vide", vbCritical
Unload Me
End If
'recuperation detailée
TblMsgTxt = Split(MsgTxt, ",", , vbTextCompare)
'remplissage de la collection, boucle OUI, mais une seul fois
For T = 0 To UBound(TblMsgTxt)
MeLstRep.Add T, TblMsgTxt(T)
Next T
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
'exemple d'utilisation
If KeyAscii = 13 Then
If VerifList(MeLstRep, Text1.Text) Then
MsgBox Text1.Text & " existe bien dans ma liste, index: " & MeLstRep.Item(Text1.Text)
Else
MsgBox Text1.Text & " n'existe pas dans ma liste"
End If
End If
End Sub
Public Function VerifList(Lst As Collection, RechRep As String) As Boolean
'recherche sans bouclage de la liste, mais par le .Key de la collection
On Error Resume Next
VerifList = Not IsError(Lst.Item(RechRep))
If Err Then Err.Clear
End Function |
Partager