?
Solved

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

Posted on 2006-04-05
5
Medium Priority
?
822 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!





0
Comment
Question by:SINEtist
  • 3
5 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 16384938
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1000 total points
ID: 16385351
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
 

Author Comment

by:SINEtist
ID: 16387511
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
 

Author Comment

by:SINEtist
ID: 16387743
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
 

Author Comment

by:SINEtist
ID: 16387931
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question