Ad

Sunday, August 11, 2013

Console Class Methods in C#.Net

Overview:
                In this article i would like to publish an article related to "Console Class Methods in C#.Net".


Description:
                    Console is nothing but a black color screen.It is defined as a standard input,output error streams for a console applications.
 For More Article Related To Console Class


It consists of different methods and events.
Method Name Description
Beep() Plays the sound of a beep through the console speaker.
Beep(int32,int32) Plays the sound of a beep of a specified frequency and duration through the console speaker.
Clear() Clears the console buffer and corresponding console window of display information.
OpenStandardInput() Acquires the standard input stream.
OpenStandardInput(Int32) Acquires the standard input stream, which is set to a specified buffer size.
Read(boolean) Reads the next character from the standard input stream.
ReadKey() Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.
ReadKey(boolean) Obtains the next character or function key pressed by the user. The pressed key is optionally displayed in the console window.
ReadLine() Reads the next line of characters from the standard input stream.
ResetColor() Sets the foreground and background console colors to their defaults.
SetBufferSize() Sets the height and width of the screen buffer area to the specified values.
Write(Many) Writes the current terminator to the standard output stream.
WriteLine() Writes the current line terminator to the standard output stream.

Note:
Write() and WriteLine() Methods are overloadable methods in different forms:
Write(Boolean),Write(Char),Write(Char[]),Write(Int32),Write(Int64),Write(Object),Write(Single),Write(String),Write(UInt32),Write(UInt64),Write(String, Object),Write(String, Object[]),Write(Char[], Int32, Int32),Write(String, Object, Object),Write(String, Object, Object, Object),Write(String, Object, Object, Object, Object),WriteLine(),WriteLine(Boolean),WriteLine(Char),WriteLine(Char[]),WriteLine(Decimal),WriteLine(Double),WriteLine(Int32),WriteLine(Int64),WriteLine(Object),WriteLine(Single),WriteLine(String),WriteLine(UInt32),WriteLine(UInt64),WriteLine(String, Object),WriteLine(String, Object[]),WriteLine(Char[], Int32, Int32),WriteLine(String, Object, Object),WriteLine(String, Object, Object, Object),WriteLine(String, Object, Object, Object, Object).


Program:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Consolemethods
{
    class Program
    {
        public static int saveBufferWidth;
        public static int saveBufferHeight;
        public static int saveWindowHeight;
        public static int saveWindowWidth;
        public static bool saveCursorVisible;
        public static void Main(String[] args)
    {
        //Title Property
        Console.Title = "Console Class By Naga Veerendra Parvathaneni visit(http://www.pv999.blogspot.com)";
            //Beep Method
        Console.Beep();
        Console.Beep(100,100);
        //OpenStandardInput Method(Use this namespace "using System.IO")
        Stream inputStream = Console.OpenStandardInput();
        byte[] bytes = new byte[100];
        Console.WriteLine("To decode, type or paste the UTF7 encoded string and press enter:");
        Console.WriteLine("(Example: \"M+APw-nchen ist wundervoll\")");
        int outputLength = inputStream.Read(bytes, 0, 100);
        char[] chars = Encoding.UTF7.GetChars(bytes, 0, outputLength);
        Console.WriteLine("Decoded string:");
        Console.WriteLine(new string(chars));       
            //For Buffer Size
        string m1 = "1) Press the cursor keys to move the console window.\n" +
            "2) Press any key to begin. When you're finished...\n" +
            "3) Press the Escape key to quit.";
        string g1 = "+----";
        string g2 = "|    ";
        string grid1;
        string grid2;
        StringBuilder sbG1 = new StringBuilder();
        StringBuilder sbG2 = new StringBuilder();
        ConsoleKeyInfo cki;
        int y;
        //
        try
        {
            saveBufferWidth = Console.BufferWidth;
            saveBufferHeight = Console.BufferHeight;
            saveWindowHeight = Console.WindowHeight;
            saveWindowWidth = Console.WindowWidth;
            saveCursorVisible = Console.CursorVisible;
            //
            Console.Clear();
            Console.WriteLine(m1);
            Console.ReadKey(true);

            // Set the smallest possible window size before setting the buffer size.
            Console.SetWindowSize(1, 1);
            Console.SetBufferSize(80, 80);
            Console.SetWindowSize(40, 20);

            // Create grid lines to fit the buffer. (The buffer width is 80, but
            // this same technique could be used with an arbitrary buffer width.)
            for (y = 0; y < Console.BufferWidth / g1.Length; y++)
            {
                sbG1.Append(g1);
                sbG2.Append(g2);
            }
            sbG1.Append(g1, 0, Console.BufferWidth % g1.Length);
            sbG2.Append(g2, 0, Console.BufferWidth % g2.Length);
            grid1 = sbG1.ToString();
            grid2 = sbG2.ToString();

            Console.CursorVisible = false;
            Console.Clear();
            for (y = 0; y < Console.BufferHeight - 1; y++)
            {
                if (y % 3 == 0)
                    Console.Write(grid1);
                else
                    Console.Write(grid2);
            }

            Console.SetWindowPosition(0, 0);
            do
            {
                cki = Console.ReadKey(true);
                switch (cki.Key)
                {
                    case ConsoleKey.LeftArrow:
                        if (Console.WindowLeft > 0)
                            Console.SetWindowPosition(
                                    Console.WindowLeft - 1, Console.WindowTop);
                        break;
                    case ConsoleKey.UpArrow:
                        if (Console.WindowTop > 0)
                            Console.SetWindowPosition(
                                    Console.WindowLeft, Console.WindowTop - 1);
                        break;
                    case ConsoleKey.RightArrow:
                        if (Console.WindowLeft < (Console.BufferWidth - Console.WindowWidth))
                            Console.SetWindowPosition(
                                    Console.WindowLeft + 1, Console.WindowTop);
                        break;
                    case ConsoleKey.DownArrow:
                        if (Console.WindowTop < (Console.BufferHeight - Console.WindowHeight))
                            Console.SetWindowPosition(
                                    Console.WindowLeft, Console.WindowTop + 1);
                        break;
                }
            }
            while (cki.Key != ConsoleKey.Escape);  // end do-while
        } // end try
        catch (IOException e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {
            Console.Clear();
            Console.SetWindowSize(1, 1);
            Console.SetBufferSize(saveBufferWidth, saveBufferHeight);
            Console.SetWindowSize(saveWindowWidth, saveWindowHeight);
            Console.CursorVisible = saveCursorVisible;
        }
            Console.ReadLine();
        }
}
    }


 Output:
Console Methods in C#.Net

No comments: