Pass control as parameter

0
Hello, I'm doing a reduction of some lines of code in vb.net, one of them is a search function that creates an AutoComplete from a database, the code that does this is as follows:

 Private Sub pesquisaNome(ByVal txtNome As TextBox)
    reconect()
    txtNome.AutoCompleteMode = AutoCompleteMode.Suggest
    txtNome.AutoCompleteSource = AutoCompleteSource.CustomSource

    strsql = "SELECT * FROM motorista WHERE motorista_nome LIKE '%" & txtNome.Text & "%'"
    Dim objCommand As New MySqlCommand(strsql, conn)
    dr_usuario = objCommand.ExecuteReader()

    While (dr_usuario.Read())
        txtNome.AutoCompleteCustomSource.Add(dr_usuario.Item("motorista_nome"))
    End While
End Sub

I have three different search fields, one by Name, another by RG and another by CPF, and each one has a code exactly like this, changing only 3 arguments, where clause (driver_name, driver_rg etc ... ), the text of the TextBox control (txtNome.text "driver name", txtRG.text "rg number" etc. and the txtNome control, txtRG etc ..., I decided to create a function and pass through these values: / p>

Private Sub search(clausula As String, campo As String, controle As Object)
    strsql = "SELECT * FROM motorista WHERE" & clausula & " LIKE '%" & campo & "%'"
    Dim objCommand As New MySqlCommand(strsql, conn)
    dr_usuario = objCommand.ExecuteReader()
    While (dr_usuario.Read())
        controle.AutoCompleteCustomSource.Add(dr_usuario.Item(campo))
    End While
End Sub

and the passage would look something like this:

search("motorista_nome",txtnome,txtnome.text)

And my question is just how to pass the control by parameter, or if you have another way to join those searches in a code only ??

Thank you in advance!

    
asked by anonymous 17.04.2015 / 21:31

1 answer

2

Try this:

Private Sub search(clausula As String, campo As String, ByRef controle As System.Windows.Forms.TextBox)
   strsql = "SELECT * FROM motorista WHERE " & clausula & " LIKE '%" & campo & "%'"
   Dim objCommand As New MySqlCommand(strsql, conn)
   dr_usuario = objCommand.ExecuteReader()
   While (dr_usuario.Read())
      controle.AutoCompleteCustomSource.Add(dr_usuario.Item(clausula ))
   End While
End Sub
    
07.05.2015 / 21:03