Nyx Assasin Nyx Assasin - 3 months ago 13
Vb.net Question

Generate 1 Letter in all Labels

Good Morning

I am creating an application that has 100

Labels
from
Label1
to
Label100
. My Target here is that all of that Labels must generate random letters in the alphabet no matter if it is repeated as long as its different.

Here is my code I tried.

Dim validchars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim sb As New StringBuilder()
Dim rand As New Random()

For i As Integer = 1 To 1
Dim idx As Integer = rand.Next(0, validchars.Length)
Dim randomChar As Char = validchars(idx)
sb.Append(randomChar)
Next i
Label1.Text = sb.ToString()
Label2.Text = sb.ToString()
Label3.Text = sb.ToString()
Label4.Text = sb.ToString()
Label5.Text = sb.ToString()
Label6.Text = sb.ToString()
Label7.Text = sb.ToString()
Label8.Text = sb.ToString()
'and so on until i reached Label100


But my output is this :(

enter image description here

Please ignore the other letters because i tried to code until
Label50


How can i achieve it? and is there other way to shorten up calling each label?

TYSM for future help

Answer

Here is the VB.NET version of the code provided by @maricatch. I like it.

Dim alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim random As New Random()

For i As Integer = 0 To 99
    Dim label As New Label()
    label.Text = alphabet(random.[Next](0, alphabet.Length)).ToString()

    flowLayoutPanel1.Controls.Add(label)
Next

This is the Convertor used online for converting C# code to VB.NET code for reference.

Comments