|
-
Apr 7th, 2009, 06:14 PM
#1
Thread Starter
New Member
Textbox with a mind of its own
Hi, I'm using VB.NET 2008 Express Edition and I'm trying to create a simple program that will roll a number of dice I choose. It does this by generating a random number within a specified range(I'm rolling 3-sided dice in this case so I'm generating numbers between 1 and 3) and adding each number to a textbox to create a total of all the dice rolled so far. This is my code:
Code:
Private Sub butRoll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butRoll.Click
Dim RandNum, i As Integer
Dim RandomClass As New Random()
'if at least one dice is rolled
If txtD3.Text > 0 Then
For i = 0 To txtD3.Text 'roll the dice and add to total
'generate a number > or equal to 1 and < 4
RandNum = RandomClass.Next(1, 4)
txtLastRoll.Text = RandNum 'last number generated
txt1.Text = txtTotal.Text 'whats the value of txtTotal?
'add latest dice roll to total
txtTotal.Text = txtTotal.Text + RandNum
txt2.text = txtTotal.Text 'whats the value of txtTotal now?
Next i 'roll the next dice
End If
End Sub
Now the problem I am having is that although my random number generator is generating the right numbers (confirmed by checking the txtLastRoll textbox), when I add it to txtTotal, it seems to be getting another number from somewhere and adding that to my random number then adding it to the total and I have no idea how this is possible! txtTotal always starts as 0 but txt1 is not showing that. Normally I would just set txtTotal to 0 there but if I do that, I won't be able to add the numbers together. I've tried finding a pattern in the mystery numbers appearing in txtTotal but they just seem to be random. Any ideas would be great as this has me completely stumped.
Thanks in advance,
Mooncinder
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|