none
how to iterate controls in a form RRS feed

  • Question

  • Hi all,

    my form contains different type controls--some derived from user control and some are just windows' standard controls. how to iterate through each and change their location property's Y value?

    thanks,

    SZY

    Monday, January 28, 2019 9:58 PM

Answers

  • Hi

    Here is some code, taken from a larger project that illustrates what I think you want.

    This example code is from a project that has various controls at  Form level and in Panel(s). They vary, some Buttons, TextBoxes and Labels (Labels are all named "lb"xxxx)

    You can access all the control properties you want, such as Location, Size etc and change as needed.

      Private Sub cmdClear_Click(sender As Object, e As EventArgs) Handles cmdClear.Click
    
        ' cycle through all Controls at
        ' Form level and manipulate/use
        ' them according to type
        For Each c As Control In Controls
          If c.GetType() Is GetType(TextBox) Then
            c.Text = Nothing
          End If
    
          If c.GetType() Is GetType(Button) Then
            With c
              .Text = "Button Text"
              .Size = New Size(100, 40)
              .Font = New Font("Arial", 12, FontStyle.Bold)
            End With
          End If
        Next
    
        ' cycle through all Controls in
        ' Panel1 and manipulate/use
        ' them according to type
        Dim count As Integer = 1
        For Each c As Control In Panel1.Controls
          If c.GetType() Is GetType(Label) Then
            With c
              If .Name.StartsWith("lb") Then
                .Text = count.ToString("000")
                count += 1
              End If
            End With
          End If
        Next
      End Sub


    Regards Les, Livingston, Scotland

    • Marked as answer by sesquipedalian Tuesday, January 29, 2019 3:20 PM
    Monday, January 28, 2019 10:53 PM
  • Here is a conceptual example which sets the Y of the Location property of a control only if it has a Location property.

    For Each ctl As Control In Me.Controls
        If ctl.GetType().GetProperty("Location") IsNot Nothing Then
            ctl.Location = New Point(ctl.Location.Y + 10, ctl.Location.X)
        End If
    Next


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by sesquipedalian Tuesday, January 29, 2019 3:22 PM
    Monday, January 28, 2019 11:31 PM
    Moderator

All replies

  • Hi

    Here is some code, taken from a larger project that illustrates what I think you want.

    This example code is from a project that has various controls at  Form level and in Panel(s). They vary, some Buttons, TextBoxes and Labels (Labels are all named "lb"xxxx)

    You can access all the control properties you want, such as Location, Size etc and change as needed.

      Private Sub cmdClear_Click(sender As Object, e As EventArgs) Handles cmdClear.Click
    
        ' cycle through all Controls at
        ' Form level and manipulate/use
        ' them according to type
        For Each c As Control In Controls
          If c.GetType() Is GetType(TextBox) Then
            c.Text = Nothing
          End If
    
          If c.GetType() Is GetType(Button) Then
            With c
              .Text = "Button Text"
              .Size = New Size(100, 40)
              .Font = New Font("Arial", 12, FontStyle.Bold)
            End With
          End If
        Next
    
        ' cycle through all Controls in
        ' Panel1 and manipulate/use
        ' them according to type
        Dim count As Integer = 1
        For Each c As Control In Panel1.Controls
          If c.GetType() Is GetType(Label) Then
            With c
              If .Name.StartsWith("lb") Then
                .Text = count.ToString("000")
                count += 1
              End If
            End With
          End If
        Next
      End Sub


    Regards Les, Livingston, Scotland

    • Marked as answer by sesquipedalian Tuesday, January 29, 2019 3:20 PM
    Monday, January 28, 2019 10:53 PM
  • Here is a conceptual example which sets the Y of the Location property of a control only if it has a Location property.

    For Each ctl As Control In Me.Controls
        If ctl.GetType().GetProperty("Location") IsNot Nothing Then
            ctl.Location = New Point(ctl.Location.Y + 10, ctl.Location.X)
        End If
    Next


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by sesquipedalian Tuesday, January 29, 2019 3:22 PM
    Monday, January 28, 2019 11:31 PM
    Moderator