PDA

Click to See Complete Forum and Search --> : Visual Basic - The Best Typewriting Code!


vb Legend
Jul 15th, 2004, 09:27 AM
'Virtually the best typewriting code available.
'All others i have seen are much longer.
'I am now currently working on a "wait" function
'so there is no need for a timer.

Private Sub Timer1_Timer()
Typewrite "Testing"
End Sub

Private Function Typewrite(Word As String)
'Function Simply Processes Effect Of A Typewriter.
Static NextChar As Integer 'Move onto the next character.
Dim Length As Integer 'Used for increasing length.
Dim Display As String 'Letters to display.
NextChar = NextChar + 1 'Move onto the next letter.
Length = Length + 1 'Increase the length of the word.
Display = Mid$(Word, NextChar, Length) 'What to display.
Label1.Caption = Label1.Caption & Display 'Display!
End Function


If anyone has any ideas how to include a wait function then please offer me your opinions. :bigyello:

Ecniv
Jul 15th, 2004, 09:57 AM
Originally posted by vb Legend

'Virtually the best typewriting code available.
'All others i have seen are much longer.
'I am now currently working on a "wait" function
'so there is no need for a timer.

Public sub Typewrite(byval strWord As String, byval dteDelayFor as date)
'Function Simply Processes Effect Of A Typewriter.
Dim dteDisp as Date 'DateTime to disp after
Dim lngCtr as long

for lngCtr = 0 to len(strWord)
Label1.Caption = mid$(strWord,1,lngCtr)
dteDisp = now + dteDelay
do until now>dteDisp
' Delay around here
loop
next
End sub



This sort of works, but not brilliantly. Probably need the gettickcount api call to get a more accurate time.
To run :
TypeWrite "Testing",Cdate("0:0:01")


Vince

vb Legend
Jul 15th, 2004, 10:34 AM
A nice way to configure the delay.

Im just re-arranging it so that it delays between each letter appearing. :D

The reason why i am doing these basic code's is because i am currently working on a module file, that is an archive of functions that people can use. Im just gonna keep adding simple functions and subs similar to this one.



:bigyello:

dsheller
Jul 15th, 2004, 12:26 PM
Private Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long

Private Sub Wait ( msDelay as Long )

Dim lngLastTick as Long

lngLastTick = GetTickCount

Do While (lngLastTick + msDelay) >= GetTickCount
Loop

End Sub

You should add a doevents inside the do while loop if you want your program not to be "frozen"