Vb.net 在visual basic中从对象列表中检索对象,并使用它填充文本/组合框

Vb.net 在visual basic中从对象列表中检索对象,并使用它填充文本/组合框,vb.net,object,Vb.net,Object,我的课程如下所示: 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导入的对象列

我的课程如下所示:

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导入的对象列表。列表中的用户\u测试\u名称将发送到列表框:

For Each parameters In param
  ' MsgBox(parameters.user_test_name)
  ListBox1.Items.Add(parameters.user_test_name)
Next
现在,当用户从列表中选择某个内容时,我希望该特定user\u test\u 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
现在,我很难从列表中找到具有所选名称的对象,并使用其属性填充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

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

Dim params As New Dictionary(Of String, parameters)

params.add(MyParameterObject.user_test_name, MyParameterObject)
然后


像这样的东西应该可以做到:

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\u test\u name
。其次,如果在当前名称空间中已经有一个名为
parameters
的类,则不能将
parameters
用作变量名(确实如此)。

与其将名称(字符串)添加到列表框中,不如将实际的实例添加到列表框中

首先,在类中重写ToString(),以便它在列表框中正确显示:

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
接下来,将每个实例添加到列表框:

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
Private子列表框1\u选择了dexchanged(由Val发送方作为System.Object,
ByVal e作为System.EventArgs)处理ListBox1.SelectedIndexChanged
如果ListBox1.SelectedIndex-1,则
Dim P As parameters=DirectCast(列表框1.SelectedItem,参数)
' ... 用“P”做点什么。。。
Debug.Print(P.user\u test\u name&“-->”和P.test)
如果结束
端接头

有一种比上述任何一种更简单的解决方案——只需执行以下操作:

Dim i作为整数=ListBox1.SelectedIndex


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

我也同意Ceres的观点,在这里,词典会更好地为您服务。它的代码更少,性能也会更好。感谢您的回复。为什么字典是解决这个问题的更好的选择呢?它降低了复杂性,您不需要对集合中的每个对象进行迭代的函数。此外,字典使用比for循环更有效的方法来检索您要查找的值。因此,随着对象列表的增加,您会看到更好的性能。旁注:如果
param
列表(参数)
,那么您只需使用
ListBox1.DataSource=param
设置列表框的数据源即可。
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