CupCode Gamers
From the Cup, to the Code, for the Gamers
  • News
  • Games and Apps
    • Baseball Score Tracker
    • Droplets
    • Jet Fighter
    • Jet Fighter Neon
    • Short Circuit
    • ThaumCheater
    • Tic-Tac-Toe
    • Zombie Swarm
  • Other Pages
    • Facebook
    • Itch-IO
    • Surviving Ars Magica 2
    • Steam Group
    • YouTube Channel
  • Sneaky Peeky
  • C# Tutorials
  • About Us
  • Policies
  • Contact Us

Console Tutorial By Example

3/27/2017

using System;

// This tutorial will show examples of using basic input/output and manipulation
// methods of the console class.

namespace ConsoleTutorials
{
    class BasicsTutorial
    {
        static void Main(string[] args)
        {
            // Console.WriteLine writes an entire line to screen then moves to the next line
            Console.WriteLine("This is Tutorial1 - Basics");

            // Console.Write writes the text to screen, but doesn't move to the next line.
            Console.Write("Console.");
            Console.Write("Write");

            // Using Console.Write, there are two ways to go to the next line.
            // This first is the best recommended as it automatically detects the proper new line sequence.
            Console.Write(Environment.NewLine);
            // This is the old standard often used on Linux to represent a windows new line
            // \r represents a carriage return
            // \n represents a new line
            Console.Write("\r\n");

            // Sets the title of the console window to whatever you want.
            Console.Title = "Console.Title";

            // Sets the background color
            Console.BackgroundColor = ConsoleColor.DarkBlue;
            // Sets the foreground color
            Console.ForegroundColor = ConsoleColor.Yellow;

            // Set if the cursor is visible in the console window or not
            Console.CursorVisible = true;

            // Set this to true to prevent CTRL-C from being treated as an interrupt.
            // Default for this is false
            Console.TreatControlCAsInput = true;

            // Detect if the user has thier caps-lock on
            if (Console.CapsLock == true)
                Console.WriteLine("Your caps lock is turned on.");
            else
                Console.WriteLine("Your caps lock is turned off.");

            // Detect if the user has thier num-lock on
            if (Console.NumberLock == true)
                Console.WriteLine("Your num lock is turned on.");
            else
                Console.WriteLine("Your num lock is turned off.");

            // These will cause errors unless they are the very first things in
            // the main class
            // Set the distance the console window is from the top of the screen in pixels
            //Console.WindowTop = 0;
            // Set the distance the console window is from the left of the screen in pixels
            //Console.WindowLeft = 0;
            // Set how tall the window is in pixels
            //Console.WindowHeight = 600;
            // Set how wide the window is in pixels
            //Console.WindowWidth = 800;

            // Set how far from the left the cursor is in characters
            Console.CursorLeft = 0;
            // Set how far from the top the cursor is in characters
            Console.CursorTop = 15;

            Console.WriteLine("Please type a line of characters then press ENTER");
            // Read a line of characters from the standard input stream(keyboard)
            // This means it won't stop until you press enter
            string Entry = Console.ReadLine();

            Console.WriteLine("Please press ESCAPE");
            // Read a single key press from the keyboard
            // This is usefull for detecting function and arrow keys.
            ConsoleKeyInfo key = Console.ReadKey();
            if(key.Key == ConsoleKey.Escape)
                Console.WriteLine("You pressed escape.");

            Console.WriteLine("Please press a letter or number.");
            // Read the next character pressed on the keyboard
            // Returns the value as an integet represtantion of an ascii value
            int ikey = Console.Read();
            Console.WriteLine(string.Format("You pressed {0}", Convert.ToChar(ikey)));

            Console.WriteLine("Press any key to continue . . .");
            int tmp = Console.Read();
        }
    }
}


Respond

Copyright © 2020 CupCode Gamers / Jason Yarber

Privacy Policy