Console Write : Console Input Output « Development Class « C# / C Sharp






Console Write

        
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Threading;

namespace Dragger
{
    enum ConsoleAnimation
    {
        None,
        Typewriter,
        Blink
    }

    enum TextAlign
    {     
        Default,
        Left,
        Center,
        Right
    }

    static class ConsoleUtil
    {
        public static void Write(string message, ConsoleColor color)
        {
            ConsoleColor oldColor = Console.ForegroundColor;
            Console.ForegroundColor = color;
            Console.Write(message);
            Console.ForegroundColor = oldColor;
        }

        public static void Write(string message, ConsoleAnimation anim) { Write(message, Console.ForegroundColor, anim); }
        
        public static void Write(string message, TextAlign align)
        {
            Write(message, Console.ForegroundColor, align);
        }        

        public static void Write(string message, ConsoleColor color, TextAlign align)
        {
            int left = Console.CursorLeft;
            switch (align)
            {
                case TextAlign.Left:
                    Console.CursorLeft = 0;
                    break;
                case TextAlign.Center:
                    Console.CursorLeft = Console.BufferWidth / 2;
                    break;
                case TextAlign.Right:
                    Console.CursorLeft = Console.BufferWidth - message.Length;
                    break;
                default:
                    break;
            }
            Write(message, color);
            Console.CursorLeft = left;
        }        

        public static void Write(string message, ConsoleColor color, ConsoleAnimation anim)
        {
            switch (anim)
            {
                case ConsoleAnimation.Typewriter:
                    WriteTwriter(message, color);
                    break;
                case ConsoleAnimation.Blink:
                    WriteBlink(message, color);
                    break;
                default:
                    Write(message, color);
                    break;
            }
        }        
        

        private static void WriteBlink(string message, ConsoleColor color)
        {            
            int top = Console.CursorTop;
            int left = Console.CursorLeft;

            for (int i = 0; i < 7; i++)
            {
                Console.CursorTop = top;
                Console.CursorLeft = left;
                Write(message, (i & 1)==1 ? Console.BackgroundColor: color);
                Thread.Sleep(200);
            }
        }

        private static void WriteTwriter(string message, ConsoleColor color)
        {
            foreach (char ch in message)
            {
                Write(ch.ToString(), color);
                Thread.Sleep(5);
            }
        }
    }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Read double and int from console
2.Read a character from the keyboardRead a character from the keyboard
3.Input from the console using ReadLine()Input from the console using ReadLine()
4.Read a string from the keyboard, using Console.In directlyRead a string from the keyboard, using Console.In directly
5.Write to Console.Out and Console.ErrorWrite to Console.Out and Console.Error
6.Output with parameters
7.Illustrates how to read a character entered using the keyboardIllustrates how to read a character entered using the keyboard
8.Illustrates how to read a string entered using the keyboardIllustrates how to read a string entered using the keyboard
9.Read a line from consoleRead a line from console
10.Demonstrates redirecting the Console output to a fileDemonstrates redirecting the Console output to a file
11.Console command-line argumentsConsole command-line arguments
12.Use format commandsUse format commands
13.Redirect Console.OutRedirect Console.Out
14.This program averages a list of numbers entered by the userThis program averages a list of numbers entered by the user
15.Demonstrate various format specifiersDemonstrate various format specifiers
16.While loop and keyboard readingWhile loop and keyboard reading
17.Demonstrates some of the formatting flags for writing text to the consoleDemonstrates some of the formatting flags for writing text
               to the console
18.Uses the #, 0 and comma characters to format outputUses the #, 0 and comma characters to format output
19.A simple command line program that reads from the console using Console.Read() and Console.ReadLine()A simple command line program that reads from
    the console using Console.Read() and Console.ReadLine()
20.C# Basic Data TypesC# Basic Data Types
21.C# Hello UniverseC# Hello Universe
22.Terminate a control input
23.Use do while to read console input
24.Use while(true) to read console input
25.Convert input from control to upper case
26.constructs sentences by concatenating user input until the user enters one of the termination characters
27.input a series of numbers separated by commas, parse them into integers and output the sum