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.
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.
What is the method signature for brdCrtSwap?
ASKER
brdCrtSwap is defined as:
Private Function brdCrtSwap(ByVal cert As String) As Boolean
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
@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.
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:
You can call it:
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
You can call it:
HandleCombos(ComboBox1)
while declaring the control remove them from Private and make them public but rememrb they shouls be same class or form
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@kambleamar
That's a horrible recommendation. Sorry : (
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 : (
ASKER
Thank you all for your help.