C
C#•5d ago
noeomii

Need help saving data from checkedlistbox

I'm working on a checklist project to keep track of which songs from which albums I've edited and I'm having trouble figuring out how to save both the checked states and list text as a beginner. The method I was trying was to create individual text files for each album but I'm not sure how to write and read the true/false statements for a checked box in the same line as the text and need some help getting pointed in the right direction for this
No description
11 Replies
Angius
Angius•5d ago
What's the UI framework you're using?
noeomii
noeomiiOP•5d ago
Oh I forgot to clarify, I'm using .NET
Angius
Angius•5d ago
What UI framework Winforms, WPF, Avalonia, MAUI, WinUI, Uno...
noeomii
noeomiiOP•5d ago
Ah sorry, Winforms
Angius
Angius•5d ago
I think you need a BindingList<T>: https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.bindinglist-1?view=net-9.0 Though I haven't dabbled in Winforms much, so maybe there are better ways
trukafatsum
trukafatsum•5d ago
I have an idea for this, asked ChatGPT 🙂 If you save like this,
1|Song A|true
2|Song B|false
3|Song C|true
1|Song A|true
2|Song B|false
3|Song C|true
To read the data:
var lines = File.ReadAllLines("album1.txt");
var songs = lines.Select(line => {
var parts = line.Split('|');
return new {
Id = int.Parse(parts[0]),
Name = parts[1],
IsChecked = bool.Parse(parts[2])
};
}).ToList();
var lines = File.ReadAllLines("album1.txt");
var songs = lines.Select(line => {
var parts = line.Split('|');
return new {
Id = int.Parse(parts[0]),
Name = parts[1],
IsChecked = bool.Parse(parts[2])
};
}).ToList();
To write it back:
var linesToSave = songs.Select(s => $"{s.Id}|{s.Name}|{s.IsChecked}");
File.WriteAllLines("album1.txt", linesToSave);
var linesToSave = songs.Select(s => $"{s.Id}|{s.Name}|{s.IsChecked}");
File.WriteAllLines("album1.txt", linesToSave);
This way: Each line includes both the song text and its checked state, It’s easy to split with Split('|'), You can easily use LINQ for filtering or updating (Where, Select, FirstOrDefault, etc.). It’s simple, clean, and scalable — later they could even add more fields like AlbumName or Duration without changing the structure.
noeomii
noeomiiOP•5d ago
I'll check this out, thank you! Ahh I see, this makes sense I didn't realize it was so simply to split text using Split() thank you that's helpful!
Angius
Angius•5d ago
Or you could just use JSON instead of rolling your own format
// load
var json = File.ReadAllText("data.json");
var data = JsonSerializer.Deserialize<List<Song>>(json);

// save
var data = JsonSerializer.Serialize(songs);
File.WriteAllText(data);
// load
var json = File.ReadAllText("data.json");
var data = JsonSerializer.Deserialize<List<Song>>(json);

// save
var data = JsonSerializer.Serialize(songs);
File.WriteAllText(data);
noeomii
noeomiiOP•5d ago
That's true I did saw a post about using json on vb. I don't know anything about json files right now but they sound useful Oh that looks much cleaner, I'll do some research to understand this better
Angius
Angius•5d ago
Could even just store everything in one file instead of having each album be a separate one Ideally, you'd want to use a database But baby steps, JSON is good enough
noeomii
noeomiiOP•5d ago
Yeah I'd definitely want to do this eventually. My plan with this project was to start with some simpler potentially clunky stuff and then slowly make it more efficient and add in more functions like a media player

Did you find this page helpful?