Jump to content

Populate a checkedlistbox from text file?

Hey everybody,

 

I am trying to populate a checkedboxlist with the appropriate checked items from a text file.  When I press the Load button on my program, it should populate with the checked items from the text file.  I have already converted the items to strings that write to the text file with a Save button, now I just need to be able to read the text file with a Load Button.  

 

Almost done making this program in visual basic - going to remake it in C#, but for now, visual basic help would be greatly appreciated.  Thanks!

 

PS:

Here's what I have so far for the Write portion of the program:

 

 Dim sb As New System.Text.StringBuilder
        For Each item In My.Forms.Form2.clbDiscA.CheckedItems
            sb.Append(item)
            sb.Append(" ")
        Next
 
        SW = New StreamWriter("C:\Users\Pikey10\Documents\Visual Studio 2013\Projects\Planner3\Read Write\clbDiscA.txt")
        SW.WriteLine(sb.ToString())

        SW.Close()

End Sub 

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

Ok so the file looks to be just a "space separated list of values".

 

Example: value1 value2 value3 ...

 

I'm assuming there are no spaces inside each value.

 

Read from a file and use the String.Split() method. Space is the default to split on, if you need something else in the future, you pass it the character to split on.

Try     Using sr As New StreamReader("TestFile.txt")        Dim line As String        line = sr.ReadToEnd()                Dim values As String() = line.Split()        For Each value As String In values            If Not String.IsNullOrEmpty(value) Then // Ignore any empty values in case they happen                ComboBox1.Items.Add(value)            End If        Next    End Using Catch e As Exception    Console.WriteLine("The file could not be read:")    Console.WriteLine(e.Message)End Try 
Link to comment
Share on other sites

Link to post
Share on other sites

 

Ok so the file looks to be just a "space separated list of values".

 

Example: value1 value2 value3 ...

 

I'm assuming there are no spaces inside each value.

 

Read from a file and use the String.Split() method. Space is the default to split on, if you need something else in the future, you pass it the character to split on.

Try     Using sr As New StreamReader("TestFile.txt")        Dim line As String        line = sr.ReadToEnd()                Dim values As String() = line.Split()        For Each value As String In values            If Not String.IsNullOrEmpty(value) Then // Ignore any empty values in case they happen                ComboBox1.Items.Add(value)            End If        Next    End Using Catch e As Exception    Console.WriteLine("The file could not be read:")    Console.WriteLine(e.Message)End Try 

Cool, thanks!  I will try this out...

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

An improvement would be to wrap your StreamWriter in a Using block as @madknight3 has done with his StreamReader.

 

See the example at the end of the MSDN Using Statement link: "Because the TextWriter and TextReader classes implement the IDisposable interface, the code can use Using statements to ensure that the file is correctly closed after the write and read operations." StreamWriter & StreamReader inherit these classes.

 

Also multiple appends/concatenations can and should be saved by using String.Format.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Also multiple appends/concatenations can and should be saved by using String.Format.

Just wanted to clarify on this point as I don't see where a problem exists in the current examples. It seems like he's using StringBuilder correctly.

Link to comment
Share on other sites

Link to post
Share on other sites

Just wanted to clarify on this point as I don't see where a problem exists in the current examples. It seems like he's using StringBuilder correctly.

 

Of course and there's no problem at all really. I'm just being picky over details.

 sb.Append(item) sb.Append(" ")

Would be more concise as:

 sb.Append(String.Format("{0} ", item))

You can eliminate a line and practice a new concept.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Oh!  Yeah, that use of append was basically recommended to me...I like the idea of it being more cleaned up, though...

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

Of course and there's no problem at all really. I'm just being picky over details.

 sb.Append(item) sb.Append(" ")

Would be more concise as:

 sb.Append(String.Format("{0} ", item))

You can eliminate a line and practice a new concept.

 

Hmm, in this case, I would imagine that is slightly less efficient though, as String.Format is internally using another StringBuilder. With that said, it's nothing to worry about in this situation, does look cleaner, and does introduce a new concept :)

Link to comment
Share on other sites

Link to post
Share on other sites

Hmm, in this case, I would imagine that is slightly less efficient though, as String.Format is internally using another StringBuilder. With that said, it's nothing to worry about in this situation, does look cleaner, and does introduce a new concept :)

I'll mess around with both ideas! :)

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

Hmm, in this case, I would imagine that is slightly less efficient though, as String.Format is internally using another StringBuilder. With that said, it's nothing to worry about in this situation, does look cleaner, and does introduce a new concept :)

 

Hehe that's where StringBuilder.AppendFormat comes to the rescue!!!

sb.AppendFormat("{0} ", item)

I forgot all about it  :lol:

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Hehe that's where StringBuilder.AppendFormat comes to the rescue!!!

sb.AppendFormat("{0} ", item)

I forgot all about it  :lol:

 

That's definitely better, and introduces another new method to know about! I actually didn't even know about it :P

 

But it's still overkill for something as simple as

sb.Append(item).Append(" ")

:P (and it's not that ugly)

 

Anyway, like I said before, performance isn't a concern in this particular situation, but it doesn't hurt to be aware of this stuff anyway ;)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×