簡體   English   中英

vb.net更改組合框選項,具體取決於之前2個組合框中的所選項目

[英]vb.net change combobox options depending on selected item in 2 previous comboboxes

我正在創建一個程序,有人可以在一個文本框中輸入搜索內容,然后使用一系列組合框來縮小搜索范圍(或者只是使用組合框來搜索所有內容)。

該程序如下所示: 表格1

我使用以下代碼更改了第二個組合框中的選項:

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

但是,該代碼將無法更改第三個框中的內容。 我為每個選項重復了以下代碼:

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

我已經檢查了明顯的問題,例如輸入了錯誤的文本文件名和大寫字母等,但是無論我做什么都無法使它正常工作。

有誰知道為什么即使同時滿足兩個條件(組合框1和2都選擇了正確的選項),第三個組合框仍為空白? 任何建議將不勝感激。

首先,我懷疑這段代碼:

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

如果您的事件不應該出現在每個事件中。 在選擇了所有信息之后,應該將它們確實放置在可以執行的地方嗎?

首先,將其粘貼到名為“ device type.txt”的文本文件中

電話,E:\\ phone.txt
平板電腦,E:\\ tablet.txt
台式計算機,E:\\ pc.txt
筆記本電腦,E:\\ laptop.txt

然后編輯您的phone.txt文件和上述其他文件以匹配該格式,例如此phone.txt文件

蘋果,E:\\ apple.txt
三星,E:\\ samsung.txt
HTC,E:\\ htc.txt
諾基亞,E \\ nokia.txt
黑莓,E:\\ blackberry.txt

對於鏈接到另一個文件的每個項目,依此類推。 對於樹中的最后一個文件-我想是包含每個品牌手機型號列表的文件,只需將它們保留為簡單列表即可。 每個型號后都沒有逗號或其他任何內容。

使用此代碼填充每個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

首先要檢查讀取的文件是否是逗號分隔的文件。

如果定界,它將讀取itemSource中引用的文件,並期望使用一對值。 第一個值是您在框中看到的值(DisplayMember),第二個值是單擊該框后實際返回的值(ValueMember)

如果文件不是逗號分隔的,它將讀取每一行並將其添加到組合框,以便其正常運行(這對於樹中的最后一個文件很重要)

接下來,您需要將這些方法用於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

調用該方法以可能在窗體的load事件中填充Combobox1。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM