By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,830 Members | 2,182 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,830 IT Pros & Developers. It's quick & easy.

Random labels.text

cindy2
P: 35
Hi everyone,

I have 4 textboxes on form1. On runtime the user will be asked to fill in names. After that, I want these names placed RANDOMLY on 4 labels (form2). How should I do that? I am trying it for ages but I still don't have a clue to get them random.

Please help me.

Cindy
Sep 15 '07 #1
Share this Question
Share on Google+
11 Replies


VBPhilly
P: 95
Hi everyone,

I have 4 textboxes on form1. On runtime the user will be asked to fill in names. After that, I want these names placed RANDOMLY on 4 labels (form2). How should I do that? I am trying it for ages but I still don't have a clue to get them random.

Please help me.

Cindy
Random is about numbers in vb6.

You should represent your labels as a number, and ask for one of the numbers in a random number generator function.

So, for each textbox. You could say, "RandomNumberGenerator function, give me a number between 1 and 4."

For example,
It returns 3.

See if the 3 label has a value, if not put the name there (from the textbox).

If it does have a value, keep askin the RandomNumberGenerator to give you another number until you get a clean label to populate.

keep going until you get your labels filled (all 4 of them).

Your technical challenge is with the Random generator function. You'll need to make it give you a value of only between 1 and 4. Let me know if you need help with this.
Sep 15 '07 #2

cindy2
P: 35
Random is about numbers in vb6.

You should represent your labels as a number, and ask for one of the numbers in a random number generator function.

So, for each textbox. You could say, "RandomNumberGenerator function, give me a number between 1 and 4."

For example,
It returns 3.

See if the 3 label has a value, if not put the name there (from the textbox).

If it does have a value, keep askin the RandomNumberGenerator to give you another number until you get a clean label to populate.

keep going until you get your labels filled (all 4 of them).

Your technical challenge is with the Random generator function. You'll need to make it give you a value of only between 1 and 4. Let me know if you need help with this.

Thank you for your help!

I don't know if this is the most elegant way (probebly not :)), but it does work. I have something that looks like a random generator:

My module1 looks like this:

Function random(ByVal x As Single) As Single
Dim y As Single
x = Rnd()
If x < 0.25 Then
y = 1
ElseIf x < 0.5 And 0.25 < x Then
y = 2
ElseIf x < 0.75 And 0.5 < x Then
y = 3
ElseIf x < 1 And 0.75 < x Then
y = 4
End If
random = y
End Function


And my form1 (button_click) looks like this:

Dim g As Integer
Form2.Show()
Label1.Text = random(g)


But how can I represent my labels as numbers. Do I have to rename my labels as numbers?

I don't know how to connect the generator with the textboxes and labels.


Cindy
Sep 15 '07 #3

Expert 5K+
P: 8,434
If you have a look at the index in the VB Articles area, there's a routine there to return a random whole-number value within specified limits.
Sep 15 '07 #4

cindy2
P: 35
If you have a look at the index in the VB Articles area, there's a routine there to return a random whole-number value within specified limits.
Ok Thanks, the Randomize function is a lot easyer than my original generator. So I have the next code:


' Initialize the random-number generator.
Randomize()
' Generate random value between 1 and 4.
Dim value As Integer = CInt(Int((4 * Rnd()) + 1))

Label1.Text = value

But how do I connect this with my labels and textboxes. It is possible to do it with an If-Then structure. But that is a lot of programming. Is there maybe a better way?

Cindy
Sep 15 '07 #5

kadghar
Expert 100+
P: 1,295
Ok Thanks, the Randomize function is a lot easyer than my original generator. So I have the next code:


' Initialize the random-number generator.
Randomize()
' Generate random value between 1 and 4.
Dim value As Integer = CInt(Int((4 * Rnd()) + 1))

Label1.Text = value

But how do I connect this with my labels and textboxes. It is possible to do it with an If-Then structure. But that is a lot of programming. Is there maybe a better way?

Cindy
work it with arrays, the main idea would be something like this

Expand|Select|Wrap|Line Numbers
  1. dim myArr(1 to 4) as string 'the ones in textboxes
  2. dim myArr2(1 to 4) as string 'the ones in labels
  3. dim Order(1 to 4) as integer
  4. dim i as integer
  5. dim j as integer
  6.  
  7. order(1) = int(rnd * 4) +1
  8. i=2
  9. for i = 2 to 4
  10.     order(i) = int(rnd * 4)+1
  11.     j=1
  12.     do
  13.         if order(i) = order(j) then
  14.             order(i) = int(rnd * 4)+1
  15.             j = 1
  16.         else
  17.             j=j+1
  18.             if j = i then exit do
  19.         end if
  20.     loop
  21.     i=i+1
  22. next 
  23.  
  24. for i = 1 to 4
  25.     myarr2(i) = myarr(order(i))
  26. next
then just write the second array in your labels
Sep 15 '07 #6

VBPhilly
P: 95
How can you represent labels as numbers? Give the labels the same name.

Each label will then be indexed because they will be considered a control array.

You'll then refer to the labels as:

Label(0)
Label(1)
Label(2)
Label(3)

From above, you can see how you would refer to the labels as a number. 0, 1, 2 or 3.
Sep 16 '07 #7

cindy2
P: 35
work it with arrays, the main idea would be something like this

Expand|Select|Wrap|Line Numbers
  1. dim myArr(1 to 4) as string 'the ones in textboxes
  2. dim myArr2(1 to 4) as string 'the ones in labels
  3. dim Order(1 to 4) as integer
  4. dim i as integer
  5. dim j as integer
  6.  
  7. order(1) = int(rnd * 4) +1
  8. i=2
  9. for i = 2 to 4
  10.     order(i) = int(rnd * 4)+1
  11.     j=1
  12.     do
  13.         if order(i) = order(j) then
  14.             order(i) = int(rnd * 4)+1
  15.             j = 1
  16.         else
  17.             j=j+1
  18.             if j = i then exit do
  19.         end if
  20.     loop
  21.     i=i+1
  22. next 
  23.  
  24. for i = 1 to 4
  25.     myarr2(i) = myarr(order(i))
  26. next
then just write the second array in your labels

Thank you, it looks very nice. It took a while, but now I understand your programming: It fils the (Order)Array, element by element, until the whole array is filt with different numbers (1 to 4). I only don't understand the next code:

myArr2(i) = myArr(Order(i))

If I run the programm, I will get the following error: "Index was outside the bounds of the array." Maybe this is because of the adjustments I had to make for the arrays. Arrays should always begin with the 0-ellement. So my code looks like this:

Expand|Select|Wrap|Line Numbers
  1. Dim myArr(0 To 3) As String 'the ones in textboxes
  2.         Dim myArr2(0 To 3) As String 'the ones in labels
  3.         Dim Order(0 To 3) As Integer
  4.         Dim i As Integer
  5.         Dim j As Integer
  6.  
  7.         Order(0) = Int(Rnd() * 4) + 1
  8.         i = 1
  9.         For i = 1 To 3
  10.             Order(i) = Int(Rnd() * 4) + 1
  11.             j = 0
  12.             Do
  13.                 If Order(i) = Order(j) Then
  14.                     Order(i) = Int(Rnd() * 4) + 1
  15.                     j = 0
  16.                 Else
  17.                     j = j + 1
  18.                     If j = i Then Exit Do
  19.                 End If
  20.             Loop
  21.             i = i + 1
  22.         Next
  23.  
  24.         For i = 0 To 3
  25.             myArr2(i) = myArr(Order(i))
  26.         Next
Apart form the error I get, I really wanna know what the code

myArr2(i) = myArr(Order(i))

exactly means. Because if I don't understand it, I can't make it work.

Cindy
Sep 17 '07 #8

kadghar
Expert 100+
P: 1,295
Thank you, it looks very nice. It took a while, but now I understand your programming: It fils the (Order)Array, element by element, until the whole array is filt with different numbers (1 to 4). I only don't understand the next code:

myArr2(i) = myArr(Order(i))

If I run the programm, I will get the following error: "Index was outside the bounds of the array." Maybe this is because of the adjustments I had to make for the arrays. Arrays should always begin with the 0-ellement. So my code looks like this:
Yeap, not all the VBs let you use arrays starting in 1.

here you have 3 arrays:

MyArr should look like this:
MyArr(0) = John
MyArr(1) = Peter
MyArr(2) = Bill
MyArr(3) = Bob

Order array has random integers, but it could look like this:
Order(0) = 3
Order(1) = 1
Order(2) = 4
Order(3) = 2

and now MyArr2(order(0) = myarr(0)... that means myArr2(3) = John and so on...

The problem here is that Order has Integers from 1 to 4 so all you have to do is to subtract one to that index:

myArr2(i) = myArr(Order(i) - 1)

That should fix the problem
HTH
Sep 17 '07 #9

cindy2
P: 35
The problem here is that Order has Integers from 1 to 4 so all you have to do is to subtract one to that index:

myArr2(i) = myArr(Order(i) - 1)

That should fix the problem
HTH
Just before your reply I considerd also the substraction by one. But unfortunately the same error occured. :(

Cindy
Sep 17 '07 #10

kadghar
Expert 100+
P: 1,295
Just before your reply I considerd also the substraction by one. But unfortunately the same error occured. :(

Cindy
Hi there,

I've checked the code, it had some indexes troubles with the FOR since the begining, this should solve it
(also now Order is a random number from 0 to 3 so its not necessary to substract one anymore)
Expand|Select|Wrap|Line Numbers
  1. Dim myArr(0 To 3) As String 'the ones in textboxes
  2. Dim myArr2(0 To 3) As String 'the ones in labels
  3. Dim Order(0 To 3) As Integer
  4. Dim i As Integer
  5. Dim j As Integer
  6.  
  7. Order(0) = Int(Rnd() * 4)
  8.  
  9. For i = 1 To 3
  10.     Order(i) = Int(Rnd() * 4)
  11.     j = 0
  12.     Do
  13.         If Order(i) = Order(j) Then
  14.             Order(i) = Int(Rnd() * 4)
  15.             j = 0
  16.         Else
  17.             j = j + 1
  18.             If j = i Then Exit Do
  19.         End If
  20.     Loop
  21. Next
  22.  
  23. For i = 0 To 3
  24. myArr2(i) = myArr(Order(i))
  25. Next
HTH
Sep 17 '07 #11

cindy2
P: 35
I've checked the code, it had some indexes troubles ...
Thank You Very Much!!!!!
It works perfectly.

Cindy
Sep 18 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.