Link to home
Start Free TrialLog in
Avatar of Maria Torres
Maria Torres

asked on

How to pass a control to a function

I have four combo boxes named cboCert1, cboCert2, cboCert3, and cboCert4. I am trying to create a generic function routine which accepts the combo boxes as parameter.  However, I am having trouble getting the routine to work.  I'm unable to pass the variable which is defined as an object to the routine.

Below is an example of the code:

Dim objCert As obj
Dim bFlag As Boolean
Dim i As Integer

bFlag = False

For i = 1 To 4
        objCert = string.concat("cboCert", i, ".text")
        If bFlag = False Then
               bFlag = brdCrtSwap(objCert)
               If bFlag = True Then Exit For
        End If
Next i

thank you for your help.
Avatar of kaufmed
kaufmed
Flag of United States of America image

What is the method signature for brdCrtSwap?
Avatar of Maria Torres
Maria Torres

ASKER

brdCrtSwap is defined as:

Private Function brdCrtSwap(ByVal cert As String) As Boolean
Are you really using VB.Net 2003?  WinForms or WebForms?
Pass the control name as string and using the function you can access the control by using its name as the following:
Private Sub accessControl(ByVal ctrlName As String)
        Dim ctrl As Control = Me.Controls(ctrlName)
        ' Now you can access the control using the variable 'ctrl'
    End Sub

Open in new window

@Medo...that will work, but ONLY if the control is contained directly by the form itself.  If the controls in another container, like a Panel, then it won't be found.  *If all the controls are in the same container then we can obviously replace "Me" with the container name.

We can also search for the control to make it generic and handle a scenario where the controls are all in different containers...but the type of project and version of VB really dictates the easiest approach; thus my questions.

For 2003, you have to either use a manual recursive search or get a reference via Reflection.  For newer versions the Controls.Find() method is usually the easiest.

If the number of controls being acted upon is small and doesn't really change then we could also just build an ArrayList/List and populate it once at run-time from the Load() event of the Form.
Hi,

Do this:
Private Sub HandleCombos(ByVal combo As ComboBox)
        If (combo.Name = "ComboBox1") Then

        ElseIf (combo.Name = "ComboBox2") Then

        End If

        MessageBox.Show(combo.Name)

    End Sub

Open in new window


You can call it:
 
HandleCombos(ComboBox1)

Open in new window

 
while declaring the control remove them from Private  and make them public but rememrb they shouls be same class  or form
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@kambleamar

while declaring the control remove them from Private  and make them public but rememrb they shouls be same class  or form

That's a horrible recommendation. Sorry  : (
Thank you all for your help.