我的VB程序中有一个数据库链接到一个组合框。从组合框中选择名称时,它将自动用其他文本框填充相关信息。我现在正在添加一个单选按钮,以便切换姓名&组合框内的地址,允许用户根据自己的喜好进行搜索。
有人知道我可以放在我的单选按钮private sub
中的一小段代码,这会在选择后更改组合框的显示成员吗?
由于
答案 0 :(得分: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
这说明了如何做到这一点。你需要弄清楚你的逻辑。