显示成员和价值成员

Display members and Value members

嗨,我是 VB 平台的新手,任何人都可以帮助我理解最后几行代码,这里我最后用 bold 突出显示,我不明白或感到困惑。显示成员和值编号有什么作用?

.. cmbcust 是组合框...

其中 客户 table 有以下字段。

**Customer_sname** **Customer_code** **Customer_fname**
nokia       1       nokia corp.
samsung     2       samsung corp.
sony        3       sony corp.
Micromax    4       Micromax India corp.

路过custval是诺基亚,三星,索尼

Public Function customfunc(ByVal custval As String) As DataSet
        Try
            Dim strSQL As String = "select * from customer  where  cust_sname  in (" & custval & ")"
            If Conn.State = ConnectionState.Open Then Conn.Close()
            Conn.Open()
            Dim Adap As New SqlDataAdapter(strSQL, Conn)
            Dim Ds As New DataSet
            Adap.Fill(Ds, "customer")
            ReadINICustomers = Ds
        Catch EXP As Exception
            MsgBox("Error Connecting to Server :" & EXP.Message, MsgBoxStyle.Critical)
        End Try
    End Function


   Public Sub Fillcustomer()
        Dim Lstcust() As String
        Dim Lstcust1 As String
        Lstcust1 = ""
        Lstcust1 = custINIval
        Dim Ds As New DataSet
        Ds = objData.ReadINICustomers(Lstcust1)
        cmbcust.DataSource = Ds.Tables("customer")
        cmbcust.DisplayMember = Ds.Tables("customer").Columns.Item("cust_sname").ToString().Trim()
        cmbcust.ValueMember = Ds.Tables("customer").Columns.Item("cust_code").ToString().Trim()
    End Sub

cmbcust.DisplayMember = Ds.Tables("customer").Columns.Item("cust_sname").ToString().Trim() cmbcust.ValueMember = Ds.Tables("customer").Columns.Item("cust_code").ToString().Trim()

使用任何 .NET 语言工作时,例如 VB.NET、the MSDN is your friend. It is the official resource for documentation regarding the languages and all of the types in the .NET Framework. In this case, you are asking about a couple of properties on the ComboBox control. The first thing you should do, then, is to search the MSDN for the ComboBox class. If you do so, you will find this article. It lists all of the members of the class and has a separate article explaining each one. If you scroll down through the list of properties, you will find links to articles for the DisplayMember property and the ValueMember 属性.

如那些文章所述,ComboBox 控件可以在其项目列表中包含任何类型的对象。如果您将一些简单的东西(例如字符串列表)放入 ComboBox,那么它显然很容易确定要在列表中显示什么以及要 return 显示什么作为其当前值。但是,当您在 ComboBox 中放置复杂的自定义对象时,这是一个更困难的命题。

默认情况下,它将显示列表中每个对象的 ToString 方法 return。但是,通过设置 DisplayMember 属性,您可以指示它使用列表中对象的特定成员(例如 PropertyFunction)而不是ToString 方法。为此,您可以将 DisplayMember 属性 设置为对象成员的字符串名称。然后它使用 reflection 在每个对象中按该名称查找成员并检索其值。

ValueMember 非常相似,但它不是控制显示的内容,而是控制 return 由 SelectedValue 属性 编辑的内容。默认情况下,SelectedValue 属性 只是 return 在列表中选择的整个对象。但是,通过设置 ValueMember,您可以指示它仅 return 对象中一个特定成员的值,而不是整个对象的值。