Setting check boxes from text file list

First of all, I want to publicly sincerely apologize for my previous behavior. I have acted like a total butthead. I would have used a stronger word, but this is a public forum. This behavior will not continue. Please forgive me.

That said, if I may be so bold, I have a complicated problem…well, it’s complicated for my ill-equipped brain. I have developed an app that can uninstall the default apps that we are forced to have in windows 10. It can do everything at once, or by user-selected check boxes. What I would like to have happen is when the user opens the window to make selections, I would like to have all the uninstall choices they have previously implemented to be automatically unchecked, and the remaining choices checked. Under normal circumstances, this wouldn’t be a problem. The trouble is, the only ways to access these previous choices is either by running a powershell command that spits out a massive cryptic list of every detail of everything on your system (which is virtually indecipherable), or create a list in a text file of each app name as it gets uninstalled. I have chosen the text file route, with the contents displayed in a textarea. This seems really clunky and inadequate.
Is there a way I can read the text file (I know how to do that already) and use each line (one app name per line) to set the appropriate check box values to false while setting all the remaining check box values to true?

checkbox.value = not (is it in the list)

Could you please elaborate? I don’t understand.

Set the Checkbox.Value the oposite of if it’s in the List.
So if it’s in the List it’s a False and if it’s not in the List it’s a True.

[quote=388036:@Sascha S]Set the Checkbox.Value the oposite of if it’s in the List.
So if it’s in the List it’s a False and if it’s not in the List it’s a True.[/quote]
So something like this?

[code]Dim f As FolderItem
f = GetFolderItem(“uninstaller\removed_apps.txt”)
Dim t As TextInputStream

If f <> Nil Then
t = TextInputStream.Open(f)
If t <> Nil Then
dim content as string
content = t.ReadAll
if content = “3D Builder” then
builder.Value = False
else
builder.Value = True
end if
t.Close
Else
MsgBox(“Could not access data.”)
End If
End If[/code]
This would find that one line out of the entire list?
EDIT: or should I be trying to figure out TextInputStream.ReadLine?

You will need to change the paths to where you want to load/save, call LoadStates in your Window.Open and SaveStates in your Window.Close (ideally you want to do this on app start/stop but I wont complicate things for you) :slight_smile: This is just showing you how to do this, its not necessarily the best or right way to do this, its just one of many solutions.

This goes through each line, if the name maps to a checkbox it will tick it.

[code]Public Sub LoadStates(win As Window)
Dim f As FolderItem
f = SpecialFolder.desktop.Child(“removed_apps.txt”)

if f<>nil then
Dim t As TextInputStream = TextInputStream.Open(f)

Do
  
  If t <> Nil Then
    Dim entry As String = t.ReadLine
    For i As Integer = 0 To win.ControlCount -1
      If win.control(i) IsA CheckBox And win.control(i).name = entry Then
        checkbox(win.control(i)).value = True
      End If
    Next
    
  End If
  
Loop Until t.EOF

t.Close()

End If

End Sub[/code]

This goes through every control on the window, if its a checkbox it will save its name.

[code]Public Sub SaveStates(win As Window)
Dim f As FolderItem
f = SpecialFolder.desktop.Child(“removed_apps.txt”)

if f<>nil then
Dim t As TextOutputStream = TextOutputStream.Create(f)

For i As Integer = 0 To win.ControlCount -1
  If win.control(i) IsA CheckBox Then
    If checkbox(win.control(i)).value = True Then
      t.writeline(win.Control(i).name)
    End If
    
  End If
Next

t.Close()

End If

End Sub
[/code]

I am not sure, but i think reading all at once, then splitting it into lines and pushing the lines into an Array. Is faster then reading the file line by line.

And an IndexOf will reveal if the “3D Builder” is within the file.

many thanks to both of you! It seems I have a hefty learning curve in front of me. I sort of understand what you guys are giving me…I’m still very much a rookie with all this (in case you couldn’t tell!). I’ll apply some brain cells and see what I can do. Will post back and let you know (after I do some more RTFM).

OK. Thanks to you guys and Dave S (have I got the name right?) in another thread, I got it working.
in the action event for a button, I’ve added the following for each checkbox (obviously, names are changed accordingly). It is meant to prevent duplicate entries in removed_apps.txt

[code] Dim z As FolderItem
z = GetFolderItem(“uninstaller\removed_apps.txt”)
Dim j as TextInputStream
j = TextInputStream.Open(z)

dim k as string=j.READALL
dim txtstr() as string = Split(k,endofline)

Dim nameIndex As Integer
nameIndex = txtstr.IndexOf(“3D Builder”)
if nameIndex <> -1 then
j.Close()
else
Dim u as TextOutputStream

If z <> Nil then
  u = TextOutputStream.Append(z)
  u.WriteLine("3D Builder")
  u.Close()
  j.Close()
End If

end if[/code]
And in the open event for the containing window, in order to have the appropriate checkboxes automatically set to true, I have this for each checkbox:

[code]Dim z As FolderItem
z = GetFolderItem(“uninstaller\removed_apps.txt”)
Dim j as TextInputStream
j = TextInputStream.Open(z)

dim k as string=j.READALL
dim txtstr() as string = Split(k,endofline)

Dim nameIndex As Integer
nameIndex = txtstr.IndexOf(“3D Builder”)
if nameIndex <> -1 then
builder.Value = False
j.Close
else
builder.Value = True
j.close()
End If[/code]
As I said, this works, but I wonder…have I maybe missed a more efficient way of doing this?

Doing so in the Open Event of anything is not “wrong” but i would like to recommend to put it maybe in a Thread which reads the File and starts a Timer which handles the CheckBoxes, to prevent “slow opening” if the File is getting very big f.e…

Pulling Data from an external source in an open event, often leads to slow behaviour and your users may dislike it one day.

Anyway. I am very happy to read it’s working now as expected. :slight_smile:

[quote=388447:@Sascha S]Doing so in the Open Event of anything is not “wrong” but i would like to recommend to put it maybe in a Thread which reads the File and starts a Timer which handles the CheckBoxes, to prevent “slow opening” if the File is getting very big f.e…

Pulling Data from an external source in an open event, often leads to slow behaviour and your users may dislike it one day.

Anyway. I am very happy to read it’s working now as expected. :)[/quote]
Thanks, Sascha. I’ll have to look into using threads…have never done that before. In any case, the text file will never get very big, as there are a limited number of checkboxes and therefore a limited number of entries. Plus as shown above, duplicate entries are being prevented. I’ll look into threads though. Thanks. :slight_smile: