We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

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

SINEtist
SINEtist asked
on
Medium Priority
969 Views
Last Modified: 2012-06-27
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!





Comment
Watch Question

CERTIFIED EXPERT

Commented:
Senior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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!

Author

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.

Author

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!!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.