2015-11-03 62 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 

這顯示瞭如何做到這一點。你需要制定出你的邏輯。