' Copyright (C) 2025  P4pillon.org
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU Affero General Public License as
' published by the Free Software Foundation, either version 3 of the
' License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU Affero General Public License for more details.
'
' You should have received a copy of the GNU Affero General Public License
' along with this program.  If not, see <https://www.gnu.org/licenses/>.

Imports System.IO
Imports System.Collections
Imports System.Runtime.InteropServices.Marshal
Imports System.Runtime.InteropServices



Public Class Technique_MAIN
    
#Region "Lire Session"


    Public Sub LireSession_Out_VersFichier2(ByVal NuméroSession As UInt16, ByVal CheminFichier As String)

        Dim rep As String = Mid(CheminFichier, 1, InStrRev(CheminFichier, "\") - 1)
        If My.Computer.FileSystem.DirectoryExists(rep) = False Then
            My.Computer.FileSystem.CreateDirectory(rep)
        End If

        Using sw As New StreamWriter(CheminFichier)

            Dim IdGroupeSuivant As UInt16
            Dim OccurrenceGroupeSuivant As UInt16
            Dim IdGroupeCourant As UInt16 = 0
            Dim OccurrenceGroupeCourant As UInt16 = 0
            While SGD_LireGroupeSuivant(NuméroSession, IdGroupeCourant, OccurrenceGroupeCourant, IdGroupeSuivant, OccurrenceGroupeSuivant) = 0
                sw.WriteLine("Groupe:" + IdGroupeSuivant.ToString + "    Occurrence:" + OccurrenceGroupeSuivant.ToString)
                IdGroupeCourant = IdGroupeSuivant
                OccurrenceGroupeCourant = OccurrenceGroupeSuivant
                Dim i As UInt16 = 1


                While SGD_LireChampFichier(NuméroSession, IdGroupeCourant, OccurrenceGroupeCourant, i, "temp\temp") = 0
                    Using sr As New StreamReader("temp\temp")
                        sw.WriteLine("   Champ " + i.ToString + ":" + sr.ReadToEnd)
                    End Using
                    i = i + 1
                End While
            End While
        End Using
        Try
            My.Computer.FileSystem.DeleteFile("temp\temp")
        Catch ex As Exception
            MyMsg_Box.Presentation("Officine", "LireSession_Out_VersFichier:" + Chr(10) + ex.ToString, False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
        End Try

    End Sub

    Private Sub lire_session_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lire_session.Click
        If CLS_CheckBox.Checked = True Then
            RichTextBox1.Clear()
        End If

        Me.UseWaitCursor = True
        Dim IdGroupeSuivant As UInt16
        Dim OccurrenceGroupeSuivant As UInt16
        Dim numerosession As UInt16 = Val(TextBoxNumeroSession.Text)
        Dim IdGroupeCourant As UInt16 = 0
        Dim OccurrenceGroupeCourant As UInt16 = 0
        Dim CompteurChampsTotal As Integer = 0
        While SGD_LireGroupeSuivant(numerosession, IdGroupeCourant, OccurrenceGroupeCourant, IdGroupeSuivant, OccurrenceGroupeSuivant) = 0
            RichTextBox1.AppendText("Groupe:" + IdGroupeSuivant.ToString + "    Occurrence:" + OccurrenceGroupeSuivant.ToString + Chr(10))
            IdGroupeCourant = IdGroupeSuivant
            OccurrenceGroupeCourant = OccurrenceGroupeSuivant
            Dim i As UInt16 = 1
            Dim ValeurChamp As String = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
            Dim TailleMax As Integer = 80
            While SGD_LireChamp(numerosession, IdGroupeCourant, OccurrenceGroupeCourant, i, ValeurChamp, TailleMax) = 0
                CompteurChampsTotal = CompteurChampsTotal + 1
                RichTextBox1.AppendText("   Champ " + i.ToString + ":" + ValeurChamp + "  CompteurChampsTotal:" + CompteurChampsTotal.ToString)
                ValeurChamp = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                TailleMax = 80
                RichTextBox1.AppendText(Chr(10))
                i = i + 1
            End While
        End While
        Me.UseWaitCursor = False
    End Sub

    Private Sub CPS_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles CPS.LinkClicked
        TextBoxNumeroSession.Text = "1"
        lire_session.PerformClick()
    End Sub

    Private Sub Vitale_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles Vitale.LinkClicked
        TextBoxNumeroSession.Text = "2"
        lire_session.PerformClick()
    End Sub
#End Region

#Region "Lire champ Lire groupe"


    'LireGroupeSuivant
    Private Sub LireGroupeSuivant_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LireGroupeSuivant.Click
        Dim IdGroupeSuivant As UInt16
        Dim OccurrenceGroupeSuivant As UInt16
        Dim numerosession As UInt16 = Val(TextBoxNumeroSession.Text)
        Dim IdGroupeCourant As UInt16 = Val(TextBoxIdGroupe.Text)
        Dim OccurrenceGroupeCourant As UInt16 = Val(TextBoxOccurrenceGroupe.Text)
        RichTextBox1.AppendText((SGD_LireGroupeSuivant(numerosession, IdGroupeCourant, OccurrenceGroupeCourant, IdGroupeSuivant, OccurrenceGroupeSuivant) Mod 65536).ToString + Chr(10))
        RichTextBox1.AppendText("IdGroupeSuivant:" + IdGroupeSuivant.ToString + "    OccurrenceGroupeSuivant:" + OccurrenceGroupeSuivant.ToString + Chr(10))
    End Sub

    'LireChamp
    Private Sub LireChamp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LireChamp.Click
        ValeurChamp = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        RichTextBox1.AppendText(SGD_LireChamp(TextBoxNumeroSession.Text, TextBoxIdGroupe.Text, TextBoxOccurrenceGroupe.Text, TextBoxNumeroChamp.Text, ValeurChamp, 10000).ToString + Chr(10))
        RichTextBox1.AppendText(ValeurChamp + Chr(10))
    End Sub
    Private Sub Lire_Champ_Bin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Lire_Champ_Bin.Click
        Dim mystring As String = "123456789012345678901234567890123456789012345678901234567890"
        Dim taille As Integer = 1000
        RichTextBox1.AppendText(SGD_LireChampBin(2, 111, 1, 1, mystring, taille).ToString + Chr(10))
        mystring = Mid(mystring, 1, taille)
        RichTextBox1.AppendText(SGD_LireChampFichier(2, 111, 1, 1, "c:\test.txt").ToString + Chr(10))
        RichTextBox1.AppendText(mystring + "     taille " + taille.ToString + Chr(10))
        Dim mybytes() As Byte = System.Text.Encoding.UTF7.GetBytes(mystring)
        RichTextBox1.AppendText("mybytes.Length " + mybytes.Length.ToString + Chr(10))
        For i = 0 To mybytes.Length - 1
            RichTextBox1.AppendText(CInt(mybytes(i)).ToString + " " + Chr(10))
        Next

    End Sub
#End Region

#Region "InitTrace"


    'SIS_InitTrace
    Private Sub SIS_InitTraceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SIS_InitTraceButton.Click
        RichTextBox1.AppendText(SIS_InitTrace(TextBoxNomFichier.Text, ComboBoxModeOuverture.Text, ComboBoxModule.Text, ComboBoxNiveau.Text).ToString)
    End Sub



    Private Sub STS_InitTraceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles STS_InitTraceButton.Click
        RichTextBox1.AppendText(STS_InitTrace(TextBoxNomFichier.Text, ComboBoxModeOuverture.Text, ComboBoxModule.Text, ComboBoxNiveau.Text).ToString)
    End Sub

    Private Sub SGD_InitTrace_Button_Click(sender As Object, e As EventArgs) Handles SGD_InitTrace_Button.Click
        RichTextBox1.AppendText(SGD_InitTrace(TextBoxNomFichier.Text, ComboBoxModeOuverture.Text, ComboBoxModule.Text, ComboBoxNiveau.Text).ToString)
    End Sub

#End Region

#Region "AUTRES"


    Private Sub ButtonKillGalss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonKillGalss.Click
        killgalss()
    End Sub

    Private Sub Button_Histo_SMTP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Histo_SMTP.Click
        Histo_SMTP.Visible = True
    End Sub

    Private Sub Button_Histo_ARL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Histo_ARL.Click
        Histo_ARL.Visible = True
    End Sub
    Private Sub jeux_d_essai_Button_Click(sender As Object, e As EventArgs) Handles jeux_d_essai_Button.Click
        jeux_d_essai.Visible = True
    End Sub

    Private Sub jeux_d_essai_2_Button_Click(sender As Object, e As EventArgs) Handles jeux_d_essai_2_Button.Click
        Jeux_d_essai2.Visible = True
    End Sub
#End Region
  
    Private Sub CLS_Click(sender As Object, e As EventArgs) Handles CLS.Click
        RichTextBox1.Clear()
    End Sub

#Region "Time"


    Structure SYSTEMTIME
        Public wYear As Short
        Public wMonth As Short
        Public wDayOfWeek As Short
        Public wDay As Short
        Public wHour As Short
        Public wMinute As Short
        Public wSecond As Short
        Public wMilliseconds As Short
    End Structure

    <DllImport("kernel32.dll", setLastError:=True)> Public Shared Function SetSystemTime(ByRef theDateTime As SYSTEMTIME) As Boolean
    End Function
    Private Sub Set_time_Button_Click(sender As Object, e As EventArgs) Handles Set_time_Button.Click
        Dim thetimetoset As SYSTEMTIME
        thetimetoset.wYear = DateTimePicker1.Value.Year
        thetimetoset.wMonth = DateTimePicker1.Value.Month
        thetimetoset.wDayOfWeek = DateTimePicker1.Value.DayOfWeek
        thetimetoset.wDay = DateTimePicker1.Value.Day
        thetimetoset.wHour = DateTimePicker1.Value.Hour
        thetimetoset.wMinute = DateTimePicker1.Value.Minute
        thetimetoset.wSecond = DateTimePicker1.Value.Second
        thetimetoset.wMilliseconds = DateTimePicker1.Value.Millisecond
        If SetSystemTime(thetimetoset) = False Then
            MyMsg_Box.Presentation("Officine", "failed", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
        End If
    End Sub

#End Region

#Region "Carte Vitale"
#Region "INI File"
    'Retourne un booléen indiquant l'existance ou non d'une section
    Public Function ExisteSection(ByVal File As String, ByVal Section As String) As Boolean
        Dim PtrCh As IntPtr
        Dim Lng As Integer

        PtrCh = StringToHGlobalAnsi(New String(vbNullChar, 1024))
        Lng = GetPrivateProfileSection(Section, PtrCh, 1024, File)

        Return (Lng)
    End Function

    'Retourne une collection contenant l'ensemble des sections du fichier "File"
    Public Function GetAllSections(ByVal File As String) As Collection
        Dim PtrCh As IntPtr
        Dim Sections As Collection
        Dim I, Lng As Integer
        Dim Chaine, SChaine As String

        PtrCh = StringToHGlobalAnsi(New String(vbNullChar, 1024))
        Lng = GetPrivateProfileSectionNames(PtrCh, 1024, File)
        Chaine = PtrToStringAnsi(PtrCh, Lng)
        FreeHGlobal(PtrCh)

        Sections = New Collection
        SChaine = ""
        For I = 0 To Lng - 1
            If Chaine.Chars(I) = vbNullChar Then
                Sections.Add(SChaine)
                SChaine = ""
            Else
                SChaine = SChaine & Chaine.Chars(I)
            End If
        Next
        GetAllSections = Sections
        Sections = Nothing
    End Function

    'Retourne une collection contenant l'ensemble des clés de la section "Section" du fichier "File"
    Public Function GetSectionCles(ByVal File As String, ByVal Section As String) As Collection
        Dim PtrCh As IntPtr
        Dim Cles As Collection
        Dim I, Lng As Integer
        Dim Chaine, SChaine As String
        PtrCh = StringToHGlobalAnsi(New String(vbNullChar, 1024))
        Lng = GetPrivateProfileSection(Section, PtrCh, 1024, File)
        Chaine = PtrToStringAnsi(PtrCh, Lng)
        FreeHGlobal(PtrCh)

        Cles = New Collection
        SChaine = ""
        For I = 0 To Lng - 1
            If Chaine.Chars(I) = vbNullChar Then
                Cles.Add(SChaine)
                SChaine = ""
            Else
                SChaine = SChaine & Chaine.Chars(I)
            End If
        Next
        GetSectionCles = Cles
        Cles = Nothing
    End Function

    'Retourne la valeur de la clé "Cle" de la section "Section" du fichier "File"
    Public Function GetCle(ByVal File As String, ByVal Section As String, ByVal Cle As String) As String
        Dim PtrCh As IntPtr
        Dim Lng As Integer
        Dim Chaine As String

        PtrCh = StringToHGlobalAnsi(New String(vbNullChar, 1024))
        Lng = GetPrivateProfileString(Section, Cle, "", PtrCh, 255, File)
        Chaine = PtrToStringAnsi(PtrCh, Lng)
        FreeHGlobal(PtrCh)

        GetCle = Chaine
    End Function


    'Insère une section dans le fichier "File"
    Public Function SetSection(ByVal File As String, ByVal Section As String, ByVal Valeur As String) As Boolean
        SetSection = WritePrivateProfileSection(Section, Valeur, File)
    End Function

    'Insère la clé "Cle" dans la section "Section" du fichier "File"
    Public Function SetCle(ByVal File As String, ByVal Section As String, ByVal Cle As String, ByVal Valeur As String) As Boolean
        SetCle = WritePrivateProfileString(Section, Cle, Valeur, File)
    End Function


    'Efface toute les clés de la section "Section"
    Public Function DelSection(ByVal File As String, ByVal Section As String) As Boolean
        DelSection = WritePrivateProfileSection(Section, "", File)
    End Function

    'Efface la valeur de la clé "Cle" de la section "Section"
    Public Function DelCle(ByVal File As String, ByVal Section As String, ByVal Cle As String) As Boolean
        DelCle = WritePrivateProfileString(Section, Cle, "", File)
    End Function

    Private Declare Function GetPrivateProfileSection Lib "kernel32.dll" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedBuffer As IntPtr, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
    Private Declare Function GetPrivateProfileSectionNames Lib "kernel32.dll" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer As IntPtr, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
    Private Declare Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedBuffer As IntPtr, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
    Private Declare Function WritePrivateProfileSection Lib "kernel32.dll" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
    Private Declare Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
#End Region

    Private Sub affiche_carte_physique()
        If Label_Carte_Courante.Text = "" Then
            Label_Carte_Phys.Text = ""
        Else
            Label_Carte_Phys.Text = "carte physique: ??"
            Dim line, carte_phys, carte_virt As String
            Using sr As New StreamReader("C:\ProgramData\santesocial\fsv\1.40.9\adm\Phys-Virt.txt")
                line = sr.ReadLine

                Do
                    carte_phys = Mid(line, 1, InStr(line, ";") - 1)
                    line = Mid(line, InStr(line, ";") + 1)
                    carte_virt = Mid(line, 1, InStr(line, ";") - 1)
                    If carte_virt = Mid(Label_Carte_Courante.Text, 1, InStr(Label_Carte_Courante.Text, ".") - 1) Then
                        Label_Carte_Phys.Text = "carte physique: " + carte_phys
                        Exit Do
                    End If
                    line = sr.ReadLine
                Loop Until line Is Nothing Or line = ""
                sr.Close()
            End Using
        End If
    End Sub

    Private Sub CV_ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CV_ComboBox.SelectedIndexChanged
        SetCle(Config.SesamIni, "TESTSSV", "NomFichierVitale1", CV_ComboBox.Text)
        Label_Carte_Courante.Text = GetCle(Config.SesamIni, "TESTSSV", "NomFichierVitale1")
        affiche_carte_physique()
    End Sub

    Private Sub Technique_MAIN_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        COMPTOIR_MAIN.DP_Affiche_Msg()
    End Sub

    Private Sub Technique_MAIN_Load(sender As Object, e As EventArgs) Handles Me.Load

        CV_ComboBox.Items.Add("")
        Dim fichier As String
        fichier = Dir("C:\ProgramData\santesocial\fsv\1.40.9\adm\*.in")
        While fichier <> ""
            CV_ComboBox.Items.Add(fichier)
            fichier = Dir()
        End While
        Dim Ret As String = ""
        Label_Carte_Courante.Text = GetCle(Config.SesamIni, "TESTSSV", "NomFichierVitale1")
        affiche_carte_physique()
    End Sub
#End Region

#Region "TRACES"
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Fichiers_Traces_ListBox.Items.Clear()
        Dim fichier As String = Dir(Application.StartupPath + "\traces\")
        While fichier <> ""
            Fichiers_Traces_ListBox.Items.Add(fichier)
            fichier = Dir()

        End While

    End Sub







    Private Sub Fichiers_Traces_ListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Fichiers_Traces_ListBox.SelectedIndexChanged
        If CLS_CheckBox.Checked = True Then
            RichTextBox1.Clear()
        End If
        RichTextBox1.AppendText(Fichiers_Traces_ListBox.SelectedItem.ToString)
        Dim fichier As String = Application.StartupPath + "\traces\" + Fichiers_Traces_ListBox.SelectedItem.ToString

        If My.Computer.FileSystem.FileExists(fichier) = False Then
            RichTextBox1.AppendText("  FICHIER MANQUANT")
            Exit Sub
        End If
            Using sr As New StreamReader(fichier)
                Dim line As String = sr.ReadLine
            While Not line Is Nothing
                '''''
                If InStr(line, "Groupe 1110    (1)") = 1 Then
                    RichTextBox1.AppendText(Chr(10) + line)
                    line = sr.ReadLine
                    num_facture_TextBox.Text = (Mid(line, 5, 8))
                End If
                '''''
                RichTextBox1.AppendText(Chr(10) + line)
                line = sr.ReadLine
            End While
        End Using

      
    End Sub

    Private Sub Vidage_B2_Button_Click(sender As Object, e As EventArgs) Handles Vidage_B2_Button.Click
        If CLS_CheckBox.Checked = True Then
            RichTextBox1.Clear()
        End If

        Dim fichier, line As String
        Dim num_fact_string As String = Trim(num_facture_TextBox.Text)
        While num_fact_string.Length < 9
            num_fact_string = "0" + num_fact_string
        End While
        num_fact_string = "@" + num_fact_string + "@"
        fichier = Dir(Config.Repertoires_RepertoireFactures & "*" + num_fact_string + "*.fse")
        If fichier <> "" Then
            Using sr As New StreamReader(Config.Repertoires_RepertoireFactures & fichier, System.Text.Encoding.UTF8)
                line = sr.ReadLine

                Dim entête As String = Mid(line, 2, 32)
                While Not line Is Nothing


                    While InStr(line, "5" + entête) <> 1 And line.Length > 128
                        RichTextBox1.AppendText(Mid(line, 1, 33))
                        line = Mid(line, 34)
                        RichTextBox1.AppendText(Mid(line, 1, InStr(line, entête) - 2) + Chr(10))
                        line = Mid(line, InStr(line, entête) - 1)
                    End While
                    RichTextBox1.AppendText(Mid(line, 1, 128) + Chr(10))
                    If InStr(line, "5" + entête) <> 1 Then
                        line = sr.ReadLine
                    Else
                        line = Nothing
                    End If
                End While
            End Using
        End If
        fichier = Dir(Config.Repertoires_RepertoireFactures & "*" + num_fact_string + "*.dre")
        If fichier <> "" Then
            RichTextBox1.AppendText(Chr(10) + "DRE" + Chr(10))
            Using sr As New StreamReader(Config.Repertoires_RepertoireFactures & fichier, System.Text.Encoding.UTF8)
                line = sr.ReadLine

                Dim entête As String = Mid(line, 2, 32)
                While Not line Is Nothing


                    While InStr(line, "5" + entête) <> 1 And line.Length > 128
                        RichTextBox1.AppendText(Mid(line, 1, 33))
                        line = Mid(line, 34)
                        RichTextBox1.AppendText(Mid(line, 1, InStr(line, entête) - 2) + Chr(10))
                        line = Mid(line, InStr(line, entête) - 1)
                    End While
                    RichTextBox1.AppendText(Mid(line, 1, 128) + Chr(10))
                    If InStr(line, "5" + entête) <> 1 Then
                        line = sr.ReadLine
                    Else
                        line = Nothing
                    End If
                End While
            End Using
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Fichiers_Traces_ListBox.Items.Clear()
        Dim fichier As String = Dir(Application.StartupPath + "\traces\")
        While fichier <> ""
            My.Computer.FileSystem.DeleteFile(Application.StartupPath + "\traces\" + fichier)
            fichier = Dir()

        End While

    End Sub

    Private Sub RichTextBox1_SelectionChanged(sender As Object, e As EventArgs) Handles RichTextBox1.SelectionChanged
        Column_Label.Text = "C: " + (GetColumn()).ToString
    End Sub
    Public Function GetColumn() As Integer
        Dim Position As Integer = Me.RichTextBox1.SelectionStart
        Dim Line As Integer = Me.RichTextBox1.GetLineFromCharIndex(Position)
        Dim x As Integer = Line
        Dim y As Integer = Position
        If x = 0 Then
            Return Position + 1
        Else
            Do Until x = Line - 1
                x = Me.RichTextBox1.GetLineFromCharIndex(y)
                y -= 1
            Loop
            Return (Position - y - 1)
        End If
    End Function
#End Region

    Private Sub RAZ_Button_Click(sender As Object, e As EventArgs) Handles RAZ_Button.Click
        If UCase(InputBox("REMISE A ZERO DE LA BASE DE DONNEES ET SUPPRESSION DES FICHIERS!" + Chr(10) + "POUR CONTINUER, SAISIRE sesam")) <> "SESAM" Then
            Exit Sub
        Else
            RAZ.Visible = True
        End If
    End Sub

    Private Sub jeux_d_essai_CasStandard_Button_Click(sender As Object, e As EventArgs) Handles jeux_d_essai_CasStandard_Button.Click
        jeux_d_essai_CAS_STANDARD.Visible = True
    End Sub

    Private Sub jeux_d_essai_TroncCmmun_Button_Click(sender As Object, e As EventArgs) Handles jeux_d_essai_TroncCmmun_Button.Click
        jeux_d_essai_TRONC_COMMUN.Visible = True
    End Sub
End Class