我有一个XML文件,但有两个组合框,我想填充第二个,具体取决于第一个

时间:2012-07-10 08:10:26

标签: xml vb.net combobox

我的应用程序中有两个组合框。第一个组合框可以获得所有必需的数据(在这种情况下它是工厂编号)但是根据选择的工厂,我希望下一个组合框(在这种情况下是行号)自动填充该工厂的正确行。 这是我的XML文件 - 或测试数据

<?xml version="1.0" encoding="utf-8"?>
<Profiles>
<Factories>
  <Factory>F1</Factory>

  <Factory>F2</Factory>

  <Factory>F3</Factory>

  <Factory>F4</Factory>

  <Factory>F5</Factory>
  <Factory>F6</Factory>
  <Factory>F7</Factory>
  <Factory>F8</Factory>
</Factories>

  <Lines>
    <F1>G1</F1>
    <F1>G2</F1>
    <F1>G3</F1>
  </Lines>
</Profiles>

这是我到目前为止所做的:

Private Sub populateComboBoxes()
    Dim doc As New XmlDocument()
    doc.Load("C:\TFS2010Source\ShopFloorApps\Main\Source\Components\000280LinePCBackup\000280LinePCBackup\Lines.XML")

    Dim factoryList As XmlNodeList = doc.SelectNodes("/Profiles/Factories/Factory")
    For Each Factory As XmlNode In factoryList
        factoryComboBox.Items.Add(Factory.InnerText)
    Next



End Sub

Private Sub factoryComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles factoryComboBox.SelectedIndexChanged
    Dim doc As New XmlDocument()
    doc.Load("C:\TFS2010Source\ShopFloorApps\Main\Source\Components\000280LinePCBackup\000280LinePCBackup\Lines.XML")


    Dim lineList As XmlNodeList = doc.SelectNodes("/Profiles/Lines")

    For Each line As XmlNode In lineList


        If factoryComboBox.SelectedItem.ToString = line.FirstChild.Name Then


            lineComboBox.Items.Add(line.FirstChild.InnerText)


        End If
        Next
End Sub

所以我想要的是当我选择F1时,另一个组合框填充G1,G2和G3线。

由于

1 个答案:

答案 0 :(得分:1)

您必须清除lineComboBox的Items集合,并更改SelecteNodes路径。

lineComboBox.Items.Clear()
Dim lineList As XmlNodeList = doc.SelectNodes("/Profiles/Lines/" & factoryComboBox.SelectedItem.ToString)
For Each line As XmlNode In lineList
    lineComboBox.Items.Add(line.FirstChild.InnerText)
Next