|
-
Mar 6th, 2004, 06:31 AM
#1
Thread Starter
Supreme User
Randomize String
VB Code:
Private Sub cmdGenerate_Click()
'STORE VARIABLES'
Dim sPossible(5) As String, iTemp As Integer
Static iLast As Integer
'STRINGS TO BE RANDOMIZED'
sPossible(0) = "Hello"
sPossible(1) = "Hello vbForums"
sPossible(2) = "Good day"
sPossible(3) = "Lets go to the pub"
sPossible(4) = "Youre getting married?!"
sPossible(5) = "Im getting a new car today!"
'RANDOMIZE IT'
Randomize Timer
Do
iTemp = Rnd() * 5
Loop Until iLast <> iTemp
iLast = iTemp
txtMain.Text = sPossible(iTemp)
'FINISH THE JOB'
txtMain.SetFocus
txtMain.Refresh
End Sub
-
Jun 15th, 2004, 08:15 AM
#2
Thread Starter
Supreme User
IN DELPHI...
{$R *.dfm}
var
iLast: integer;
Code:
procedure TfrmTipOfTheDay.FormCreate(Sender: TObject);
Var
sPossible: array[0..5] of string;
iTemp: integer;
begin
frmMain.tmrMain.Enabled:= False;
//Possible tips
sPossible[0]:= 'Hello';
sPossible[1]:= 'Hello vbForums!';
sPossible[2]:= 'Good day';
sPossible[3]:= 'Lets go to the pub';
sPossible[4]:= 'Youre getting married?!';
sPossible[5]:= 'Im getting a new car today!';
//Generate random tip
Randomize;
repeat
iTemp:= Random(6);
Until iLast <> iTemp;
iLast:= iTemp;
txtMain.Text:= sPossible[iTemp]
end;
Last edited by Madboy; Jun 15th, 2004 at 08:52 AM.
-
Jun 15th, 2004, 09:29 AM
#3
Thread Starter
Supreme User
You could also put it in a private procedure and call it as needed, useful for say if you have a Next tip button. Although this is in Delphi, you can see how to convert back to VB from above:
Code:
var
iLast: integer;
procedure TfrmTipOfTheDay.RandomizeTips;
var
sPossible: array[0..5] of string;
iTemp: integer;
begin
//Possible tips
sPossible[0]:= 'String 0';
sPossible[1]:= 'String 1';
sPossible[2]:= 'String 2';
sPossible[3]:= 'String 3';
sPossible[4]:= 'String 4';
sPossible[5]:= 'String 5';
//Randomize tips
Randomize;
repeat
iTemp:= Random(6);
Until iLast <> iTemp;
iLast:= iTemp;
txtMain.Text:= sPossible[iTemp]
end;
procedure TfrmTipOfTheDay.FormCreate(Sender: TObject);
begin
RandomizeTips;
end;
procedure TfrmTipOfTheDay.cmdNextClick(Sender: TObject);
begin
RandomizeTips;
end;
Only problem is the next tip will never be unique, meaning you could randomize the next tip and get the same tip as the previous one. There are many ways to prevent this however.
-
Jun 18th, 2004, 02:57 PM
#4
Banned
Re: IN DELPHI...
Is there any specific reason you are posting delphi code in a VB6 codebank? Can you read ????
-
May 26th, 2005, 05:47 AM
#5
Thread Starter
Supreme User
Re: Randomize String
I said it can be illistrutated for conversion back to VB6
-
May 26th, 2005, 05:53 AM
#6
Thread Starter
Supreme User
Re: Randomize String
Actually it is VB ready, just shows Delphi code too
Sorry for the mix up
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
|