如何动态更改组合框显示成员
How to dinamically change Combo box display member
我的 VB 程序中有一个数据库链接到组合框。从组合框中选择名称时,它会自动用相关信息填充其他文本框。我现在添加了一个单选按钮,允许在组合框中切换名称和地址,允许用户根据自己的喜好进行搜索。
有人知道我可以在我的单选按钮 private sub
中放置一小段代码,这会在选中时更改组合框的显示成员吗?
谢谢
下面是如何切换显示成员的示例。它不一定具有设计意义,但它展示了如何去做。这是工作代码 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
这显示了如何操作。你需要弄清楚你的逻辑。
我的 VB 程序中有一个数据库链接到组合框。从组合框中选择名称时,它会自动用相关信息填充其他文本框。我现在添加了一个单选按钮,允许在组合框中切换名称和地址,允许用户根据自己的喜好进行搜索。
有人知道我可以在我的单选按钮 private sub
中放置一小段代码,这会在选中时更改组合框的显示成员吗?
谢谢
下面是如何切换显示成员的示例。它不一定具有设计意义,但它展示了如何去做。这是工作代码 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
这显示了如何操作。你需要弄清楚你的逻辑。