Thursday, June 24, 2021

[VB.net][example] Get all form controls

Example:

For Each ctrl In Form1.Control

    Trace.WriteLine(ctrl.getType().toString())

Next

where Form1 is your form name, if you omit it mean you loop for controls in current form.


Addition examples:

To do validation on all form-control which are in groupBox and are not label:

For Each ctrl In Control

  If GetType(ctrl) IS System.Window.Forms.Groupbox

    For Each ctrlItem In ctrl

        If NOT GetType(ctrlItem) IS System.Window.Forms.Label

          IF ctrlItem.Text.Length = 0 THEN

            MessageBox.Show("Please fill in all blanks.")

            ctrlItem.Focus()

          END IF

        End If

    Next

  End If

Next


Reference

https://stackoverflow.com/questions/199521/vb-net-iterating-through-controls-in-a-container-object

https://bytes.com/topic/net/answers/560738-determining-control-type-vb-net-runtime

https://stackoverflow.com/questions/11335529/going-through-controls-in-order

No comments :

Post a Comment