Checked listbox checked items to text box in VB.NET- 500pts, urgent

Hello,

I'm working on a question list for a VB.NET program.

I have a list of questions from a text file that I want to read into a checked list box.  (each line of the text file will represent 1 line of the checked listbox)  After the questions are added to the list box the user can then check off the questions they want to answer.  they will hit a button, and then only the checked off items should be placed into a multi-line text box (again, maintaining each question on one line).

There will be a fixed maximum of 30 questions (lines of text in text file).

I'm not that hot of a programmer, so please keep that in mind when answering (sorry).  

This is kind of urgent, hence the 500 points.

Thank you _very_ much!





SINEtistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mikal613Commented:
0
Carl TawnSystems and Integration DeveloperCommented:
Assuming you have a form with a CheckedListBox, Multiline textbox and a button; the following should do what you want:

Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Const QUESTION_FILE As String = "C:\Questions.txt"      '// Replace this with the path to your file

        If File.Exists(QUESTION_FILE) Then

            Dim fs As New FileStream("C:\Questions.txt", FileMode.Open)
            Dim sr As New StreamReader(fs)

            While Not sr.EndOfStream

                Dim s As String = sr.ReadLine()
                If s.Length > 0 Then
                    CheckedListBox1.Items.Add(s)
                End If

            End While

            sr.Dispose()
            fs.Dispose()

        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim list As String = String.Empty
        For Each s As String In CheckedListBox1.CheckedItems
            list += s + vbCrLf
        Next
        TextBox1.Text = list

    End Sub
End Class
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SINEtistAuthor Commented:
carl_tawn,

Your code looks promising, but I am getting some errors:  ( i may not have something set correctly)
========================================================================================================
Error1:
'EndOfStream' is not a member of 'System.IO.StreamReader'.


offending code:
While Not sr.EndOfStream

========================================================================================================

there are also two other errors, which may or may not be relevant after considering the first error:

========================================================================================================

Error2:
D:\VisualBasicProjects\question_list_test\Form1.vb(108): 'System.IO.FileStream.Protected Overridable Sub Dispose(disposing As Boolean)' is not accessible in this context because it is 'Protected'.


offending code:
            fs.Dispose()

========================================================================================================
Error3:
D:\VisualBasicProjects\question_list_test\Form1.vb(107): 'System.IO.StreamReader.Protected Overrides Sub Dispose(disposing As Boolean)' is not accessible in this context because it is 'Protected'.

offending code:
        sr.Dispose()

========================================================================================================


Any help on this would be greatly appreciated.

Thank you!
0
SINEtistAuthor Commented:
i've been working on this....

I tried closing the stream reader and file stream instead of using Dispose() and managed to get rid of the last two errors.

new code:

            sr.Close()
            fs.Close()


I'm not sure if this will break anything or not.  I'm still really stuck on the first part though.
0
SINEtistAuthor Commented:
well, I got it working.

I found this thread and combined it with the procedure that carl_tawn wrote for the button.

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20918450.html?query=vb.net+listbox+to+textbox&clearTAFilter=true

here is the full code:
=========================================================================================================

Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim sr As StreamReader = New StreamReader("C:\questions.txt")
        Dim line As String
        line = sr.ReadLine()
        While Not line Is Nothing
            CheckedListBox1.Items.Add(line)
            line = sr.ReadLine()
        End While
        sr.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim list As String = String.Empty
        For Each s As String In CheckedListBox1.CheckedItems
            list += s + vbCrLf
        Next
        TextBox1.Text = list

    End Sub
End Class

=========================================================================================================

Thank you to all!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.