Wednesday, 18 May 2016

How to add a reference to a library in Windows Forms Application


This post explains how to add a reference to a library in Windows Forms Application.

By default when we create a new Windows Forms Application, the visual studio adds references to only needed libraries to run a windows form application.





In Solution Explorer, References folder contains a list of referenced libraries.

By default, visual studio adds the list of libraries shown in the picture to win form application when you create a new project.





Depending on application requirements the application requires extra libraries (internal or external) to perform some tasks. In this case we have to add a reference to the required libraries.


Follow these simple steps to add a reference to a library for example "System.Speech". This library contains members which supports in speech related tasks.


1. Create a new project of type "Windows Forms Application" from Windows Desktop template with a name "AddSpeechRef".





2. In Solution Explorer, right-click on References folder, Click on "Add Reference" as shown here.








3. From Reference Manager dialog, enter "Speech" in search box, depending on targeted .Net Framework (here targeted .Net Framework 4.0), the corresponding library version will be shown in search results.




Select a check-box "System.Speech" and click OK.








At this point we have added a reference successfully.




4. Simply adding a reference to our application is not sufficient. To use members of added library, we have to add a namespace of library with a keyword "using".




In Form1.cs, add a line "using System.Speech.Synthesis" as shown below. Now we can use all members of the library in application.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Synthesis;  //add namespace in order to utilize members of library

namespace AddSpeechRef
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

In case of external libraries, in Reference Manager dialog, click on Browse and locate the library files (*.dll) on local machine and click on Add.

Tuesday, 17 May 2016

Change Console Foreground Color, Background Color and Title in C#


This post explains how to change foreground color, background color and title of  console window in C#.

In some cases it is required to change the text color in console window depending on the context of application. For example, green color text indicating success or red color text indicating error/warning.

Here is the sample code -

Create a new project of type console application with a name "ConsoleDemoApp" and enter the code in Program.cs as shown here.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Threading;

namespace ConsoleDemoApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // We want to save the current console color and background color so we can restore it later
            ConsoleColor oldColor = Console.ForegroundColor;
            ConsoleColor oldBackgroundColor = Console.BackgroundColor;            

            // Set the new console color to Green and background color to Gray
            Console.ForegroundColor = ConsoleColor.Green;
            Console.BackgroundColor = ConsoleColor.DarkGray;
                        
            //set title of console window
            //If you don't set the title, the console window shows the fullpath of .exe in title
            Console.Title = "Console Demo Application";

            // Tell everyone what this program is when it starts
            Console.WriteLine("Welcome to Demo Application");

            Random random = new Random();
            while (true)
            {
                Console.Write("Ask any question: ");
                string question = Console.ReadLine();
                //console window exits until u enter either quit or exit
                if (question.ToLower() == "quit" || question.ToLower() == "exit")
                {
                    break;
                }
                Console.Write("Answer: ");
                switch( random.Next(4) )
                {
                    case 0:
                        Console.WriteLine("Yes!");
                        break;
                    case 1:
                        Console.WriteLine("No!");
                        break;
                    case 2:
                        Console.WriteLine("Probably.");
                        break;
                    case 3:
                        Console.WriteLine("I don't understand!!!");
                        break;
                }
            }

            // Restore the old foreground color and background color
            Console.ForegroundColor = oldColor;
            Console.BackgroundColor = oldBackgroundColor;
        }
    }
}

Output -



 
biz.