vb.net根据2个以前的组合框中选择的项目更改组合框选项 [英] vb.net change combobox options depending on selected item in 2 previous comboboxes

查看:414
本文介绍了vb.net根据2个以前的组合框中选择的项目更改组合框选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

此计划如下所示:表单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.

有人知道为什么第三个组合框仍然空白,即使两个条件都满足(combobox1和2有正确的事情选择)?

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.

推荐答案

首先我怀疑这个代码: -

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?

确定开始将它粘贴到一个名为device type.txt的文本文件

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


电话,E:\phone.txt

平板电脑,E:\tablet.txt

桌面计算机,E:\ pc.txt

笔记本电脑,E:\laptop.txt

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

然后编辑您的phones.txt文件和以上文件的其余部分匹配格式,例如此phone.txt文件

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


E:\apple.txt

Samsung,E:\samsung.txt

Htc,E:\htc.txt

Nokia,E\\ \\ nokia.txt

Blackberry,E:\blackberry.txt

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.

使用此代码填充每个ComboBox

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.

如果是分隔的,它将读取 itemSource 中引用的文件,并期望一对值。第一个值是在框中显示的(DisplayMember),第二个值是点击框实际返回的值(ValueMember)

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)

下一步需要ComboBox的这些方法

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

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

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

这篇关于vb.net根据2个以前的组合框中选择的项目更改组合框选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