I have a form in a WinForms application that has a Textbox called txtSupplier
, a button called btnSrchSuppliers
, a label called lblSuppName
, and a ListBox lstSuppliers
.
When the user clicks the button, it goes to a database and fetches a list of suppliers and lists them in the ListBox, selecting a supplier from the list fills the Textbox with his ID and the label with the name.
This code is used and fully working but I feel that it is not quite good.
Private Sub btnSrchSuppliers_Click(sender As Object, e As EventArgs) Handles btnSrchSuppliers.Click
Dim suppliers As New List(Of SupplierDTO)
lblSuppName.Text = String.Empty
If lstSuppliers.Visible Then
lstSuppliers.Hide()
Else
Try
lstSuppliers.ValueMember = "ID"
lstSuppliers.DisplayMember = "Description"
lstSuppliers.BringToFront()
suppliers = ItemBAL.SelectSuppliers(txtSupplier.Text.Trim()) 'Get Suppliers
If suppliers.Count = 0 Then
MsgBox("Supplier not found", MsgBoxStyle.Critical, "Supplier")
ElseIf suppliers.Count >= 1 Then
lstSuppliers.DataSource = suppliers
If suppliers.Count > 1 Then
lstSuppliers.Visible = True
Else
lstSuppliers.SelectedIndex = 0
lstSuppliers_Click(lstSuppliers, e)
End If
End If
Catch ex As Odbc.OdbcException
MsgBox(ex.Message, MsgBoxStyle.Critical, "sql error")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "general error")
End Try
End If
End Sub
and
Private Sub lstSuppliers_Click(lstSuppliers As ListBox, e As EventArgs) Handles lstSuppliers.Click
Dim selectedSupp As SMS.DTO.SupplierDTO
selectedSupp = lstSuppliers.SelectedItem
lstSuppliers.Visible = False
txtSupplier.Text = selectedSupp.ID
lblSuppName.Text = selectedSupp.Description
End Sub
What can be improved?