473,395 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Random labels.text

cindy2
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
11 4906
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
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
Killer42
8,435 Expert 8TB
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
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
1,295 Expert 1GB
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
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
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
1,295 Expert 1GB
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
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
1,295 Expert 1GB
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
35
I've checked the code, it had some indexes troubles ...
Thank You Very Much!!!!!
It works perfectly.

Cindy
Sep 18 '07 #12

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

Similar topics

2
by: Martin O'Rourke | last post by:
All, I am hoping someone might be able to put me out of my misery and let me know if it is possible or not to dervie the name of an element in a form, based on its associated label, only knowing...
13
by: Jon Agiato | last post by:
Hello, I am sure this problem is easy to spot but I have been at this project all day and the frustration has finally overcome me. I am using this function in order to produce a standard normal...
1
by: Kyle Blaney | last post by:
When labels are vertically stacked on top of one another and have their TextAlign property set to MiddleRight, the text is not properly right-aligned. You can reproduce this problem by creating...
2
by: Giovane Calabrese | last post by:
( aspx + vb ) hi everyone ! I really need make that function work ! im brazilian , and i want to make a multilanguage system , that function above must look at all ASPX take the labels ID and...
2
by: tony collier | last post by:
hi i have various labels on my page with ID's label_1, label_2 , label_3 etc. How can i write a loop that sequentially changes their text values to values i have in an array eg;. for...
5
by: Haydnw | last post by:
Hi, I have the code below as code-behind for a page which displays two images. My problem is with the second bit of code, commented as " 'Portfolio image section". Basically, the SQL query gets...
3
by: Ken McCrory | last post by:
I have a web form page (.aspx) with 95 label controls that I now need to be text boxes. Is there an easy way to change those to what I want using something like a find and replace or am I stuck...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
5
by: Anthony | last post by:
For Some reason, i get the same value everytime... Ive tested the RND Function with msgboxes and labels, they all have no influence on the RND Function... It still doesnt work... Private Sub...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.