【问题标题】:vb.net change combobox options depending on selected item in 2 previous comboboxesvb.net 根据前两个组合框中的选定项目更改组合框选项
【发布时间】:2016-04-18 23:06:14
【问题描述】:

我正在创建一个程序,有人可以将搜索输入到文本框中,然后使用一系列组合框(或仅使用组合框搜索所有内容)缩小结果范围。

程序如下所示:form 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

我已经检查了一些明显的问题,例如输入错误的文本文件名和大写字母等,但无论我做什么都无法让它工作。

有谁知道为什么即使同时满足两个条件(combobox1 和 2 都选择了正确的东西)第三个组合框仍然空白?任何建议将不胜感激。

【问题讨论】:

  • 为什么要清除ComboBox2的内容,然后再向ComboBox3添加项目?您的代码令人困惑。您有 ComboBox2_SelectedIndexChanged 处理 ComboBox3 的事件吗?如果您给控件起有意义的名称,可能会有所帮助。
  • 我先清除了内容,否则如果您在第一个组合框中单击了错误的内容,它只会添加更多选项而不是完全更改它们
  • 我让 comboBox2_SelectedIndexChanged 处理 ComboBox3 的事件,因为我想根据为 combobox2 选择的内容来更改 combobox3,所以我认为这是有道理的
  • 不,这没有意义。如果用户在 ComboBox2 中选择了某些内容,那么您需要处理 ComboBox2 的 SelectedIndexChanged 事件以填充 ComboBox3 中的项目。
  • 好的,我去改一下看看能不能用

标签: vb.net winforms visual-studio combobox visual-studio-2015


【解决方案1】:

首先我怀疑这段代码:-

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

然后编辑您的phones.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

可能在表单的加载事件中调用填充 Combobox1 的方法。

【讨论】:

  • 谢谢我会试试这个
猜你喜欢
  • 1970-01-01
  • 2015-09-30
  • 2015-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-17
  • 1970-01-01
相关资源
最近更新 更多