Results 1 to 17 of 17

Thread: Could this be done?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    24

    Could this be done?

    Everything is written in codes:

    vb Code:
    1. 'CHECK IF "sIntroSequence" IS AVAILABLE IN TEXT FILE.
    2.         Dim checkthevalue As String = My.Computer.FileSystem.ReadAllText("c:\test.txt")
    3.         If checkthevalue.Contains("sIntroSequence") Then
    4.  
    5.             '"INTRO SEQUENCE" STRING IS AVAILABLE.
    6.             'CHECK WHAT  COMES AFTER EQUALS SIGN.
    7.             For Each k As String In IO.File.ReadAllLines("c:\test.txt")
    8.                 If k.Contains("sIntroSequence=BGS_LOGO.BIK") Then
    9.             'IF IT IS "sIntroSequence=BGS_LOGO.BIK" THEN CHECKBOX18.CHECKED=TRUE.
    10.                     Form2.CheckBox18.Checked = True
    11.                 Else
    12.             'OTHERWISE SET CHECKBOX18.CHECKED=FALSE.
    13.                     Form2.CheckBox18.Checked = False
    14.                 End If
    15.             Next
    16.  
    17.         Else
    18.             '"INTRO SEQUENCE" STRING IS NOT AVAILABLE IN THE TEXT.
    19.             'SO CREATE IT...
    20.             Dim lines() As String = System.IO.File.ReadAllLines("c:\test.txt")
    21.             lines(10) &= vbNewLine & "sIntroSequence=" 'WRITE IT TO LINE10
    22.             System.IO.File.WriteAllLines("c:\test.txt")
    23.         End If

    I can't seem to get the checkbox checked according to presence of a particular string in a text file. It looks like it should work but it doesn't. I don't know what i'm doing wrong.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Could this be done?

    Is this some kind of INI file for configurations ? If so, there are cleaner approaches to it like XML or the GetPrivateProfileString family of APIs.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    24

    Re: Could this be done?

    Quote Originally Posted by Niya View Post
    Is this some kind of INI file for configurations ? If so, there are cleaner approaches to it like XML or the GetPrivateProfileString family of APIs.
    Yeah you do have a valid point but I prefer this way.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Could this be done?

    You are looking for
    If k.Contains("sIntroSequence=BGS_LOGO.BIK") Then
    but you are writing
    lines(10) &= vbNewLine & "sIntroSequence=" 'WRITE IT TO LINE10
    What is actually in the file?
    Have you set a break point and stepped through your code to see what is happening?
    If not then these are the first things you should do.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    24

    Re: Could this be done?

    Quote Originally Posted by DataMiser View Post
    You are looking for


    but you are writing


    What is actually in the file?
    Have you set a break point and stepped through your code to see what is happening?
    If not then these are the first things you should do.
    Firstly my program checks if the string was created. If it is created then it reads whether it is "sIntroSequence=BGS_LOGO.BIK" which represents checked status of checkbox18 or "sIntroSequence=" which represents unchecked status of checkbox18.

    If no such things is found in text file then it writes default string "sIntroSequence=" (unchecked status).

    On the form I have a button that writes "sIntroSequence=BGS_LOGO.BIK" or ""sIntroSequence=" according to checkbox18's checked status.

    So it makes sense.

  6. #6
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: Could this be done?

    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Could this be done?

    Quote Originally Posted by reinhold View Post
    Firstly my program checks if the string was created. If it is created then it reads whether it is "sIntroSequence=BGS_LOGO.BIK" which represents checked status of checkbox18 or "sIntroSequence=" which represents unchecked status of checkbox18.

    If no such things is found in text file then it writes default string "sIntroSequence=" (unchecked status).

    On the form I have a button that writes "sIntroSequence=BGS_LOGO.BIK" or ""sIntroSequence=" according to checkbox18's checked status.

    So it makes sense.
    Yet you did not answer the question as to what is actually in the file nor the one as to if you have tried to step through the code.

  8. #8
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: Could this be done?

    As I see it, the OP's problem was that he couldn't set the Checkbox's state to True or False, he didn't mention anything about problems writing to his file.

    According to the link in my previous post, you should use the Checkstate property (Gets or sets the state of the CheckBox) instead of the Checked property (Gets or set a value indicating whether the CheckBox is in the checked state).
    Last edited by Arve K.; May 21st, 2012 at 07:31 PM.
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Could this be done?

    If you look you will see that the code only sets the checked state if it finds a specific value in the file so my question is totally revelant to the issue at hand. We do not know what is in the file and the OP has not told us. We can see that the code we have been shown does not write the expected string to the file and we can also see that the code shoudl work if the string is there so logically one should check the file and see if it is actually there. If it is there then one should set a break point and step through the code.

    btw
    Code:
    CheckBox1.Checked = True
    May not be the reccommended way but it works just fine so I would not think that is the issue.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    24

    Re: Could this be done?

    No people, no. I have no problems in writing to the file. I just can't find a way to read whether the string after "=" is "BGS_LOGO.BIK" or "".

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Could this be done?

    Once again have you tried stepping through the code at runtime to see what is happening?
    Have you looked at the file to confirm the data you are looking for is there? Note it must be exact, your test is case sensitive.

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Could this be done?

    Two things:

    1) Stepping through the code is the first and best thing you can do to diagnose this issue. We can't do it for you, and we can't be certain what you will find when you do it. If you are not familiar with this, then ask, as it is the thing you must do, and is the most powerful debugging tool in existence.

    2) I notice that you use form2. That almost certainly means that you are working with the default instance in that code. That leaves open the possibility that you are not displaying the default instance, and therefore your code will appear not to work because you are changing the state of a form that is not visible, while looking at a different instance.
    My usual boring signature: Nothing

  13. #13

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    24

    Re: Could this be done?

    After re-checking my code from beginning to end, I think now I know the source of this problem.

    As i stated before, upon clicking save button, my program checks the state of a checkbox, finds "sIntroSequence=" in a text file, then writes "BGS_LOGO.BIK" to the end of it or remove any text there (if present) according to checked status of a checkbox.

    When the program loads, it again checks the same text file. If the "BGS_LOGO.BIK" is present after "sIntroSequence=", it marks the checkbox as "checked". If nothing comes after "sIntroSequence=" then checkbox remains unchecked.

    Well at least that's what i want it to do. The problem is,

    The regex identifiers i'm using are causing problems. I cannot write "BGS_LOGO.BIK" if there are no word/numbers after equals sign. These identifiers require that there should be some text,numbers,characters after "sIntroSequence=" in order to write "BGS_LOGO.BIK" there.

    THE CODE THAT SAVES:

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim lines() As String = IO.File.ReadAllLines("C:\test.txt")
    3.         Dim foundIndex As Integer = Array.FindIndex(lines, Function(l) l.StartsWith("sIntroSequence"))
    4.         Select Case Form2.CheckBox18.Checked
    5.             Case True
    6.  
    7.                 lines(foundIndex) = Regex.Replace(lines(foundIndex), "(?<=" & "sIntroSequence" & "=)\d+", "BGS_LOGO.BIK") 'WRITE "BGS_LOGO.BIK"
    8.                 IO.File.WriteAllLines("C:\test.txt"), lines)
    9.             Case False
    10.  
    11.                 lines(foundIndex) = Regex.Replace(lines(foundIndex), "(?<=" & "sIntroSequence" & "=)\d+", "") 'REMOVE TEXT (IF ANY PRESENT)
    12.                 IO.File.WriteAllLines("C:\test.txt"), lines)
    13.         End Select



    THE CODE THAT READS:



    vb Code:
    1. 'CHECK IF "sIntroSequence" IS AVAILABLE IN TEXT FILE.
    2.             Dim checkthevalue As String = My.Computer.FileSystem.ReadAllText("c:\test.txt")
    3.             If checkthevalue.Contains("sIntroSequence") Then
    4.      
    5.                 '"INTRO SEQUENCE" STRING IS AVAILABLE.
    6.                 'CHECK WHAT  COMES AFTER EQUALS SIGN.
    7.                 For Each k As String In IO.File.ReadAllLines("c:\test.txt")
    8.                     If k.Contains("sIntroSequence=BGS_LOGO.BIK") Then
    9.                 'IF IT IS "sIntroSequence=BGS_LOGO.BIK" THEN CHECKBOX18.CHECKED=TRUE.
    10.                         Form2.CheckBox18.Checked = True
    11.                     Else
    12.                 'OTHERWISE SET CHECKBOX18.CHECKED=FALSE.
    13.                         Form2.CheckBox18.Checked = False
    14.                     End If
    15.                 Next
    16.      
    17.             Else
    18.                 '"INTRO SEQUENCE" STRING IS NOT AVAILABLE IN THE TEXT.
    19.                 'SO CREATE IT...
    20.                 Dim lines() As String = System.IO.File.ReadAllLines("c:\test.txt")
    21.                 lines(10) &= vbNewLine & "sIntroSequence=" 'WRITE IT TO LINE10
    22.                 System.IO.File.WriteAllLines("c:\test.txt")
    23.             End If

  14. #14
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Could this be done?

    vb Code:
    1. Select Case Form2.CheckBox18.Checked
    2.             Case True
    3.  
    4.                 lines(foundIndex) = Regex.Replace(lines(foundIndex), "(?<=" & "sIntroSequence" & "=)\d+", "BGS_LOGO.BIK") 'WRITE "BGS_LOGO.BIK"
    5.                 IO.File.WriteAllLines("C:\test.txt"), lines)
    6.             Case False
    7.  
    8.                 lines(foundIndex) = Regex.Replace(lines(foundIndex), "(?<=" & "sIntroSequence" & "=)\d+", "") 'REMOVE TEXT (IF ANY PRESENT)
    9.                 IO.File.WriteAllLines("C:\test.txt"), lines)
    10.         End Select

    You are checking the wrong property in your Select Case statement

    One of the earlier posters resolved your problem already.
    Sorry you weren't paying attention

  15. #15

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    24

    Re: Could this be done?

    Quote Originally Posted by proneal View Post
    vb Code:
    1. Select Case Form2.CheckBox18.Checked
    2.             Case True
    3.  
    4.                 lines(foundIndex) = Regex.Replace(lines(foundIndex), "(?<=" & "sIntroSequence" & "=)\d+", "BGS_LOGO.BIK") 'WRITE "BGS_LOGO.BIK"
    5.                 IO.File.WriteAllLines("C:\test.txt"), lines)
    6.             Case False
    7.  
    8.                 lines(foundIndex) = Regex.Replace(lines(foundIndex), "(?<=" & "sIntroSequence" & "=)\d+", "") 'REMOVE TEXT (IF ANY PRESENT)
    9.                 IO.File.WriteAllLines("C:\test.txt"), lines)
    10.         End Select

    You are checking the wrong property in your Select Case statement

    One of the earlier posters resolved your problem already.
    Sorry you weren't paying attention
    This works as well. I tested it by replacing my code with msgbox("true") and msgbox("false") and i get the checked status information correctly.

  16. #16
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Could this be done?

    So Form2 is the default instance as well. You did not instantiate any new form2, is this right?

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Could this be done?

    Not much point in a select case for that. I would expect it is somewhat slower (not that you'd notice) than this:

    Code:
    If Form2.CheckBox18.Checked Then
     lines(foundIndex) = Regex.Replace(lines(foundIndex), "(?<=" & "sIntroSequence" & "=)\d+", "BGS_LOGO.BIK") 'WRITE "BGS_LOGO.BIK"                
     IO.File.WriteAllLines("C:\test.txt"), lines)
    Else
     lines(foundIndex) = Regex.Replace(lines(foundIndex), "(?<=" & "sIntroSequence" & "=)\d+", "") 'REMOVE TEXT (IF ANY PRESENT)                
     IO.File.WriteAllLines("C:\test.txt"), lines)
    End If
    The select case probably does a different conditional check for both True or False, whereas the If statement will only do a single conditional check. You'd have to study the MSIL generated from the code to be certain, but it seems logical.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width