C# Programming Examples: Console Window Application

C# TASK:

Create a console window C# application that shows the following each time the Enter key is pressed:

      1. NUM LOCK key state (using green text on a blue background);
      2. CAPS LOCK key state (using red text on a yellow background);
      3. a request for a user to input a message (using default text and background colors) which is then set as the new titlebar text for the application.

SOLUTION:

In this sample, we are going to inspect some of the more advanced functionalities when it comes to dealing with data exchange between the user and C# application through the console window. As you probably already know, this means that all the information comes in the form of text, with the user being expected either to make/confirm choices, or input text data. To learn more ways to influence this, you may want to take a look at the properties and methods of .NET Framework’s Console Class itself: https://msdn.microsoft.com/en-us/library/system.console%28v=vs.110%29.aspx

Since the program is meant to work cyclically each time Enter is pressed (that is, each time the user inputs his or her message), we will need to use a looping statement. Let us also provide the user with the option to end it if he or she inputs the “quit” keyword. The basic looped code would look as follows:

Console window #1

Such an application will keep on prompting the user to enter a message until he or she inputs “quit”, after which the application will stop running. Next, let’s add the display of NUM LOCK and CAPS LOCK states into the loop. Since we can get their properties only as boolean true/false, a little transition will be necessary to allow us to show their states as on/off:

Console.NumberLock

Now we should upgrade the code with a text/background color alteration, as well as the ability to update the titlebar on each loop:

Now we should upgrade the code with a text/background color alteration, as well as the ability to update the titlebar on each loop

With this, the colors and the titlebar are updated; but the colors used for our CAPS LOCK-related message now also spreads to the subsequent messages when they shouldn’t. To fix this, we should reset them back to the defaults once the CAPS LOCK message ends:

This fixes the issue, and the application now works as it should.

This is one of our C# programming examples that was completed by our IT expert. You may be interested in our C# programming sample on switch-case operator as well. If you need any of technical assignments completed according to your requirements, don’t hesitate to contact us any time. Our experts will follow your requirements and will provide you with high-quality help.

Leave a Reply

Your email address will not be published. Required fields are marked *

Customer testimonials

Submit your instructions to the experts without charge.