Home All Groups Group Topic Archive Search About

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

Author
22 Jun 2006 3:04 PM
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:)

Author
22 Jun 2006 4:37 PM
Ahmed
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


auror***@gmail.com wrote:
Show quoteHide quote
> 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:)
Author
22 Jun 2006 11:15 PM
Herfried K. Wagner [MVP]
"Ahmed" <ahmed1***@gmail.com> schrieb:
>        If s.Contains("True") Then
>            yourCheckbox.value = True
>        Else
>            yourcheckbox.value = False
>        End If

.... can be simplified as...

\\\
YourCheckBox.Value = s.Contains("True")
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
23 Jun 2006 4:34 PM
aurora10
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 ?
Author
23 Jun 2006 4:54 PM
Ahmed
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
auror***@gmail.com wrote:
Show quoteHide quote
> 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 ?
Author
24 Jun 2006 1:36 PM
aurora10
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
Author
24 Jun 2006 3:06 PM
aurora10
I found the solution!
should have said

s=f.Readline()

Thanks
Author
24 Jun 2006 5:43 PM
Ahmed
Trim will only take the extra spaces if any after and before the word.

I am glad that it worked for you.

Cheers,

Ahmed
auror***@gmail.com wrote:
Show quoteHide quote
> I found the solution!
> should have said
>
> s=f.Readline()
>
> Thanks