We help IT Professionals succeed at work.

How can I refer to a ComboBox by its string name?

How can I refer to a ComboBox by its string name?

Comment
Watch Question

option 1
Dim c As ComboBox =  Me.Controls("ComboBoxName")

or

option 2
Dim matches() As Control
Dim c As ComboBox
matches = Me.Controls.Find("ComboBoxName", True)
If matches.Length > 0 Then
    c = matches(0)
End If
Moshe Singerdeveloper

Author

Commented:
thanks a milion
Moshe Singerdeveloper

Author

Commented:
i tried to use
Dim c As ComboBox =  Me.Controls("ComboBoxName")
this works fine

and then

c.Items.Clear()
Or
c.Items.Add(RsItm.Fields("itm").Value)
it givs a error
Moshe Singerdeveloper

Author

Commented:
the exsact error masseg
Object reference not set to an instance of an object.
I checked, no error .
May be the Combox name is wrong?
Please check
Moshe Singerdeveloper

Author

Commented:
i tested like this
I have mane combo boxes with the nama cmbo1, cmbo2 , cmbo3, cmbo4  
index have the number      
  Dim ComboStr As String = "cmbo" & index
        Dim c As ComboBox = Me.Controls(ComboStr)
        c.Items.Clear()

and it is making this error masseg
Object reference not set to an instance of an object.
I tried following code, that's working fine

Dim strNo As Integer = 1
Dim comboName As String = "ComboBox" & strNo
Dim c As ComboBox = Me.Controls(comboName)
c.Items.Clear()
c.Items.Add("abc")
Moshe Singerdeveloper

Author

Commented:
1.    Private Sub cbo15_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbo15.Enter
2.       'Call CboEnFnc(15)
3.        Dim strNo As Integer = 15
4.        Dim comboName As String = "cbo" & strNo
5.        Dim c As ComboBox = Me.Controls(comboName)
6.        c.Items.Clear()    
7.        c.Items.Add("abc")
8.    End Sub
by line # 6
i get this error masseg   Object reference not set to an instance of an object.
maybe you have some refrence in the project thet helps