jeudi 24 mai 2018

VB.NET Read 2 checkboxes status from file

I am trying to recover the status of 2 checkboxes. This 2 checkboxes i made them to work as radiobuttun: While one is checked, the another one uncheck.

I have an external file for the configuration of the program and i want that evrytime that I exit from the program, everything be saved in this file.

For do it I use this code:

   Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing

    Dim thefile As String = Application.StartupPath & "\SafetyBox.cfg"
    Dim lines() As String = System.IO.File.ReadAllLines(thefile)

    lines(1) = "Language_file=" & ComboBox1.Text
    If CheckBox1.Checked = True Then
        lines(2) = "Status1=" & "1"
    Else
        lines(2) = "Status1=" & "0"
    End If
    If CheckBox2.Checked = True Then
        lines(3) = "Status2=" & "1"
    Else
        lines(3) = "Status2=" & "0"
    End If
    System.IO.File.WriteAllLines(thefile, lines)

End Sub`

And this part working great. Status1 should be the status of checkbox1, while status2 is the status of checkbox2.

The code that is not working is:

 Dim path As String = Application.StartupPath & "\SafetyBox.cfg"
    If File.Exists(path) Then

        Using sr As StreamReader = New StreamReader(path)

            Dim linenew As String = sr.ReadLine()
            If linenew.Contains("\") Then
                TextBox1.Text = linenew



            Else
                MsgBox("Configura il programma da usare")
            End If

            Dim lineN As String = sr.ReadLine()
            If lineN.Contains("Language_file=") Then
                ComboBox1.Text = lineN.Split("=").Last()
            End If
            If lineN.Contains("Status1=1") Then
                CheckBox1.Checked = True
                CheckBox2.Checked = False

            ElseIf lineN.contains("Status1=0") Then
                CheckBox1.Checked = False
                CheckBox2.Checked = True

            End If



            If lineN.Contains("Status2=1") Then
                CheckBox1.Checked = False
                CheckBox2.Checked = True

            ElseIf lineN.Contains("Status2=0") Then
                CheckBox1.Checked = True
                CheckBox2.Checked = False
            End If
                sr.ReadToEnd()
            sr.Close()
        End Using

Can yOu let me understnd where is my mistake? Why when in the .cfg file is wrote correctly Status1=0 and Status2=1, when loading the program i always see checkbox1 checkd and not checkbox2?

Thanks




Aucun commentaire:

Enregistrer un commentaire