Reputation: 9
I am a newbie and I am facing some problem with the 'if' and 'else' function of visual basic. I have added a button labeled as Save, its code is as given below:
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If CheckBox1.Checked = True Then
Dim savedata7 As New System.IO.StreamWriter("dat0C.bin")
savedata7.WriteLine("checked")
savedata7.Close()
End If
If CheckBox1.Checked = False Then
Dim savedata7 As New System.IO.StreamWriter("dat0C.bin")
savedata7.WriteLine("unchecked")
savedata7.Close()
End If
If CheckBox2.Checked = True Then
Dim savedata8 As New System.IO.StreamWriter("dat1C.bin")
savedata8.WriteLine("checked")
savedata8.Close()
End If
If CheckBox2.Checked = False Then
Dim savedata8 As New System.IO.StreamWriter("dat1C.bin")
savedata8.WriteLine("unchecked")
savedata8.Close()
End If
End Sub
And another button labeled Load, its code is given below:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim dataloader As New System.IO.StreamReader("dat0C.bin")
Label17.Text = dataloader.ReadToEnd
dataloader.Close()
Catch ex As Exception
End Try
Try
Dim dataloader As New System.IO.StreamReader("dat1C.bin")
label10.Text = dataloader.ReadToEnd
dataloader.Close()
Catch ex As Exception
End Try
If Label17.Text = "checked" Then
CheckBox1.Checked = True
Else
CheckBox1.Checked = False
End If
If Label10.Text = "checked" Then
CheckBox2.Checked = True
Else
CheckBox1.Checked = False
End If
End Sub
The Save button(Button5) is working perfectly fine, but the Load button(Button1) is not working as it should, on clicking Button1 the files 'dat0C.bin' and 'dat1c.bin' are loaded in 'Label17' and 'Label10' respectively but it is not working(i.e. it is not Checking the CheckBoxes1 or 2) from
If Label17.Text = "checked" Then
CheckBox1.Checked = True
Else
CheckBox1.Checked = False
End If
If Label10.Text = "checked" Then
CheckBox2.Checked = True
Else
CheckBox1.Checked = False
End If
Please correct me, If I'm doing anything wrong in it.
Upvotes: 0
Views: 663
Reputation: 216263
This happens because in the reading code you use ReadToEnd instead of ReadLine.
This method reads back the newline character added by WriteLine and doesn't strip it away as ReadLine
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Using dataloader = New System.IO.StreamReader("dat0C.bin")
Label17.Text = dataloader.ReadLine()
End Using
Catch ex As Exception
MessageBox.Show("Exception=" & ex.Message)
End Try
....
A part from this you really should put the creation of disposable objects like the reader and the write inside a using statement to be sure that these objects are properly closed and disposed ALSO in case of exceptions.
Said that, your code could be simplified a lot if you use the static methods of the File class
WRITING
File.WriteAllText("dat0C.bin", If(CheckBox1.Checked, "checked", "unchecked"))
File.WriteAllText("dat1C.bin", If(CheckBox2.Checked, "checked", "unchecked"))
READING
Label17.Text = File.ReadAllText("dat0C.bin")
Label10.Text = File.ReadAllText("dat1C.bin")
CheckBox1.Checked = (Label17.Text = "checked")
CheckBox2.Checked = (Label10.Text = "checked")
Upvotes: 1
Reputation: 136074
Asside from not naming your controls, and not disposing properly of your StreamReader
or StreamWriter
instances the reason this is not working correctly is because when you write your file you call WriteLine
- which appends a carriage return and line feed onto the end of what you've written.
You then read the file, and put the content in a label, which itself will render the CR/LF albeit not visible to you. You then compare to the Text
of this label but without the CR/LF - so that comparison evaluates false.
Upvotes: 1