user1819469
user1819469

Reputation: 13

VB looping complication

I have to come up with a program that shifts names in a text box a random amount of times. I have gotten everything up to the point where the names shifts a random amount of times. It only shifts its once, but my messagebox appears through the code as many times as the names should be shifting once i click ok. Does anyone know why the loop is not working for the name shifts. I was thinking maybe the messagebox needs to control the loop, but I have searched endlessly and cannot find how that would be done. Any suggestions or referrals to other sites would be nice thanks. My code is below.

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim RandomNumber As Integer
    Dim min, max As Integer
    Dim temp, temp2, temp3, temp4, temp5, temp6 As String
    Dim i As Integer

    min = 3
    max = 11

    Randomize()
    RandomNumber = Int((max - min + 1) * Rnd() + min)

    temp = n1.Text
    temp2 = n2.Text
    temp3 = n3.Text
    temp4 = n4.Text
    temp5 = n5.Text
    temp6 = n6.Text

    For i = 0 To RandomNumber - 1
        n1.Text = temp6
        n2.Text = temp
        n3.Text = temp2
        n4.Text = temp3
        n5.Text = temp4
        n6.Text = temp5
        MessageBox.Show("Shift " & i & " of " & RandomNumber & " complete")
    Next

End Sub

End Class

Upvotes: 1

Views: 172

Answers (3)

codingbiz
codingbiz

Reputation: 26386

Think the temp variables should also be in the loop

For i = 0 To RandomNumber - 1

temp = n1.Text
temp2 = n2.Text
temp3 = n3.Text
temp4 = n4.Text
temp5 = n5.Text
temp6 = n6.Text  

    n1.Text = temp6
    n2.Text = temp
    n3.Text = temp2
    n4.Text = temp3
    n5.Text = temp4
    n6.Text = temp5
    //MessageBox.Show("Shift " & i & " of " & RandomNumber & " complete")
Next

UPDATE

You can also do it this way using one temp variable

For i = 0 To RandomNumber - 1

  temp = n6.Text

  n6.Text = n5.Text
  n5.Text = n4.Text
  n4.Text = n3.Text
  n3.Text = n2.Text
  n2.Text = n1.Text
  n1.Text = temp

Next

Upvotes: 2

Guffa
Guffa

Reputation: 700212

Putting the assignment of the temporary variables inside the loop like codingbiz suggested would make it work, but you don't need a loop at all. If you put the strings in an array, you can calculate the position for them:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Dim RandomNumber As Integer
  Dim min, max As Integer
  Dim temp As New String(5)

  min = 3
  max = 11

  Randomize()
  RandomNumber = Int((max - min + 1) * Rnd() + min)

  temp(0) = n1.Text
  temp(1) = n2.Text
  temp(2) = n3.Text
  temp(3) = n4.Text
  temp(4) = n5.Text
  temp(5) = n6.Text

  n1.Text = temp((0 + RandomNumber) Mod 6)
  n2.Text = temp((1 + RandomNumber) Mod 6)
  n3.Text = temp((2 + RandomNumber) Mod 6)
  n4.Text = temp((3 + RandomNumber) Mod 6)
  n5.Text = temp((4 + RandomNumber) Mod 6)
  n6.Text = temp((5 + RandomNumber) Mod 6)

End Sub

Upvotes: 0

Fede
Fede

Reputation: 44038

Add this line before or after the MessageBox:

Application.DoEvents

Upvotes: 0

Related Questions