0

我目前正在尝试编写一个像地址簿这样的应用程序。列表框工作正常,它正确地显示了一切。但我需要将所选列表框项的 id 传递给另一个表单。我在 Form2 中得到了这样的代码:

Private myTable As New DataTable()
Public Sub LoadXml(sender As Object, e As EventArgs) Handles Me.Load

    With myTable.Columns
        .Add("DisplayValue", GetType(String))
        .Add("HiddenValue", GetType(Integer))
    End With
    myTable.DefaultView.Sort = "DisplayValue ASC"

    ListBox1.DisplayMember = "DisplayValue"
    ListBox1.ValueMember = "HiddenValue"
    ListBox1.DataSource = myTable
    Dim doc As New Xml.XmlDocument
    doc.Load("c:\address.xml")
    Dim xmlName As Xml.XmlNodeList = doc.GetElementsByTagName("name")
    Dim xmlSurname As Xml.XmlNodeList = doc.GetElementsByTagName("surname")
    Dim xmlId As Xml.XmlNodeList = doc.GetElementsByTagName("id")
    For i As Integer = 0 To xmlName.Count - 1
        Dim nazwa As String = xmlName(i).FirstChild.Value + " " + xmlSurname(i).FirstChild.Value
        myTable.Rows.Add(nazwa, xmlId(i).FirstChild.Value)
        MsgBox(myTable.Rows(i).Item(1).ToString)
    Next i
    ListBox1.Sorted = True
End Sub

稍后在代码中我有事件:

Public Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick

End Sub

我想知道如何从 DataTable 中为选定的列表框项目调用 id。我希望你明白我的意思,因为我的英语不完美:)

4

1 回答 1

0

Since you have added the XML value id to the data table column HiddenValue and you have assigned HiddenValue as the ValueMember for the listbox, once a record is selected in the listbox, id will be available in the listbox's [SelectedValue][1] member. For example:

Public Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
    MsgBox("Selected Id: " & ListBox1.SelectedValue.ToString())
End Sub
于 2013-01-06T00:18:25.400 回答