Working With ADOX and Combo Box Control - More on Combo Box
(Page 5 of 5 )
We checked if the Combo Box had some items, and we checked the Count of the items. Since we have more than the zero items, then we will ask the Combo Box to select the first item, and we specify that the SelectedIndex should be to the first item (Zero based)
Very true, that should not be it, when the user selects an employee from the Combo Box, then we need to display the employee information such as the name and the salary.
Mind you that we bound the control to a collection of employees.
Couldn’t be more clever -- what we have in the “Items” property of the “cboEmployees” is nothing but a collection of “Employee” object.
So let's get into the “cboEmployees_SelectedIndexChanged” to get a better understanding of what we are talking about.
Private Sub cboEmployees_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
cboEmployees.SelectedIndexChanged
Dim oEmployee As Employee
Try
'Get the item that was selected in the combo box so
that we can load
'the textboxes with information from the Employee that
was selected
If cboEmployees.SelectedIndex > -1 Then
oEmployee = cboEmployees.SelectedItem
txtName.Text = oEmployee.Name
txtSalary.Text = oEmployee.Salary
End If
Catch oException As Exception
'Handle the Exception here
MessageBox.Show(oException.ToString)
End Try
End Sub
1) First we need to check that the Combo Box has items.
2) If true, then we can set the selected item to the oEmployee Object, (remember that the items of the cboEmployees are nothing but a collection of Employee?)
3) Since we have a valid Employee object, then we can use its properties such as the “Name” and the “Salary”.
That was a quick implementation on how to use the new .Net Combo Box. It is so efficient specially when working between tiers. And we also hold to the Collections design pattern, and the object oriented concepts.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |