Question randomly choosen number to show in label text!

danter

Member
Joined
Mar 24, 2010
Messages
13
Programming Experience
Beginner
hi guys,
i have a loop in here what suppose to add a rondomly choosen number and show it on the label .text increase every time by the the same number when appear. It does not do it correctly. that is the code:


Private Sub licz()
Dim pbs() As PictureBox = {PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5}
Dim las() As Label = {Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8, Label9, Label10, Label11, Label12, Label13, Label14, Label15}
Dim r As New Random
Dim rr As Integer


For Each p In pbs
rr = r.Next(1, 7)
If p.BorderStyle <> BorderStyle.Fixed3D Then
p.Image = Image.FromFile(Application.StartupPath + "\Dice" + rr.ToString + ".png")
las(rr - 1).Text = rr
las(rr - 1).Text = CStr(CInt(las(rr - 1).Text + (rr)))' that should work but it can not convert it to ''string''
End If


Next
End Sub​

I would be grateful, chears friends.
 
danter said:
that should work but it can not convert it to ''string''
The problem with that code, first and foremost, seems that you don't know the difference between the two concatenation operators + and &. The article I linked to explains how these operators work. When you've read the article you should be able to resolve the problem in that code line. If not try to break it down in several steps, use more temp variables, comment the code to explain to yourself and others what each step is supposed to do.
 
HI John,

I know the diffrence between + and & but I have not read any article form you, as i did not know where to look for it, Now i am guessing you sent me a title so i should fing it.
Thank you very much

I hope It will help
OK, You sent me a link to Microsoft web thanks
 
Hi John,
I have sorted my problem. But can you advice me how i can make this loop to add only 1 to label1.text if there picture box contains only 1 image.dice1 but if there is more add adequately figure: for instance in the picture boxe random function show 3 dice1 so how can i add 1 three times? I would be gratefull that is my loop:
Private Sub licz()
Dim pbs() As PictureBox = {PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5}

Dim r As New Random
Dim rr As Integer
Dim d As String

For Each p In pbs
rr = r.Next(1, 7)

If p.BorderStyle <> BorderStyle.Fixed3D Then

p.Image = Image.FromFile(Application.StartupPath + "\Dice" + rr.ToString + ".png")
d = rr

las(rr - 1).Text = CStr(CInt(d) + rr)
End If
Next
End Sub
 
Oh, you were adding numbers? I saw the Text being added with + and figured you were adding strings, which cause problems when one of the operands is a number. This is a problem to read when you neither explicitly convert the string to a number, or the number to a string. It can also make for undesirable results and runtime errors.
But can you advice me how i can make this loop to add only 1 to label1.text if there picture box contains only 1 image.dice1 but if there is more add adequately figure: for instance in the picture boxe random function show 3 dice1 so how can i add 1 three times?
I'm not sure if I understand your problem description. If you want to add the numbers of all pictureboxes you can for example store the number in the Picturebox.Tag property, then loop/query for that sum.
 
hi john,
thanks a lot,
I have solved my problem, now new one has arrived. I have got 2 arrays: 1 picure box
2 image containing a 6 photos how can i work on both of them in the same time:
i need to check if photo 2 ("dice2)from image array has been used in more then 1 picture box.
please

thanks

daniel
 
I will be the same procedure as summing them, loop/query for a specific number. When I say 'query' I'm talking about Linq expressions. Here are two examples:
VB.NET:
Dim sum = pbs.Sum(Function(x As PictureBox) CInt(x.Tag))
Dim twos = pbs.Count(Function(x) CInt(x.Tag) = 2)
The 'sum' here is the sum of all values, 'twos' is the count of two-dices. Both result can also be achieved using a variable to keep the result and a loop over all boxes to compare/count.
 
hi John,
Thanks for it,
I am beginner in programing and i have never heard about the function,
how are we using it, what does the function sum up?
You had put an x as picture box but how can i get through it to the image?
thank you for your support
 
how are we using it, what does the function sum up?
It sums the numbers in each box Tag property. For example is a box is displaying a two-dice it could be useful to keep the number 2 in Tag for later reference, otherwise it would be difficult to figure out that box holds a 2 ;)
 
Hi John,
do not be angry with me but how can i check the result of the sum?
that is a part of my code, working fine with the labels and showing the result correctly in there,the problem statrs when i want to count a dice2 in the pictureBoxe. So if it appeared 3 times i have to locate 20 pts in the labels "three in kind"
Dim r As New Random
Dim rr, sumu As Integer
For Each p In pb
rr = r.Next(1, 7)
sumu += 1
If p.BorderStyle <> BorderStyle.Fixed3D Then
p.Image = dic(rr)

If las(rr - 1).Text = "" Then
las(rr - 1).Text = rr
ElseIf las(rr - 1).Text = rr Then
las(rr - 1).Text = (CInt(las(rr - 1).Text)) + rr
ElseIf las(rr - 1).Text = rr + rr Then
las(rr - 1).Text = (CInt(las(rr - 1).Text)) + rr
ElseIf las(rr - 1).Text = rr + rr + rr Then
las(rr - 1).Text = (CInt(las(rr - 1).Text)) + rr
End If
End If
If p.BorderStyle = BorderStyle.Fixed3D Then
Dim sum = pb.Sum(Function(x As PictureBox) CInt(x.Tag))
End If
Next
End Sub
 
Back
Top