Création d'un fichier config a l'aide de tableaux

cs_Yves_P Messages postés 2 Date d'inscription dimanche 5 mars 2006 Statut Membre Dernière intervention 3 janvier 2010 - 3 janv. 2010 à 12:58
cs_Yves_P Messages postés 2 Date d'inscription dimanche 5 mars 2006 Statut Membre Dernière intervention 3 janvier 2010 - 3 janv. 2010 à 19:08
Bonjour à tous et à toutes...

Voila ma question, je developpe une petite application ou des boutons portent le nom de variables pour permettre à l'utilisateur de donner à ces boutons des noms de son choix.

Pour cela j'ai créé des TextBox comportant le nom de ces variables.
Chaque TextBox comporte un Label, et si le TextBox est vide Il prend comme valeur le Label.text

Private Sub PanelMnemo_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PanelMnemo.Paint
If TextBox_Bex.Text = "" Then
TextBox_Bex.Text = Label_Bex.Text
End If
If TextBox_BalBlanc1.Text = "" Then
TextBox_BalBlanc1.Text = Label_BalBlanc1.Text
End If
If TextBox_BalBlanc2.Text = "" Then
TextBox_BalBlanc2.Text = Label_BalBlanc2.Text
End If
If TextBox_BalNoir.Text = "" Then
TextBox_BalNoir.Text = Label_BalNoir.Text
End If
If TextBox_BalMire.Text = "" Then
TextBox_BalMire.Text = Label_MireBarre.Text
End If
If TextBox_EntGr1.Text = "" Then
TextBox_EntGr1.Text = Label_Entree1.Text
End If
If TextBox_EntGr2.Text = "" Then
TextBox_EntGr2.Text = Label_Entree2.Text
End If
If TextBox_EntGr3.Text = "" Then
TextBox_EntGr3.Text = Label_Entree3.Text
End If
If TextBox_EntGr4.Text = "" Then
TextBox_EntGr4.Text = Label_Entree4.Text
End If
If TextBox_EntGr5.Text = "" Then
TextBox_EntGr5.Text = Label_Entree5.Text
End If
If TextBox_EntGr6.Text = "" Then
TextBox_EntGr6.Text = Label_Entree6.Text
End If
If TextBox_EntGr7.Text = "" Then
TextBox_EntGr7.Text = Label_Entree7.Text
End If
If TextBox_EntGr8.Text = "" Then
TextBox_EntGr8.Text = Label_Entree8.Text
End If
If TextBox_Mem1.Text = "" Then
TextBox_Mem1.Text = Label_Mem1.Text
End If
If TextBox_Mem2.Text = "" Then
TextBox_Mem2.Text = Label_Mem2.Text
End If
If TextBox_Mem3.Text = "" Then
TextBox_Mem3.Text = Label_Mem3.Text
End If
If TextBox_Mem4.Text = "" Then
TextBox_Mem4.Text = Label_Mem4.Text
End If
If TextBox_Mem5.Text = "" Then
TextBox_Mem5.Text = Label_Mem5.Text
End If
If TextBox_Mem6.Text = "" Then
TextBox_Mem6.Text = Label_Mem6.Text
End If
If TextBox_Mem7.Text = "" Then
TextBox_Mem7.Text = Label_Mem7.Text
End If
If TextBox_Mem8.Text = "" Then
TextBox_Mem8.Text = Label_Mem8.Text
End If
If TextBox_Sortie1.Text = "" Then
TextBox_Sortie1.Text = Label_Sortie1.Text
End If
If TextBox_Sortie2.Text = "" Then
TextBox_Sortie2.Text = Label_Sortie2.Text
End If
If TextBox_Sortie3.Text = "" Then
TextBox_Sortie3.Text = Label_Sortie3.Text
End If
If TextBox_Sortie4.Text = "" Then
TextBox_Sortie4.Text = Label_Sortie4.Text
End If
If TextBox_Sortie5.Text = "" Then
TextBox_Sortie5.Text = Label_Sortie5.Text
End If
If TextBox_Sortie6.Text = "" Then
TextBox_Sortie6.Text = Label_Sortie6.Text
End If
If TextBox_Sortie7.Text = "" Then
TextBox_Sortie7.Text = Label_Sortie7.Text
End If
If TextBox_Sortie8.Text = "" Then
TextBox_Sortie8.Text = Label_Sortie8.Text
End If
End Sub

