listbox and checkbox

apacer

New member
Joined
Feb 7, 2013
Messages
3
Programming Experience
Beginner
Hello, I have a have the following question. I have a text file with 8 groups and each group has 8 questions/statements. For each argument can be chosen out of 5 answers. Each answer represents a value. Each group has a unique number and the statements also have a unique number. The listbox is filled with the questions, a new form with 5 checkboxes is displayed when a question is clicked. Is it somehow possible to create a relationship between the in the listbox, and the activated (chosen answer) checkbox? I want to count the values ​​of the check boxes (each answer has a value) per main group (number) and write the result in a text file write. Processing the data in a database seems like a bridge too far, if I can take this bridge already .... I have little experience with VB.NET and would like some tips, examples or get suggestions where to start. Thanks for the effort (to read)
 
There are various ways. You could create a Dictionary where the questions were the keys and the answers were the values. You could also define a Question class with a Text property and an Answer property. You could then bind a list of instances to your ListBox and set Text as the DisplayMember. When the user answers you get the selected Question from the SelectedItem of the ListBox and set its Answer property.
 
There are various ways. You could create a Dictionary where the questions were the keys and the answers were the values. You could also define a Question class with a Text property and an Answer property. You could then bind a list of instances to your ListBox and set Text as the DisplayMember. When the user answers you get the selected Question from the SelectedItem of the ListBox and set its Answer property.

I managed to get the questions as keys and answers as values in a dictonary, so far so good. But I now have the following challenge. How do I get all this content written in a .txt file? I used streamwriter but are stuck on getting the content. Any ideas, tips or examples? Couldn't find any here or with google. All examples I see are related to the console.writeline and not to write it to a file.
 
Just loop through the items in the Dictionary and call WriteLine on the StreamWriter for each one.

Humm, that worked but I now encounter another challenge. If I select a listbox item form2 is shown which contain the checkboxes. If I place these in the dictionary it is not possible to choose the same answer for a different question because this value has already been entered in the dictionary. Is it somehow possible to use the selected index as key in stead of the example below "Vraag 1" ? I don't want to create 64 forms for each question obviously....
VB.NET:
Public Class Form2
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.CheckState = 1 Then
            dict.Add("vraag 1", -10)
        End If
    End Sub

    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
        If CheckBox2.CheckState = 1 Then
            dict.Add("vraag 2", -5)
        End If
    End Sub

    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
        If CheckBox3.CheckState = 1 Then
            dict.Add("vraag 3", 0)
        End If
    End Sub

    Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
        If CheckBox4.CheckState = 1 Then
            dict.Add("vraag 4", 5)
        End If
    End Sub

    Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
        If CheckBox5.CheckState = 1 Then
            dict.Add("vraag 5", 10)
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Me.Close()
    End Sub
 
Back
Top