2

I want to make a random function here, so when i press Button3 it's gonna replace the number "star.9902" to a completely new random number, Without replace the text "star." and then show up in Textbox1. But right now everything is getting replaced including the text.

I'm pretty new to coding so it would be great if you guys can help me out. vb13.

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

    If TextBox1.Text.Contains("star.9902") Then
        TextBox1.Text = TextBox1.Text.Replace("star.9902", "star. 'Here goes the random numbers")

    End If
    Dim rand As New Random

    Dim letter As String = ""

    For i = 0 To 3

        letter = letter & ChrW(rand.Next(Asc("0"), Asc("9") + 1))

    Next

    TextBox1.Text = letter


End Sub
Richard
  • 106,783
  • 21
  • 203
  • 265
Fredrik
  • 33
  • 4

3 Answers3

0

First, reuse the same Random instance, the avoids the same sequence being generated repeatedly. Outside the sub:

private shared rng as Random = new Random()

Second, to remove the digits a regular expression is by far the easiest way, again outside the function:

' Match one or more digits at the end of the input
private shared digitMatcher = new Regex("\d+$")

then in the function:

dim t = Textbox1.Text
t = digitMatcher.Replace(t, "")

and then append the new values, there I'm just generating a number between 0 and 9999 and formatting with leading digits:

t &= (rng.Next(0, 10000)).ToString("0000")

and finally:

Textbox1.Text = t
Richard
  • 106,783
  • 21
  • 203
  • 265
0

Im not sure if i follow, again im new to this so. I guess this is wrong?

If TextBox1.Text.Contains("star.9902") Then TextBox1.Text = TextBox1.Text.Replace("star.9902", "star. 'Here goes the random numbers")

    End If

    Private Shared rng As Random = New Random()


    Private Shared digitMatcher = New Regex("\d+$")
    Dim t = TextBox1.Text
    t = digitMatcher.Replace(t, "")

    t &= (rng.Next(0, 10000)).ToString("0000")

    TextBox1.Text = t

End Sub
Fredrik
  • 33
  • 4
0

Could you do something like this?

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim tempString as String
    If TextBox1.Text.Contains("star.9902") Then
        tempString = TextBox1.Text.substring(0,TextBox1.Text.IndexOf(".")+1)
    End If

    Dim rand As New Random

    Dim letter As String = ""

    For i = 0 To 3

        letter = letter & ChrW(rand.Next(Asc("0"), Asc("9") + 1))

    Next

    TextBox1.Text = tempString & letter


End Sub

The reason yours replaced everything was because you set the TextBox1.Text to letter which you generated solely in the for loop (and didn't add the 'star.' to).

This will work for any word ending with a '.' (and 4 numbers after it). You could easily change it to have any amount of numbers after it as well.

Sastreen
  • 597
  • 4
  • 13
  • Ahaaaa! now i see :) that worked! Thank you! But i also have another question. What should the code look like if i need to replace both letters and numbers instead of only numbers? – Fredrik Jun 05 '15 at 12:34
  • What would you like to replace the letters with? More numbers or letters? – Sastreen Jun 05 '15 at 12:36
  • I was thinking of replacing the "9902" to both random numbers and letters. like this: "star.j28mp0t4e " – Fredrik Jun 05 '15 at 12:57
  • Just put a random character generator in your for loop as well. See this: http://stackoverflow.com/questions/15725962/how-do-i-make-a-random-character-generator-vb-net This will generate both numbers and letters – Sastreen Jun 05 '15 at 13:04
  • By the way, if this solution works for you, it'd be great if you could accept it as the answer :) – Sastreen Jun 05 '15 at 13:05