How to assign a state to checkbox in Visual Basic.net ?

  • Thread starter Thread starter aurora10
  • Start date Start date
A

aurora10

Hi,

I have textfile with True or Flase in it and I need to read this file
so that the value is assigned to a checkbox on the form.

How can I do that ?

Thanks:)
 
Hi,

Assuming that theTrue or False is at the first line of the text file:

Dim f As StreamReader
Dim s As String
f = File.OpenText("C:\yourFilePath\Filename.txt")
s = f.ReadLine.Trim
If s.Contains("True") Then
yourCheckbox.value = True
Else
yourcheckbox.value = False
End If


Of course your need to do more validation and exception handeling, to
make sure the file exists.

Ahmed
 
Ahmed said:
If s.Contains("True") Then
yourCheckbox.value = True
Else
yourcheckbox.value = False
End If

.... can be simplified as...

\\\
YourCheckBox.Value = s.Contains("True")
///
 
Thank you. That works for one checkbox only. But when I say:

While s <> nothing

if s="Ttrue" then checkbox1.checked=1

if s="Ttrue" then checkbox2.checked=1

etc

End while

Then it hangs.

How can I do this with more than one checkbox ?
 
Well, it hangs because the program is looping for ever. Why do you need
the while s<> nothing?

Do you have only one True in the file? I am assuming your file is
formated as such:

True
True
False
True
..
..
..

Dim f As StreamReader
Dim s As String
dim counter as integer = 1
f = File.OpenText("C:\yourFilePath\Filename.txt")
s = f.ReadLine.Trim

While Not s Is nothing
select case counter
case 1
yourCheckbox1.value = s.Contains("True")
case 2
yourCheckbox2.value = s.Contains("True")
case 3
yourCheckbox3.value = s.Contains("True")
end select
s = f.ReadLine.Trim
counter +=1
end while


I didn't test the code.

I hope that help.

Ahmed
 
Yea, this time it works as it should

Only now it throws Null Refference Exeption

-------------------------------------------------------------------------------------------------------------------------

......................................

While Not s Is Nothing

Select Case counter
Case 1
CheckBox1.Checked = s.Contains("True")
Case 2
CheckBox2.Checked = s.Contains("True")
Case 3
CheckBox3.Checked = s.Contains("True")
Case Else
s = "1"

End Select

s = f.ReadLine.Trim - < Hier is null refference exeption
counter += 1

End While

f.Close()


--------------------------------------------------------------------------------

It's like reffered object does not exist anymore :(

Any idea how to fix this ?

Thanks in advance
 
Trim will only take the extra spaces if any after and before the word.

I am glad that it worked for you.

Cheers,

Ahmed
 
Back
Top