Results 1 to 3 of 3

Thread: how to loop throught all label and button controls of myPage

  1. #1

    Thread Starter
    Hyperactive Member fabianus's Avatar
    Join Date
    Sep 2004
    Location
    Paris
    Posts
    402

    Question how to loop throught all label and button controls of myPage

    I would like to loop throught all label and button controls of myPage and modifie their text property (in fact I want to translate the text depending on the language of the user).

    I tried this :
    VB Code:
    1. For Each myControl As Control In Page.Controls
    2.                 Dim test As String = myControl.ID
    3.                 ListBox1.Items.Add(myControl.ID)
    4.             Next

    but this does not give me the list of all controls.
    I thought that I could loop through all controls, test for each if it is a control or button and then change the property .text to a new value depending on their old value.

    Would to nice if anybody could give me a littel help...

    Thank you in advance!

    Fabian

  2. #2
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: how to loop throught all label and button controls of myPage

    You will need a recursive function.

    Here is what you need.

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Me.ListBox1.Items.Clear()
            For Each ctl As Control In Me.Controls
                GetControls(ctl)
            Next
    
        End Sub
    
        Private Function GetControls(ByVal ctl As Control)
            For Each ct As Control In ctl.Controls
                If ct.GetType Is GetType(Label) Then
                    Dim lbl As Label
                    lbl = CType(ct, Label)
    
                    Me.ListBox1.Items.Add("ID:" & lbl.ID & " Text:" & lbl.Text)
                    lbl.Text = "New Text"
    
                End If
    
                If ct.Controls.Count > 0 Then
                    GetControls(ct)
                End If
            Next
    
        End Function
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  3. #3

    Thread Starter
    Hyperactive Member fabianus's Avatar
    Join Date
    Sep 2004
    Location
    Paris
    Posts
    402

    Smile Re: how to loop throught all label and button controls of myPage

    juste perfect - exactly what I was looking for ... and even prepared for consumption. I had nothing to do.

    Thank you very much!

    Fabian

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width