Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

Seabra

Preencher multiplos Combobox

Recommended Posts

Seabra

Olá

Necessito de preencher 20 comboboxs, Combobox1 a combobox20.

ComboBox1.Items.Add("Hora1")

ComboBox1.Items.Add("Hora2")

ComboBox1.Items.Add("Hora3")

ComboBox1.Items.Add("Hora4")

Há alguma forma de fazer isto sem repetir 20 vezes o codigo acima?

Cumprimentos

Seabra

Share this post


Link to post
Share on other sites
Tuntankamon

Podes fazer algo como isto...

  Dim lista() As String = {"1", "2", "3"}
        For Each ctrl As Control In Me.Controls
            If TypeOf ctrl Is ComboBox Then
                DirectCast(ctrl, ComboBox).Items.AddRange(lista)
            End If
        Next

Share this post


Link to post
Share on other sites
Seabra

O exemplo acima funciona na perfeição.

Neste contexto o que eu pretendo agora é dentro de um ciclo

For A=1 to (n)Comboboxs

Console.WriteLine(Combobox(a))

Next

utilizar o comboBox um a um. Ou seja seber qual o valor do combobox1, combobox2,... Isto é possível?

Cumprimentos

Share this post


Link to post
Share on other sites
Tuntankamon

A mesma lógica...

Dim cb As ComboBox
        For Each ctrl As Control In Me.Controls
            If TypeOf ctrl Is ComboBox Then
                cb = DirectCast(ctrl, ComboBox)
                'Se qeueres o selectedValue em cada uma
                Debug.WriteLine(cb.SelectedValue)
                'Se queres o item na posicao 0
                Debug.WriteLine(cb.Items(0).ToString)
                'Se queres correr todos os items
                For i As Integer = 0 To cb.Items.Count - 1
                    Debug.WriteLine(cb.Items(i).ToString)
                Next
            End If
        Next

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.