Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

combobox in a panel control

Status
Not open for further replies.

dudleys

Technical User
Apr 28, 2005
31
AU
Hi,

I have a panel with some controls, namely a combobox.

I want to find the selectedIndex property of the combo.
But I can't seem to work it out.

This is the code I have

Code:
   Public Function GetSelectedIndex(ByVal pnl As Panel, ByVal ctlName As String) As Integer

        Dim ctl As Control
        Dim cmb As ComboBox

        GetSelectedIndex = 0
        For Each ctl In pnl.Controls

            cmb = CType(ctl, ComboBox)
            If ctlName = ctl.Name Then
                GetSelectedIndex = cmb.SelectedIndex
                Exit For
            End If
        Next

    End Function

Does anyone no what I am doing wrong here?

Thanks
 
Couple of things.
1) Function isnt returning a value
2) Name compare doesnt check for upper/lower case matches.
Try the following

Code:
Public Function GetSelectedIndex(ByVal pnl As Panel, ByVal ctlName As String) As Integer

Dim ctl As Control
Dim cmb As ComboBox

GetSelectedIndex = 0
For Each ctl In pnl.Controls
    If TypeOf ctl Is ComboBox Then
         cmb = CType(ctl, ComboBox)
         If ctlName.ToLower = ctl.Name.ToLower Then
             GetSelectedIndex = cmb.SelectedIndex
             Exit For
         End If
    End If
Next

Return GetSelectedIndex

End Function


Sweep
...if it works dont mess with it
 
That does return the selected index correctly. For example, using:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer = GetSelectedIndex(Panel1, "ComboBox1")
    End Sub

    Public Function GetSelectedIndex(ByVal pnl As Panel, ByVal ctlName As String) As Integer

        Dim ctl As Control
        Dim cmb As ComboBox

        GetSelectedIndex = 0
        For Each ctl In pnl.Controls

            cmb = CType(ctl, ComboBox)
            If ctlName = ctl.Name Then
                GetSelectedIndex = cmb.SelectedIndex
                Exit For
            End If
        Next

    End Function
and selecting the second item in ComboBox1, i returns 1 which is correct.

I'm not sure why you have to loop through each control in the panel though as opposed to just referrring to the actual control itself?

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Sorry, I appeared to be typing at the same time as Sweep. I'm not sure what he means by point 1 as it does return a value (GetSelectedIndex = cmb.SelectedIndex) but he's probably hit the nail on the head with his second point.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Thanks guys seems to work fine now,not sure what I did.

 
One thing also is you say why don't I refer to the control itself.

I created the controls in the panel at runtime.

I tried to use this code

pnl.Controls("cmb1").selectedIndex

But get an error? It only seems to want an index integer.

How do I refer to cmb1 by name ?

 
Yes, Me.Controls does only accept an Integer so it would error. I din't know that you had created the controls at runtime which is why I asked why you didn't refer to it by name.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
It's not the only way to do it, but it certainly is one way of doing it.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top