|
-
May 21st, 2012, 06:14 PM
#1
Thread Starter
Junior Member
Could this be done?
Everything is written in codes:
vb Code:
'CHECK IF "sIntroSequence" IS AVAILABLE IN TEXT FILE. Dim checkthevalue As String = My.Computer.FileSystem.ReadAllText("c:\test.txt") If checkthevalue.Contains("sIntroSequence") Then '"INTRO SEQUENCE" STRING IS AVAILABLE. 'CHECK WHAT COMES AFTER EQUALS SIGN. For Each k As String In IO.File.ReadAllLines("c:\test.txt") If k.Contains("sIntroSequence=BGS_LOGO.BIK") Then 'IF IT IS "sIntroSequence=BGS_LOGO.BIK" THEN CHECKBOX18.CHECKED=TRUE. Form2.CheckBox18.Checked = True Else 'OTHERWISE SET CHECKBOX18.CHECKED=FALSE. Form2.CheckBox18.Checked = False End If Next Else '"INTRO SEQUENCE" STRING IS NOT AVAILABLE IN THE TEXT. 'SO CREATE IT... Dim lines() As String = System.IO.File.ReadAllLines("c:\test.txt") lines(10) &= vbNewLine & "sIntroSequence=" 'WRITE IT TO LINE10 System.IO.File.WriteAllLines("c:\test.txt") 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.
-
May 21st, 2012, 06:21 PM
#2
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.
-
May 21st, 2012, 06:42 PM
#3
Thread Starter
Junior Member
Re: Could this be done?
 Originally Posted by Niya
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.
-
May 21st, 2012, 06:47 PM
#4
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.
-
May 21st, 2012, 06:55 PM
#5
Thread Starter
Junior Member
Re: Could this be done?
 Originally Posted by DataMiser
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.
-
May 21st, 2012, 06:57 PM
#6
-
May 21st, 2012, 07:00 PM
#7
Re: Could this be done?
 Originally Posted by reinhold
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.
-
May 21st, 2012, 07:18 PM
#8
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.
-
May 21st, 2012, 08:39 PM
#9
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.
-
May 22nd, 2012, 05:19 AM
#10
Thread Starter
Junior Member
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 "".
-
May 22nd, 2012, 09:58 AM
#11
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.
-
May 22nd, 2012, 10:06 AM
#12
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
 
-
May 22nd, 2012, 05:49 PM
#13
Thread Starter
Junior Member
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim lines() As String = IO.File.ReadAllLines("C:\test.txt") Dim foundIndex As Integer = Array.FindIndex(lines, Function(l) l.StartsWith("sIntroSequence")) Select Case Form2.CheckBox18.Checked Case True lines(foundIndex) = Regex.Replace(lines(foundIndex), "(?<=" & "sIntroSequence" & "=)\d+", "BGS_LOGO.BIK") 'WRITE "BGS_LOGO.BIK" IO.File.WriteAllLines("C:\test.txt"), lines) Case False lines(foundIndex) = Regex.Replace(lines(foundIndex), "(?<=" & "sIntroSequence" & "=)\d+", "") 'REMOVE TEXT (IF ANY PRESENT) IO.File.WriteAllLines("C:\test.txt"), lines) End Select
THE CODE THAT READS:
vb Code:
'CHECK IF "sIntroSequence" IS AVAILABLE IN TEXT FILE. Dim checkthevalue As String = My.Computer.FileSystem.ReadAllText("c:\test.txt") If checkthevalue.Contains("sIntroSequence") Then '"INTRO SEQUENCE" STRING IS AVAILABLE. 'CHECK WHAT COMES AFTER EQUALS SIGN. For Each k As String In IO.File.ReadAllLines("c:\test.txt") If k.Contains("sIntroSequence=BGS_LOGO.BIK") Then 'IF IT IS "sIntroSequence=BGS_LOGO.BIK" THEN CHECKBOX18.CHECKED=TRUE. Form2.CheckBox18.Checked = True Else 'OTHERWISE SET CHECKBOX18.CHECKED=FALSE. Form2.CheckBox18.Checked = False End If Next Else '"INTRO SEQUENCE" STRING IS NOT AVAILABLE IN THE TEXT. 'SO CREATE IT... Dim lines() As String = System.IO.File.ReadAllLines("c:\test.txt") lines(10) &= vbNewLine & "sIntroSequence=" 'WRITE IT TO LINE10 System.IO.File.WriteAllLines("c:\test.txt") End If
-
May 22nd, 2012, 10:22 PM
#14
Re: Could this be done?
vb Code:
Select Case Form2.CheckBox18.Checked Case True lines(foundIndex) = Regex.Replace(lines(foundIndex), "(?<=" & "sIntroSequence" & "=)\d+", "BGS_LOGO.BIK") 'WRITE "BGS_LOGO.BIK" IO.File.WriteAllLines("C:\test.txt"), lines) Case False lines(foundIndex) = Regex.Replace(lines(foundIndex), "(?<=" & "sIntroSequence" & "=)\d+", "") 'REMOVE TEXT (IF ANY PRESENT) IO.File.WriteAllLines("C:\test.txt"), lines) 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
-
May 23rd, 2012, 02:04 AM
#15
Thread Starter
Junior Member
Re: Could this be done?
 Originally Posted by proneal
vb Code:
Select Case Form2.CheckBox18.Checked Case True lines(foundIndex) = Regex.Replace(lines(foundIndex), "(?<=" & "sIntroSequence" & "=)\d+", "BGS_LOGO.BIK") 'WRITE "BGS_LOGO.BIK" IO.File.WriteAllLines("C:\test.txt"), lines) Case False lines(foundIndex) = Regex.Replace(lines(foundIndex), "(?<=" & "sIntroSequence" & "=)\d+", "") 'REMOVE TEXT (IF ANY PRESENT) IO.File.WriteAllLines("C:\test.txt"), lines) 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.
-
May 23rd, 2012, 05:57 AM
#16
Re: Could this be done?
So Form2 is the default instance as well. You did not instantiate any new form2, is this right?
-
May 23rd, 2012, 09:20 AM
#17
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|