Dunkey
Dunkey

Reputation: 1922

VB.net random position of numbers in textboxes

So this is just a continuation of my question VB.Net grade distribution exercise

But what I wanna do now is swap or randomize their positions upon button click.

So this is what I'm working on:

    If txtGrade.Text <> "" Then
        If CDbl(txtGrade.Text) Then
            txtA.Text = (CDbl(txtGrade.Text) * 40) / 100
            txtB.Text = (CDbl(txtGrade.Text) * 20) / 100
            txtC.Text = (CDbl(txtGrade.Text) * 30) / 100
            txtD.Text = (CDbl(txtGrade.Text) * 10) / 100
        End If
    End If

UPDATE: I put this above code upon txtGrade_TextChanged event

But I really don't have an idea how to randomize their position. Any ideas? Thanks.

Upvotes: 0

Views: 1182

Answers (2)

Karl Anderson
Karl Anderson

Reputation: 34846

Upon clicking your button, you need to store the values you want to swap/randomize/shuffle into a List(Of T), like this:

Dim ValuesToShuffle = New List(Of String)
ValuesToShuffle.Add(txtA.Text)
ValuesToShuffle.Add(txtB.Text)
ValuesToShuffle.Add(txtC.Text)
ValuesToShuffle.Add(txtD.Text)

Now you are going to loop through the values to shuffle and generate a random number each time and move value around, like this:

Dim rand = New Random()

For counter = 0 to ValuesToShuffle.Count- 1
    Dim n = rand.Next(counter + 1)
    Dim temp = ValuesToShuffle(counter)
    ValuesToShuffle(counter) = ValuesToShuffle(n)
    ValuesToShuffle(n) = temp
Next

Finally, put the shuffled values back into the textboxes, like this:

txtA.Text = ValuesToShuffle(0)
txtB.Text = ValuesToShuffle(1)
txtC.Text = ValuesToShuffle(2)
txtD.Text = ValuesToShuffle(3)

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460108

Use the Random class: (edited to show a more concise version)

Dim grade As Double
If Double.TryParse(txtGrade.Text, grade) Then
    Dim values = {(grade * 40 / 100).ToString(), (grade * 20 / 100).ToString(), (grade * 30 / 100).ToString(), (grade * 10 / 100).ToString()}
    FillRandomControlText({txtA, txtB, txtC, txtD}, values)
End If

with this method:

Shared Sub FillRandomControlText(txtControls As IList(Of TextBox), values As IList(Of String))
    Dim rnd = New Random()
    Dim txt As TextBox
    Dim selectedControls = New List(Of Control)(txtControls.Count)
    For i As Int32 = 0 To txtControls.Count - 1
        Do
            txt = txtControls(rnd.Next(txtControls.Count))
        Loop While selectedControls.Contains(txt)
        selectedControls.Add(txt)
        txt.Text = values(i)
    Next
End Sub

Dim grade As Double Dim rnd = New Random() Dim controls = {txtA, txtB, txtC, txtD} Dim selectedControls = New List(Of Control) Dim txt As TextBox

If Double.TryParse(txtGrade.Text, grade) Then
    Do
        txt = controls(rnd.Next(controls.Length))
    Loop While selectedControls.Contains(txt)
    selectedControls.Add(txt)
    txt.Text = (grade * 40 / 100).ToString()
    Do
        txt = controls(rnd.Next(controls.Length))
    Loop While selectedControls.Contains(txt)
    selectedControls.Add(txt)
    txt.Text = (grade * 20 / 100).ToString()
    Do
        txt = controls(rnd.Next(controls.Length))
    Loop While selectedControls.Contains(txt)
    selectedControls.Add(txt)
    txt.Text = (grade * 30 / 100).ToString()
    Do
        txt = controls(rnd.Next(controls.Length))
    Loop While selectedControls.Contains(txt)
    txt.Text = (grade * 10 / 100).ToString()
End If

Upvotes: 2

Related Questions