4

How do i blink the text in console using C#?

nikky
  • 1,681
  • 3
  • 17
  • 19

7 Answers7

6

Person-b was on the right track, but their code needs some changes:

    static void Main()
    {
        string txt = "Hello, world!";
        while (true)
        {
            WriteBlinkingText(txt, 500, true);
            WriteBlinkingText(txt, 500, false);
        }
    }

    private static void WriteBlinkingText(string text, int delay, bool visible)
    {
        if (visible)
            Console.Write(text);
        else
            for (int i = 0; i < text.Length; i++)
                Console.Write(" ");
        Console.CursorLeft -= text.Length;
        System.Threading.Thread.Sleep(delay);
    }

EDIT: Refactored the code a little

Matt Brindley
  • 9,581
  • 7
  • 45
  • 49
  • Tah! I was in a bit of a hurry earlier, thanks for the refactoring. I was thinking about having a visible bool but decided for the quick solution that might not work. Anyway, +1 from me. – Lucas Jones Apr 10 '09 at 10:41
  • np :-) The main problem was that you need to call sleep and adjust cursorleft after writing both times - otherwise it'll either always be visible or always hidden. – Matt Brindley Apr 10 '09 at 10:45
  • 1
    Don't forget to select an answer :-) – Matt Brindley Apr 10 '09 at 15:46
1

To do this you will have to use:

console.clear

which will clear all the information in the console, but allow you to simulate a blinking text, by doing the following code you can do this: (This is in VB but its easily translated)

Dim Flash As Integer
Flash = 0
While Flash < 100 (Any number can be put here for duration)
Flash = Flash + 1
console.Backgroundcolor = consolecolor.Black
console.clear
system.threading.thread.sleep(25)
console.Backgroundcolor = consolecolor.White
console.clear
system.threading.thread.sleep(25)
End While

And that would give a blinking screen for example, for the blinking text simply adjust it:

Dim FlashWord As Integer
FlashWord = 0
While FlashWord < 100 (Any number can be put here for duration)
FlashWord = FlashWord + 1
console.Foregroundcolor = consolecolor.Black
console.clear
Console.Writeline("Hello World")
system.threading.thread.sleep(25)
console.Foregroundcolor = consolecolor.White
console.clear
Console.Writeline("Hello World")
system.threading.thread.sleep(25)
End While

And this will simulate 'flashing', the only down side is that you lose your previous on-screen info, but nothing else, and for an effect this is pretty good!

0

I improved Matthew's code a bit using \r and some fancy use of String:

static void Main()
{
    string txt = "Hello, world!";
    WriteBlinkingText(txt, 500);
}

private static void WriteBlinkingText(string text, int delay)
{
    bool visible = true;
    while (true)
    {
        Console.Write("\r" + (visible ? text : new String(' ', text.Length)));
        System.Threading.Thread.Sleep(delay);
        visible = !visible;
    }
}

I also think that the WriteBlinkingText method should be self-contained, so the loop is inside here, but that's just a matter of personal taste I guess :)

Sune Rievers
  • 2,506
  • 3
  • 24
  • 29
0
    private void timer1_Tick(object sender, EventArgs e)//This might work better for you :)
    {
        Random rand = new Random();
        for (int i = 0; i < 255; i++)
        {
            int A = rand.Next(i);
            int R = rand.Next(i);
            int G = rand.Next(i);
            int B = rand.Next(i);
            label2.ForeColor = Color.FromArgb(A, R, G, B);
        }
    }
0

enter image description here

Why not use backspace and rewrite the text on the same screen line?

string blink = "Password Please!"

while (!System.Console.KeyAvailable)
   {
      Console.Write(blink);
      Thread.Sleep(650);

      for (int j = 1; j <= blink.Length + 2; j++)
          {
             Console.Write("\b"+(char)32+"\b");
             if (j == blink.Length + 2) Thread.Sleep(650);
          }
   }

Io-oI
  • 2,386
  • 3
  • 17
  • 25
0
string txt = "Hello, world!";
while ( doingSomething )
{
  Console.Write(txt);
  System.Threading.Thread.Sleep(20);
  Console.CursorLeft -= txt.Length;
  for ( int i = 0; i < txt.Length; i++ )
    Console.Write(" ");
}

this code does not make it blink though

Bevan
  • 42,405
  • 10
  • 82
  • 131
nikky
  • 1,681
  • 3
  • 17
  • 19
0

There is no direct support, you will need to overwrite the text with spaces (or in different colours) based on a timer.

Richard
  • 103,472
  • 21
  • 199
  • 258