Nyx Assasin
Nyx Assasin

Reputation: 141

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

Upvotes: 0

Views: 877

Answers (4)

Andrew
Andrew

Reputation: 7880

Option 1, with your existing form:

I would have created the labels in the code and not in the designer, but if you already have them, you can do this:

private void Form1_Load(object sender, EventArgs e)
{
    var labels = this.Controls.OfType<Label>();
    var rnd = new Random();

    foreach (var label in labels)
    {
        label.Text = ((char)(rnd.Next(26) + 'A')).ToString();
    }
}

Simply loop through all the form's labels (you may filter them if necessary) and assign each one a random letter. And that's all. No need to use StringBuilders or an array with the letters.

I also used Convertor to turn it into VB, I hope it works:

Private Sub Form1_Load(sender As Object, e As EventArgs)
    Dim labels = Me.Controls.OfType(Of Label)()
    Dim rnd = New Random()

    For Each label As var In labels
        label.Text = CChar(rnd.[Next](26) + "A"C).ToString()
    Next
End Sub

Option 2, starting from scratch:

Finally, based on mariocatch's answer, which uses a FlowLayoutPanel, I suggest you do this:

  1. Start with an empty form.
  2. Add a Panel.
  3. Set its Dock property to Bottom.
  4. Add a Button inside.
  5. Go to its Anchor property and deselect Top and Left (nothing selected).
  6. Set the panel's height and center the button horizontally.
  7. Add a FlowLayoutPanel in the middle of the form.
  8. Set its Dock property to Fill.

And nothing else there. Then use this code:

Private Sub Form1_Load(sender As Object, e As EventArgs)
    Dim rnd = New Random()

    For i As Integer = 0 To 49
        Dim label = New Label()
        label.Width = 20
        label.Text = CChar(rnd.[Next](26) + "A"C).ToString()
        Me.flowLayoutPanel1.Controls.Add(label)
    Next
End Sub

After this I think you can adjust all the details without any issue.

Upvotes: 2

vivek
vivek

Reputation: 1605

Here is the VB.NET version of the code provided by @mariocatch. 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.

Upvotes: 1

mariocatch
mariocatch

Reputation: 8703

Is this what you're going for (c#)? Basically, creating the Labels on the fly, and adding them to a FlowLayoutPanel for stacking and positioning.

var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random random = new Random();

for (int i = 0; i < 100; i++)
{
    Label label = new Label();
    label.Text = alphabet[random.Next(0, alphabet.Length)].ToString();

    flowLayoutPanel1.Controls.Add(label);
}

VB.NET:

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

Upvotes: 2

VortixDev
VortixDev

Reputation: 1013

Assuming labels up to 50 are the ones with the "F"s:

You've determined the value of sb once you left the loop. You're then taking that value and setting that to the text of each label, which is why it wouldn't work. You've also created a loop starting at 1 and ending at 1, which is a pretty pointless loop as it only runs once. If you modify the loop to generate 100 characters in the stringbuilder and then set the nth label to the nth character of the stringbuilder, this should work.

Upvotes: 0

Related Questions