• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

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.
0
CarmenMTorres
Asked:
CarmenMTorres
  • 3
  • 2
  • 2
  • +3
1 Solution
 
käµfm³d 👽Commented:
What is the method signature for brdCrtSwap?
0
 
CarmenMTorresAuthor Commented:
brdCrtSwap is defined as:

Private Function brdCrtSwap(ByVal cert As String) As Boolean
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Are you really using VB.Net 2003?  WinForms or WebForms?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mohamed AbowardaSoftware EngineerCommented:
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

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
@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.
0
 
Asim NazirCommented:
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

 
0
 
kambleamarCommented:
while declaring the control remove them from Private  and make them public but rememrb they shouls be same class  or form
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
0
 
käµfm³d 👽Commented:
@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  : (
0
 
CarmenMTorresAuthor Commented:
Thank you all for your help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now