Jump to content
Chamuanza

Limpar Labels

Recommended Posts

Chamuanza

Oi Pessoal

Agradeço uma ajudinha

Tenho 12 Labels (Label3..................Label14) e queria poder limpar todas com um comando, estou a tentar com este código mas não funciona

Private Sub butSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSair.Click

        Dim Arraylabel(12) As Label

        For i As Integer = 0 To 11
            ' criar um novo objecto do tipo Label para a posição i
            Arraylabel(i) = New Label()

            With Arraylabel(i)
                .Name = "Label" & i + 3
                .Text = ""
            End With
        Next i


        Me.Close()
    End Sub

Share this post


Link to post
Share on other sites
ribeiro55

Estás a criar novas instâncias de label que não vão, nunca, ser referência para os labels que já lá tens.

Podes fazer a coisa de muitas formas. Eis uma:

Fora de qualquer sub, ao nível do form alvo:

Private Arraylabel(11) As Label

No teu form load, onde tens as labels:

Arraylabel(0) = Label1
Arraylabel(1) = Label2
Arraylabel(2) = Label3

.......por aí fora até referenciares todas.

E depois usas mais ou menos esse iterador, sem a parte das novas instâncias, onde for necessário:

For Each Lbl As Label In Arraylabel
    Lbl.Text = String.Empty
Next


Sérgio Ribeiro


"Great coders aren't born. They're compiled and released"
"Expert coders do not need a keyboard. They just throw magnets at the RAM chips"

Share this post


Link to post
Share on other sites
jpaulino

Também podes fazer algo como:

        For x As Integer = 3 To 14
            Dim ctrl() As Control = Me.Controls.Find("Label" & x.ToString(), True)
            If ctrl.Length = 1 Then
                Dim lbl As Label = DirectCast(ctrl(0), Label)
                lbl.Text = String.Empty
            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

×
×
  • Create New...

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.