Get controls in form
Hello! Welcome to more one tutorial. Today, I will show you different situations to list controls.
How to crte a project in Visual Studio?
Exists many ways to crte a new project in VS. For example, when you open your VS program, in theStart Pageyou will see the optionNewProject ...
If you can´t see the Start Page you can also click inFile > New Project > Select Visual Basic > Forms Appliion
Rename your project name if you want and clickOK.
It´s sy!
Get all controls in form
To get all controls in your form you just have to use:
For ch 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 ch 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 ch 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 mn, 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 ch 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 ch cControl As Control In Me.Controls
If (UCase(cControl.Name) Like "BUTTON*") Then
MsgBox(cControl.Name & "Like")
End If
Next
Thanks for rd!
Any questions lve comment or send e-mail to hyrokumata@outlook.com
No comments:
Post a Comment