1

我有一堂课,如下所示:

Public Class parameters
  Public Property test As String
  Public Property test_type As String
  Public Property user_test_name As String
  Public Property meas As String
  Public Property spec As String
  ...etc
End Class

我列出了我从某个地方的 csv 导入的对象。列表中的 user_test_name 被发送到列表框:

For Each parameters In param
  ' MsgBox(parameters.user_test_name)
  ListBox1.Items.Add(parameters.user_test_name)
Next

现在,当用户从列表中选择某些内容时,我希望该特定 user_test_name 对象的其余属性填充到应用程序的某些文本/组合框中。这是我如何获取所选内容的方法。

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object,
            ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
  Dim selected_name As String = ListBox1.SelectedItem()
  ' MsgBox(selected_name)
  find_object_by_user_test_name(selected_name)
End Sub

现在我很难从列表中找到具有 selected_name 的对象并使用它的属性来填充 text.combo 框。我尝试了以下没有成功:

Public Sub find_object_by_user_test_name(ByVal description)
  MsgBox(description)
  Dim matches = From parameters In param
  Where parameters.user_test_name = description
  Select parameters
  ' MsgBox(matches)
  '  MsgBox(matches.user_test_name)
  TextBox1.Text = matches.test
  TextBox2.Text = matches.test_name
  etc,,, on and on
  ' populate_area(matches)
End Sub
4

4 回答 4

1

如果 user_test_names 是唯一的,那么使用字典并以这种方式检索对象可能会更容易。

Dim params As New Dictionary(Of String, parameters)

params.add(MyParameterObject.user_test_name, MyParameterObject)

然后

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    Dim selected_name As String = ListBox1.SelectedItem()
    Dim selected_param as parameters = params(selected_name)



End Sub
于 2013-06-03T19:18:47.760 回答
1

不要将名称(字符串)添加到 ListBox,而是将您的实际实例添加到其中。

首先,在您的类中覆盖 ToString() 以便它在您的 ListBox 中正确显示:

Public Class parameters

    Public Property test As String
    Public Property test_type As String
    Public Property user_test_name As String
    Public Property meas As String
    Public Property spec As String

    Public Overrides Function ToString() As String
        Return user_test_name
    End Function

End Class

接下来,将每个实例添加到 ListBox:

For Each parameters In param
    ListBox1.Items.Add(parameters)
Next

现在,在 SelectedIndexChanged() 事件中,您可以将 SelectedItem() 项转换回参数,并且您已经拥有了一切供您使用:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object,
            ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    If ListBox1.SelectedIndex <> -1 Then
        Dim P As parameters = DirectCast(ListBox1.SelectedItem, parameters)
        ' ... do something with "P" ...
        Debug.Print(P.user_test_name & " --> " & P.test)
    End If
End Sub
于 2013-06-03T19:39:10.027 回答
0

这样的事情应该这样做:

Public Sub find_object_by_user_test_name(ByVal description As String)
  ' assuming param is your list of parameter objects
  For Each p In param
    If p.user_test_name = description Then
      TextBox1.Text = p.test
      TestBox2.Text = p.test_name
      ...
      Exit For
    End If
  Next
End Sub

关于您的代码的一些注释。首先,您不能允许两个对象具有相同的user_test_name. parameters其次,如果您已经parameters在当前命名空间中调用了一个类(您这样做),则不能将其用作变量名。

于 2013-06-03T19:20:40.993 回答
0

有一个比这些更简单的解决方案 - 只需执行以下操作:

Dim i as Integer = ListBox1.SelectedIndex

然后您可以使用 i 作为原始对象列表的索引。

于 2013-06-03T21:24:56.453 回答