2015-11-03 59 views
1

我有一个数据库在我的VB程序链接到组合框。当从组合框中选择一个名称时,它会自动填充其他文本框与相关信息。我现在添加一个单选按钮,允许在组合框内切换名称&地址,以便用户根据自己的偏好进行搜索。如何dinamically更改组合框显示成员

有人知道我可以放在我的单选按钮private sub中的一小段代码,它会在选择时更改组合框的显示成员吗?

由于

+3

你尝试过什么吗? – Caveman

+0

以与第一次相同的方式更改它。或者显示代码如何设置它 – Fabio

+0

此刻我有我的组合框和文本框设置不使用代码我只是简单地将我的访问数据库拖到VB然后简单地将它从我的引用拖到我的应用程序,然后使其自己的文本框。我做了我的单选按钮,我知道数据库和显示成员被称为只是想知道我必须放入单选按钮private sub中的If语句中。 –

回答

0

下面是示例的如何切换显示构件。这并不一定使设计有意义,但它展示了如何去做。这是工作代码BTW

Public Class Vendor 
    Public Property Id As Integer 
    Public Property Name As String 
    Public Property Address As String  
End Class 
. . . . . 
' Form constructor 
Dim listOfVendors As New List(Of Vendor)() 
listOfVendors.Add(New Vendor() With {.Address = "A1", .Id = 1, .Name = "Name1"}) 
listOfVendors.Add(New Vendor() With {.Address = "A2", .Id = 2, .Name = "Name2"}) 
listOfVendors.Add(New Vendor() With {.Address = "A3", .Id = 3, .Name = "Name3"}) 

cboVendors.ValueMember = "Id" 
cboVendors.DisplayMember = "Name" 
cboVendors.DataSource = listOfVendors 

. . . . . 
' Assume SearchOptionChanged is handler for your radio buttons of the same group 
Pivate Sub SearchOptionChanged(sender As Object, e As EventArgs) Handles rbSearchbyName.CheckedChanged, rbSearchbyAddress.CheckedChanged 

    Dim rb As RadioButton = CType(sender, RadioButton) 
    If rb.Name = "rbSearchbyName" AndAlso rb.Checked Then 
     cboVendors.DisplayMember = "Name" 
    Else If rb.Name = "rbSearchbyAddress" AndAlso rb.Checked Then 
     cboVendors.DisplayMember = "Address" 
    Else 
     ' put your logic here 
    End If 

End Sub 

' Getting item 
Private Sub FillForm() 
    ' Cool thing about this style is, now you can fill text boxes with data 
    Dim v As Vendor = TryCast(cboVendors.SelectedItem, Vendor) 
    If v Is Nothing Then 
     MessageBox.Show("No Vendor selected") 
    Else 
     txtName.Text = v.Name 
     txtAddress.Text = v.Address 
     lblId.Text = v.Id 
    End If 

End Sub 

这显示了如何做到这一点。你需要制定出你的逻辑。