Tuesday, August 4, 2015

Get all controls vb.net


Get controls in form

Hello! Welcome to more one tutorial. Today, I will show you different situations to list controls.

How to create a project in Visual Studio?

Exists many ways to create a new project in VS. For example, when you open your VS program, in the Start Page you will see the option New Project ... 
If you can´t see the Start Page you can also click in File > New Project > Select Visual Basic > Windows Forms Application
Rename your project name if you want and click OK.
It´s easy! 

Get all controls in form

To get all controls in your form you just have to use:
        For Each cControl As Control In Me.Controls
            MsgBox(cControl.Name)
        Next
If you want to get all controls in a Panel or FlowLayoutPanel this method also works:
        For Each cControl As Control In Me.Panel.Controls
            MsgBox(cControl.Name)
        Next

Get type of controls

If you just want to get all buttons in your form or other kind of control use this:
        For Each cControl As Control In Me.Controls.OfType(Of Button)()
            MsgBox(cControl.Name)
        Next

Get controls by string name

To get controls by string name, I mean, get a control that you know that have a specific name use this:
        Dim btn As Control = CType(Me.Controls("BUTTON2"), Button)
       MsgBox(btn.Name)

Now imagine that you want to get all controls that the name starts by “Some”. You don´t know the end of the name of the control. Can be 1,2,3 …

If you know the type use this:
        For Each cControl As Control In Me.Controls.OfType(Of Button)()
            If (UCase(cControl.Name) Like "SOME*") Then
                MsgBox(cControl.Name)
            End If
        Next
If you do not know …
        For Each cControl As Control In Me.Controls
            If (UCase(cControl.Name) Like "BUTTON*") Then
                MsgBox(cControl.Name & "Like")
            End If
        Next
Thanks for read!
Any questions leave comment or send e-mail to antivirusdownloadfullversion@outlook.com


No comments:

Post a Comment