Imports System.IO
Public Class Form1
Private FilePath As String
Private codehex(0 To 7) As Byte
Private couleurhex(0 To 1) As Byte
Private Sub OuvrirToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OuvrirToolStripMenuItem.Click
Dim open As New OpenFileDialog
open.Filter = "FICHIER PS3 |SYS-DATA"
If open.ShowDialog = Windows.Forms.DialogResult.OK Then
FilePath = open.FileName
ReadFile(FilePath)
Else
MessageBox.Show("Fichier non chargé", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
Private Sub ReadFile(FilePath As String)
ListView1.Items.Clear()
Dim input As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim reader As New BinaryReader(input)
reader.BaseStream.Position = 16
For i = 0 To 16
Dim position As Long = reader.BaseStream.Position - 10
Dim code As String = BitConverter.ToString(reader.ReadBytes(8)).Replace("-", "")
Dim couleur As String = BitConverter.ToString(reader.ReadBytes(2)).Replace("-", "")
reader.BaseStream.Position += 14
Dim lvi As New ListViewItem("&H" & position.ToString("X4"))
lvi.SubItems.AddRange(New String() {code, couleur})
ListView1.Items.Add(lvi)
Next
reader.Close()
input.Close()
TextBox1.Text = String.Empty
TextBox1.Refresh()
End Sub
Private Sub EnregistrerToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EnregistrerToolStripMenuItem.Click
Dim output As New FileStream(FilePath, FileMode.Open, FileAccess.Write, FileShare.Write)
Dim writer As New BinaryWriter(output)
writer.BaseStream.Position = 16
For Each item As ListViewItem In ListView1.Items
Dim position As Long = writer.BaseStream.Position - 10
Dim code As String = item.SubItems.Item(1).Text ' chaine code en héxadécimal
Dim couleur As String = item.SubItems.Item(2).Text ' chaine couleur en héxadécimal
codehex = converthex(code, codehex) ' on convertit la chaine héxa-ASCII en un tableau de 8 octets
couleurhex = converthex(couleur, couleurhex) ' on convertit la chaine héxa-ASCII en un tableau de 2 octets
writer.Write(codehex) ' on écrit les 8 octets du code la voiture
writer.Write(couleurhex) ' on écrit les 2 octets de la couleur de la voiture
writer.BaseStream.Position += 14
Next
writer.Close()
output.Close()
MessageBox.Show("Fichier sauvegardé", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Function converthex(chaine As String, octets() As Byte) As Byte()
For i = 0 To (chaine.Length / 2) - 1
octets(i) = Convert.ToByte(chaine.Substring(i * 2, 2), 16)
Next
converthex = octets
End Function
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If ListView1.SelectedItems.Count > 0 Then
TextBox1.Text = TextBox1.Text.ToUpper
Dim couleurhex As String = TextBox1.Text
If couleurhex.Length <> 4 Or String.IsNullOrWhiteSpace(couleurhex) = True Then
MessageBox.Show("La couleur saisie doit comportée 4 caractères héxadécimaux", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
TextBox1.Text = String.Empty
TextBox1.Refresh()
Exit Sub
End If
For i = 0 To 3
Dim caractere As String = couleurhex.Substring(i, 1)
If (caractere >= "0" And caractere <= "9") Or (caractere >= "A" And caractere <= "F") Then
For Each item As ListViewItem In ListView1.SelectedItems
item.SubItems(2).Text = couleurhex
Next
Else
MessageBox.Show("Les caractères ne sont pas tous héxadécimaux (de 0 à 9 et de A à F)", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
TextBox1.Text = String.Empty
TextBox1.Refresh()
End If
Next
Else
MessageBox.Show("Aucune sélection faite dans la liste", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
TextBox1.Text = String.Empty
TextBox1.Refresh()
End If
End Sub
Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged
If ListView1.SelectedItems.Count > 0 Then
For Each item As ListViewItem In ListView1.SelectedItems
TextBox1.Text = ListView1.SelectedItems(0).SubItems(2).Text.ToUpper
Next
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter() Private Sub ReadFile(ByVal FilePath As String, ByVal list As ListView) 'Lire la liste If File.Exists(FilePath) Then Using fs As New System.IO.FileStream(FilePath, IO.FileMode.Open) List.Items.AddRange(bf.Deserialize(fs).ToArray(GetType(ListViewItem))) End Using End If End Sub Private Sub WriteFile(ByVal FilePath As String, ByVal list As ListView) ' Sauver la liste Using fs As New System.IO.FileStream(FilePath, IO.FileMode.Create) bf.Serialize(fs, New ArrayList(list.Items)) End Using End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 'adapter listview ListVpath.View = View.Details ListVpath.Columns.Add("Noms", 150, HorizontalAlignment.Left) ListVpath.Columns.Add("Chemin", 350, HorizontalAlignment.Left) ListVpath.Items.Clear() ReadFile("Chemin fichier.txt", ListVpath)'adapter listview End Sub Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click WriteFile("Chemin fichier.txt", ListVpath)'adapter listview End Sub
item.SubItems(2).Text
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPrivate Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If ListView1.SelectedItems.Count > 0 Then
TextBox1.Text = TextBox1.Text.ToUpper
Dim couleurhex As String = TextBox1.Text
If couleurhex.Length <> 4 Or String.IsNullOrWhiteSpace(couleurhex) = True Then
MessageBox.Show("La couleur saisie doit comportée 4 caractères héxadécimaux", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
TextBox1.Text = String.Empty
TextBox1.Refresh()
Exit Sub
End If
Dim ok As Integer = 0
For i = 0 To 3
Dim caractere As String = couleurhex.Substring(i, 1)
If (caractere >= "0" And caractere <= "9") Or (caractere >= "A" And caractere <= "F") Then
ok += 1
Else
MessageBox.Show("Les caractères ne sont pas tous héxadécimaux (de 0 à 9 et de A à F)", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
TextBox1.Text = String.Empty
TextBox1.Refresh()
End If
Next
If ok = 4 Then
For Each item As ListViewItem In ListView1.SelectedItems
item.SubItems(2).Text = couleurhex
Next
End If
Else
MessageBox.Show("Aucune sélection faite dans la liste", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
TextBox1.Text = String.Empty
TextBox1.Refresh()
End If
End Sub
For i = 0 To 16
For i = 0 To 16... et ta question ?