1

I am creating a program which someone can input a search into a textbox and then narrow down the results using a series of comboboxes (or just use the comboboxes to search through everything).

The program looks like this: form 1

I have made the options in the 2nd combobox change using the following code:

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim type As String = ComboBox1.SelectedItem
    Dim make As String = ComboBox2.SelectedItem
    Dim model As String = ComboBox3.SelectedItem
    Dim version As String = TextBox2.Text
    Dim memory As String = TextBox3.Text
    Dim problem As String = TextBox4.Text
    Dim casenumber As Integer = Int(Rnd() * 9999) + 1000
    If type = "Phone" Then
        ComboBox2.Items.Clear()
        Dim file As New System.IO.StreamReader("E: \phone.txt")
        For i = 1 To 20
            q(i) = file.ReadLine() & vbNewLine
            ComboBox2.Items.Add(q(i))
        Next
    ElseIf type = "Tablet" Then
        ComboBox2.Items.Clear()
        Dim file As New System.IO.StreamReader("E:\tablet.txt")
        For i = 1 To 20
            q(i) = file.ReadLine() & vbNewLine
            ComboBox2.Items.Add(q(i))
        Next
    ElseIf type = "Desktop computer" Then
        ComboBox2.Items.Clear()
        Dim file As New System.IO.StreamReader("E:\pc.txt")
        For i = 1 To 20
            q(i) = file.ReadLine() & vbNewLine
            ComboBox2.Items.Add(q(i))
        Next
    ElseIf type = "Laptop" Then
        ComboBox2.Items.Clear()
        Dim file As New System.IO.StreamReader("E:\laptop.txt")
        For i = 1 To 20
            q(i) = file.ReadLine() & vbNewLine
            ComboBox2.Items.Add(q(i))
        Next
        'Else
        'Dim objwriter As System.IO.StreamWriter
        'objwriter = My.Computer.FileSystem.OpenTextFileWriter("E:\unknown.txt", True)
        'File.AppendText("type:" And ComboBox1.Text And "make" & ComboBox2.Text And "model: " & ComboBox3.Text And "version: " And TextBox2.Text & "memory" And TextBox3.Text)
    End If
End Sub

However,the code won't work to change what is in the 3rd box. I have repeated the following code for each option:

Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
    Dim type As String = ComboBox1.SelectedItem
    Dim make As String = ComboBox2.SelectedItem
    Dim model As String = ComboBox3.SelectedItem
    Dim version As String = TextBox2.Text
    Dim memory As String = TextBox3.Text
    Dim problem As String = TextBox4.Text
    If type = "Phone" Then
        If make = "apple" Then
            ComboBox2.Items.Clear()
            Dim file As New System.IO.StreamReader("E:\apple.txt")
            For i = 1 To 20
                q(i) = file.ReadLine() & vbNewLine
                ComboBox3.Items.Add(q(i))
            Next
        ElseIf make = "samsung" Then
            ComboBox2.Items.Clear()
            Dim file As New System.IO.StreamReader("E:\samsung.txt")
            For i = 1 To 20
                q(i) = file.ReadLine() & vbNewLine
                ComboBox3.Items.Add(q(i))
            Next
        ElseIf make = "htc" Then
            ComboBox2.Items.Clear()
            Dim file As New System.IO.StreamReader("E:\htc.txt")
            For i = 1 To 20
                q(i) = file.ReadLine() & vbNewLine
                ComboBox3.Items.Add(q(i))
            Next
        ElseIf make = "Nokia" Then
            ComboBox2.Items.Clear()
            Dim file As New System.IO.StreamReader("E:\Nokia.txt")
            For i = 1 To 20
                q(i) = file.ReadLine() & vbNewLine
                ComboBox3.Items.Add(q(i))
            Next
        ElseIf make = "Blackberry" Then
            ComboBox2.Items.Clear()
            Dim file As New System.IO.StreamReader("E:\blackberry.txt")
            For i = 1 To 20
                q(i) = file.ReadLine() & vbNewLine
                ComboBox3.Items.Add(q(i))
            Next

