dcsimg
 

Creating a Typewriter Effect in .NET

Wednesday Sep 19th 2018 by Hannes DuPreez

Learn how to mimic a typewriter's behaviour. Examples are in both C# and VB.NET.

Introduction

I'm an aspiring writer. My ultimate dream is to become a famous writer, retire, open a coffee shop in a nice little quaint town, and write more. Yes, I know, it may be a pipe dream, but who knows? Perhaps it will work out.

I have always wanted a typewriter, but alas, it will have to wait. Because I do not have my own typewriter yet, I have decided to create this article. In this article, you will learn how to mimic a typewriter's behaviour.

Practical

Open Visual Studio and create a new C# or Visual Basic.NET Windows Forms project. There is not much of a design. There is only one RichTextBox on the form and it is docked.

Design
Figure 1: Design

There isn't much code, but I do hope you forgive me. As usual, add the necessary Namespaces to your code.

C#

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

VB.NET

Imports System.Threading

The System.Threading namespace allows you to spawn different threads in your applications. If you do not know anything about threading, you may want to read this article before continuing: Introduction to Threading and VB.NET The article covers the ins and outs of basic threading.

Add the following sub procedure to your Form's code.

C#

   private void TypeWords(string strInput)
   {
      string[] ArrWords = strInput.Split(' ');

      Task t = Task.Run(() =>
      {

         foreach (string strWord in ArrWords)
         {

            foreach (char chrLetter in strWord)
            {

               Thread.Sleep(100);

            }

            Thread.Sleep(250);
         }
      });

      t.Wait();

   }

VB.NET

   Private Sub TypeWords(ByVal strInput As String)

      Dim ArrWords As String() = strInput.Split(" "c)

      Dim t As Task = Task.Run(Sub()

         For Each strWord As String In ArrWords

            For Each chrLetter As Char In strWord

               Thread.Sleep(100)

            Next

            Thread.Sleep(250)

         Next
      End Sub)
      t.Wait()

   End Sub

The TypeWords Sub procedure accepts a string as an argument. ArrWords is an array that splits the input string at each space. A new task gets spawned; this makes use of the Threading classes to pause displaying the character. This gives the delayed effect an ordinary typewriter has.

Call this sub procedure from your RichTextBox's KeyUp event.

C#

   private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
   {
      if (!String.IsNullOrEmpty(richTextBox1.Text))
      {

         TypeWords(richTextBox1.Text);

      }

   }

VB.NET

   Private Sub richTextBox1_KeyUp(sender As Object, _
            e As KeyEventArgs) Handles richTextBox1.KeyUp

      If Not String.IsNullOrEmpty(richTextBox1.Text) Then

         TypeWords(richTextBox1.Text)

      End If

   End Sub

If there is valid input, the TypeWords sub procedure will be called.

When running this application, your screen (after some input) will resemble Figure 2.

Running
Figure 2: Running

Conclusion

I think it is quite apparent that I am a lazy guy and that I didn't have much work to do. Times like these enable me to conjure up funky little apps such as this one. I sincerely hope you have enjoyed it. Happy coding!

Home
Mobile Site | Full Site
Copyright 2018 © QuinStreet Inc. All Rights Reserved