Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionImports System.IO Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ChargeListe() End Sub Private Sub ChargeListe() If My.Computer.FileSystem.FileExists(My.Application.Info.DirectoryPath & "\Maliste.txt") Then ListBox1.Items.Clear() Try Using sr As StreamReader = New StreamReader(My.Application.Info.DirectoryPath & "\Maliste.txt") Dim line As String Do line = sr.ReadLine() ListBox1.Items.Add(line) Loop Until line Is Nothing sr.Close() End Using Catch ex As Exception Console.WriteLine(ex.Message) End Try End If End Sub End Class
Imports System.IO Public Class Form1 Public stream As StreamReader Public writer As StreamWriter Public file As FileInfo Public pos1 As Integer Dim line As String Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Chargecombo() End Sub Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed Savecombo() MsgBox("Enregistrer dans le fichier 'Maliste'", MsgBoxStyle.Information, "Fermeture") End Sub Private Sub Chargecombo() ComboBox1.Items.Clear() Try ' on récupère le nom du fichier Dim file_name As String = (Application.StartupPath & "\Maliste.txt") Dim stream_reader As New IO.StreamReader(file_name) 'Lire le fichier ligne par ligne à la fois. line = stream_reader.ReadLine() Do While Not (line Is Nothing) 'Trim et assurez-vous que la ligne n'est pas vide. line = line.Trim() If line.Length > 0 Then ComboBox1.Items.Add(line) 'Obtenir la ligne suivante. line = stream_reader.ReadLine() Loop ComboBox1.SelectedIndex = 0 stream_reader.Close() Catch exc As Exception 'Rapport toutes les erreurs. MsgBox(exc.Message, MsgBoxStyle.Exclamation, "Erreur d'écriture") End Try End Sub Sub Savecombo() ComboBox1.Text = "" file = New FileInfo(Application.StartupPath & "\Maliste.txt") file.Attributes = FileAttributes.Normal writer = New StreamWriter(Application.StartupPath & "\Maliste.txt") If ComboBox1.Items.Count <> 0 Then For pos1 = 0 To ComboBox1.Items.Count - 1 writer.WriteLine(ComboBox1.Items(pos1)) Next End If writer.WriteLine(ComboBox1.Text) writer.Close() file.Attributes = FileAttributes.Normal End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If ComboBox1.Text = "" Then Exit Sub ComboBox1.Items.Add(ComboBox1.Text) MsgBox("Enregistré", MsgBoxStyle.Information, "Enregistrement") End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If ComboBox1.Text = "" Then Exit Sub If ComboBox1.SelectedIndex > -1 Then ComboBox1.Items.RemoveAt(ComboBox1.SelectedIndex) MsgBox("Supprimé ", MsgBoxStyle.Information, "Suppression") Else ComboBox1.Text = "" End If End Sub End Class
Private Sub myform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.TextBox2.Text = My.Settings.mytext End Sub Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged Me.TextBox2.Text = TextBox1.Text My.Settings.mytext = Me.TextBox2.Text My.Settings.Save() End Sub