I have checked the obvious problems like putting the wrong text file names and capital letters etc, but can't get this to work whatever I do.

Does anyone know why the third combobox remains blank even when both conditions are met (both combobox1 and 2 have the right thing selected)? Any suggestion would be much appreciated.

d.Lewin
  • 66
  • 10
  • 1
    Why do you clear the contents of ComboBox2 and then add items to ComboBox3? Your code is confusing. You have ComboBox2_SelectedIndexChanged handling the event for ComboBox3? Might help if you gave your controls meaningful names. – LarsTech Jan 13 '16 at 19:52
  • i cleared the content first as otherwise if you clicked the wrong thing in the first combobox it would just add more options rather than completly changing them – d.Lewin Jan 13 '16 at 19:54
  • i put comboBox2_SelectedIndexChanged handling the event for ComboBox3 as i wanted to change combobox3 depending on what was selected for combobox2, so i thought this made sense – d.Lewin Jan 13 '16 at 20:00
  • No, that doesn't make sense. If a user selects something in ComboBox2, then you need to handle the SelectedIndexChanged event for ComboBox2 to populate the items in ComboBox3. – LarsTech Jan 13 '16 at 20:03
  • ok i'll change it and see if it works – d.Lewin Jan 13 '16 at 20:09
  • Don't forget to change ComboBox2.Items.Clear to ComboBox3. – LarsTech Jan 13 '16 at 20:10
  • i change this but it still doesn't work – d.Lewin Jan 13 '16 at 20:11
  • oops didn't realise i had put combobox2.items.clear(). i meant to put combobox3. thanks for pointing that out (I still can't get it to work though) – d.Lewin Jan 13 '16 at 20:16
  • In ComboBox1, you would have to also clear the contents of ComboBox3, too. Why would you include vbNewLine in your ComboBox items? – LarsTech Jan 13 '16 at 20:20
  • i put vbnewline as the program reads what's meant to go into the next combobox from a textfile and i used he vbnewline to make it write each new line as a different item I hadn't thought about clearing combobox3 so i will add that to the code – d.Lewin Jan 13 '16 at 20:24
  • Don't include the vbNewLine in your data. – LarsTech Jan 13 '16 at 20:25
  • ok i'll get rid of that – d.Lewin Jan 13 '16 at 20:25
  • it comes up with an error if i don't have vbnewline – d.Lewin Jan 13 '16 at 20:27
  • You know, comments like "it doesn't work" and "comes up with an error" without telling the other person what those details are makes it really hard to help you. Why do you assume every file has 20 entries? Just read the lines until you don't have anymore. – LarsTech Jan 13 '16 at 20:30
  • ok sorry for not including details. Not sure why i set it to 20 i think it was just because there is 20 for each one. Yes i probably should of just put it so it reads till there's nothing anymore. ( the error was argumentnullexception was unhandled -- an unhandle exceptionof type 'system argumentnullexception' occurred in system.windows.forms.dll) – d.Lewin Jan 13 '16 at 20:40
  • FYI, since every single one of your `if` / `else if` statements do the exact same thing after you get your stream reader, you can simply move that loop *outside* of the branches so you only write it once instead of having the exact same code copied 5 times. Same thing with clearing the combo box. Every branch does it, pull it out of the `if` statements and just write it once. – sab669 Jan 13 '16 at 21:24
  • ok i have moved clear outside the if loop but i though i had to put the stream reader on each `if` / `elseif` statement as each one reads a different file – d.Lewin Jan 13 '16 at 21:49

1 Answers1

0

Firstly I suspect that this code :-

Dim type As String = ComboBox1.SelectedItem.ToString
Dim make As String = ComboBox2.SelectedItem.ToString
Dim model As String = ComboBox3.SelectedItem.ToString
Dim version As String = TextBox2.Text
Dim memory As String = TextBox3.Text
Dim problem As String = TextBox4.Text

