|
-
Dec 2nd, 2004, 08:14 AM
#1
Thread Starter
Junior Member
Extracting and printing defined characters from a substring
I wish to print into a label the first 3 characters from a text box called txtSurname.text, and a string variable called surname. The current code looks like
Code:
lblRef.Caption = surname & Int(Rnd * 100)
As it is, the full surname prints in the label followed by a random number between 1 and 99. I wish it to print the first 3 letters of the surname followed by the random number.
As far as I know this uses substrings. Could anybody show me how to do this.
The label is lblRef.caption the surname is txtSurname.text
-
Dec 2nd, 2004, 08:26 AM
#2
I wish it to print the first 3 letters of the surname followed by the random number.
VB Code:
lblRef.Caption = cstr(left(surname,3)) & CInt(Rnd * 100)
The substring is used in vb.net, in vb5/vb6, you need to use the left(), mid(), and right functions.
Last edited by alex_read; Dec 2nd, 2004 at 08:32 AM.
-
Dec 2nd, 2004, 08:29 AM
#3
VB Code:
lblRef.Caption = Left$(surname, 3) & (Int(Rnd * 99) + 1)
Left$ takes the given number of characters out of the string and returns these. The edited Rnd code will result the 1 - 99 you mentioned, because Rnd returns a random number from 0 to anything below 1. Thus adding one makes sure 1 is the lowest score you can get.
-
Dec 2nd, 2004, 08:30 AM
#4
followed by a random number between 1 and 99
Ah, not quite! This will generate a number from 0 to 99. Use this instead:
This will generate a random number from 0 to 98, then add a 1 to it!
-
Dec 2nd, 2004, 08:37 AM
#5
CInt uses bankers' rounding, so it might even give results up to the given number.
VB Code:
Private Sub Command1_Click()
Command1.Caption = CInt(Rnd * 2)
End Sub
See for yourself! Returns 0, 1 or 2
-
Dec 2nd, 2004, 08:40 AM
#6
ah, ok, didn't know that one! kiitos paljon!
-
Dec 2nd, 2004, 09:01 AM
#7
Thread Starter
Junior Member
Thx for all your help, worked perfectly. Now onto this part:
http://www.vbforums.com/showthread.p...hreadid=314667
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
|