Link to home
Start Free TrialLog in
Avatar of s1555
s1555

asked on

Random maths symbol into textbox

How can i make a random maths symbol, plus, minus, divide, multiply appear into a text box at the click of a button.
Avatar of Vrenox
Vrenox

Not the best way maybe but the easiest I could think of.. put the math symbols in a hashtable paired with a number from 0 to 3 like so:

Private myHashTable As New HashTable
myHashTable.Add(0, "+")
        myHashTable.Add(1, "-")
        myHashTable.Add(2, "*")
        myHashTable.Add(3, "/")

then in the buttonclick handler all you have to do is the following:

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim myRandomNumGen As New Random
        MathSignText.Text = ""  'Clean up old value
        MathSignText.Text = myHashTable(myRandomNumGen.Next(4))
End Sub

This should do it


ASKER CERTIFIED SOLUTION
Avatar of roverm
roverm
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i liked roverm's style the best !! simple and does the work.. though could be more generalized

public sub printRandomChar()
        Randomize()
        dim chars as string = "+-/*"
        txtBox.Text = Mid(chars, Int(Rnd() * chars.length) + 1, 1)
end sub


so .. now, all you'd have to do is change the value of chars, and you'd have to remember to change the max. value of string.
Avatar of Bob Learned
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: roverm {http:#9858927}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer