Getting a random Integer
Use the Random class instead of the Rnd function to get a random Integer within a specified range in the Random.Next(Int32, Int32) method. Declare a class variable of Random type:
Private ReadOnly rand As New Random
Finding a range of controls
This code snippet iterates over the Controls collection of the container, returns - if any - the Label controls where their names are equals to a range of names starts from label1 to label16, and finally, assign a random Integer to their Text properties:
Private Sub TheCaller()
For Each lbl In Controls.OfType(Of Label).
Where(Function(x) Enumerable.Range(1, 16).
Any(Function(y) x.Name.ToLower.Equals($"label{y}")))
lbl.Text = rand.Next(1, 100).ToString
Next
End Sub
Just in case, if the Label controls are hosted by different containers, then you need a recursive function to get them:
Private Function GetAllControls(Of T)(container As Control) As IEnumerable(Of T)
Dim controls = container.Controls.Cast(Of Control)
Return controls.SelectMany(Function(x) GetAllControls(Of T)(x)).
Concat(controls.OfType(Of T))
End Function
And call it as follows:
Private Sub TheCaller()
For Each lbl In GetAllControls(Of Label)(Me).
Where(Function(x) Enumerable.Range(1, 16).
Any(Function(y) x.Name.ToLower.Equals($"label{y}")))
lbl.Text = rand.Next(1, 100).ToString
Next
End Sub