shouldn't be in each if your events. They should really be placed somewhere that executes after all the information has been chosen yes?

OK for a start paste this into a text file called "device type.txt"

Phone,E:\phone.txt
Tablet,E:\tablet.txt
Desktop Computer,E:\pc.txt
Laptop,E:\laptop.txt

Then edit your phones.txt file and the rest of the above files to match that format e.g this phone.txt file

Apple,E:\apple.txt
Samsung,E:\samsung.txt
Htc,E:\htc.txt
Nokia,E\nokia.txt
Blackberry,E:\blackberry.txt

and so on for each item that links to another file. For the last files in the tree - which I presume are the files containing a list of the models for each brand of phone, just leave them as a simple list. No commas or anything after each model.

Use this code to do the populating of each ComboBox

Private Sub PopulateComboBox(ByRef cboBox As ComboBox, ByVal itemSource As String)
    RemoveHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
    RemoveHandler ComboBox2.SelectedIndexChanged, AddressOf ComboBox2_SelectedIndexChanged
    RemoveHandler ComboBox3.SelectedIndexChanged, AddressOf ComboBox3_SelectedIndexChanged
    Dim devices As New List(Of item)
    Dim csvFlag As Boolean = False
    cboBox.Items.Clear()
    Using MyReader As New Microsoft.VisualBasic.
        FileIO.TextFieldParser(itemSource)
        If MyReader.ReadLine.Contains(",") Then csvFlag = True
    End Using

    Using MyReader As New Microsoft.VisualBasic.
        FileIO.TextFieldParser(itemSource)
        If csvFlag Then
            MyReader.TextFieldType = FileIO.FieldType.Delimited
            MyReader.SetDelimiters(",")
        End If
        Dim currentRow As String() = {"", ""}
        While Not MyReader.EndOfData
            Try
                If csvFlag Then
                    currentRow = MyReader.ReadFields()
                    Dim tempItem As New item
                    tempItem.item = currentRow(0)
                    tempItem.fileName = currentRow(1)
                    devices.Add(tempItem)
                Else
                    currentRow(0) = MyReader.ReadLine
                    Dim tempItem As New item
                    tempItem.item = currentRow(0)
                    tempItem.fileName = ""
                    devices.Add(tempItem)
                End If

            Catch ex As Microsoft.VisualBasic.
              FileIO.MalformedLineException
                MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
            End Try
        End While
    End Using
    If csvFlag Then
        cboBox.DataSource = devices
    cboBox.DisplayMember = "item"
        cboBox.ValueMember = "fileName"
    Else
        cboBox.DataSource = devices
        cboBox.DisplayMember = "item"
        cboBox.ValueMember = "item"
    End If
    AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
    AddHandler ComboBox2.SelectedIndexChanged, AddressOf ComboBox2_SelectedIndexChanged
    AddHandler ComboBox3.SelectedIndexChanged, AddressOf ComboBox3_SelectedIndexChanged
End Sub

What it does is firstly check to see if the file being read is a comma-delimited file.

If it is delimited, it will read the file referred to in itemSource and expect a pair of values. The first value is what you see in the box(the DisplayMember), and the second value is what clicking on the box actually returns(the ValueMember)

If the file isn't comma-delimited, it will just read each line and add it to the combobox so that it acts normally (this will be important for the last files in the tree)

Next you need these methods for the ComboBoxes

Private Sub PopulateComboBox1()
    PopulateComboBox(ComboBox1, "E:\device type.txt")
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    PopulateComboBox(ComboBox2, ComboBox1.SelectedValue.ToString)
End Sub

Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
    PopulateComboBox(ComboBox3, ComboBox2.SelectedValue.ToString)
End Sub

Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
    PopulateComboBox(ComboBox3, ComboBox2.SelectedValue.ToString)
End Sub

Call the method to populate Combobox1 possibly in the form's load event.

David Wilson
  • 4,369
  • 3
  • 18
  • 31