Brin Žvan
Brin Žvan

Reputation: 47

Visual Basic console - Making text type itself out

I'm totally cold on this so I have no idea how I would even start coding something like this except for the part that I think it might use some kind of a timer. Basicly I want the text to type itself out on the console so say that a basic Console.WriteLine ("Hello world") would take 5 seconds to write itself (so 2 letters per second). Is there a command that does something like this? Or do I have to make some (hopefully not) complex timer based Sub that will write the text out for me?

Thanks in advance!

Upvotes: 0

Views: 1098

Answers (2)

Fᴀʀʜᴀɴ Aɴᴀᴍ
Fᴀʀʜᴀɴ Aɴᴀᴍ

Reputation: 6251

Use this code to get the effect:

Sub ConsoleType(stringToWrite As String, delay As Integer)
    For Each i As Char In stringToWrite
        Console.Write(i)
        Threading.Thread.Sleep(delay)
    Next
End Sub

Call the function ConsoleType and it will be 'typed'. Note that the delay is in milliseconds. Since you want the speed of 2 Letters/sec, you could use a value of 500.

Upvotes: 2

Hans Passant
Hans Passant

Reputation: 942538

No idea why anybody ever would want to do that. But it is easy to do, you can reassign Console.Out to another text stream which can monkey with the text as it sees fit.

Class LazyWriter
    Inherits System.IO.TextWriter
    Private original As System.IO.TextWriter

    Public Sub New(original As System.IO.TextWriter)
        Me.original = original
    End Sub

    Public Overrides Sub Write(value As Char)
        original.Write(value)
        System.Threading.Thread.Sleep(500)
    End Sub

    Public Overrides ReadOnly Property Encoding As Encoding
        Get
            Return original.Encoding
        End Get
    End Property
End Class

Usage:

Sub Main()
    Console.SetOut(New LazyWriter(Console.Out))
    Console.WriteLine("Hello slow world")
    Console.ReadLine()
End Sub

Yikes, that's slow.

Upvotes: 1

Related Questions