J'aurais du le faire avec un ableau et une boucle mais je n'y suis pas parvenu. Je m'y repencherais plus tard.

Pour ne pas avoir à retapper le nom de chque bouton à chaque lancement (pas tres cool) J'ai trouvé sur un forum une méthode pour écrir dans un fichier Texte des valeurs Puis une autre méthode pour lire le contenu de ce fichier texte.


'Module permetant de creer un fichier config
Module RecConfig

'Sub Permetant d'ecrire dans un fichier
Public Sub Record()
Dim configName(0 To 11) As String
configName(1) = Form1.TextBox_Bex.Text
My.Computer.FileSystem.WriteAllText("C:\testfile.txt", "," & configName(1), True)
End Sub

'Sub permetant de lire le contenu d'un fichier
Public Sub Read()
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\testfile.txt")
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
MsgBox(currentField)
Next
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & _
"Text non valide, Il est ignoré.")
End Try
End While
End Using
End Sub

End Module




Tout cela fonctionne tres bien mais actuellement comme vous pouvez le voir la sortie de mon tableau affiche un MsgBox car je n'ai pas réussi à le diriger vers mon fichier Texte.


Donc

Question 1 comment écrire dans mon tableau ? (Mon code devrait marcher mais non...)

Question 2 Comment relire ce tableau et ecrir ces valeur dans un autre tableau .


Qiestion 3 Comment Effacer le contenu de mon Fichier Text avant chaque ecriture pour le reinitaliser.



Merci de vos reponses.


Yves

1 réponse

cs_Yves_P Messages postés 2 Date d'inscription dimanche 5 mars 2006 Statut Membre Dernière intervention 3 janvier 2010
3 janv. 2010 à 19:08
Bonsoir,
J'ai trouvé la solution a une des questions posée.

Public Sub Record()
Dim configName(0 To 29) As String
configName(1) = Form1.TextBox_Bex.Text
configName(2) = Form1.TextBox_BalBlanc1.Text
configName(3) = Form1.TextBox_BalBlanc2.Text
configName(4) = Form1.TextBox_BalNoir.Text
configName(5) = Form1.TextBox_BalMire.Text
configName(6) = Form1.TextBox_Sortie1.Text
configName(7) = Form1.TextBox_Sortie2.Text
configName(8) = Form1.TextBox_Sortie3.Text
configName(9) = Form1.TextBox_Sortie4.Text
configName(10) = Form1.TextBox_Sortie5.Text
configName(11) = Form1.TextBox_Sortie6.Text
configName(12) = Form1.TextBox_Sortie7.Text
configName(13) = Form1.TextBox_Sortie8.Text
configName(14) = Form1.TextBox_EntGr1.Text
configName(15) = Form1.TextBox_EntGr2.Text
configName(16) = Form1.TextBox_EntGr3.Text
configName(17) = Form1.TextBox_EntGr4.Text
configName(18) = Form1.TextBox_EntGr5.Text
configName(19) = Form1.TextBox_EntGr6.Text
configName(20) = Form1.TextBox_EntGr7.Text
configName(21) = Form1.TextBox_EntGr8.Text
configName(22) = Form1.TextBox_Mem1.Text
configName(23) = Form1.TextBox_Mem2.Text
configName(24) = Form1.TextBox_Mem3.Text
configName(25) = Form1.TextBox_Mem4.Text
configName(26) = Form1.TextBox_Mem5.Text
configName(27) = Form1.TextBox_Mem6.Text
configName(28) = Form1.TextBox_Mem7.Text
configName(29) = Form1.TextBox_Mem8.Text
Dim i As Integer = 0
My.Computer.FileSystem.WriteAllText("D:\Projets_de_Yves\Projets tests\AlexTest\Config.yjp", "", False)
For i = 0 To 29
My.Computer.FileSystem.WriteAllText("D:\Projets_de_Yves\Projets tests\AlexTest\Config.yjp", configName(i) & ",", True)
Next


End Sub


Je mets a false la boléne de fin d'instruction avant ma boucle et j'efface le contenu du fichier.
Puis je passe cette Boléne à True au moment d'ecrire ma boucle dans le fichier.


Tout ne marche pas encore parfaitement mais cela avance...


Les autres questions retent bien sur d'actualité.

@+ Yves
0
Rejoignez-nous