Loading...
Friday 28 October 2016

C# Programming

1. Button , Message Box , TextBox and Label control in windows form application 

1. For This section , Create new windows form project using C# language
2. Add a Button control on Window Form name it "BTN" from properties , also add a textbox and a label any where on the form , give textbox name as "txt" and specify name for label as saquarelbl
3. Double Click Button which created in  previous step , now a code window will appear which generate button click event as shown below, just you have to type MessageBox.show() line between { }. in different way as given in sample programs.
4. For Executing programs, press F5 or Ctrl+F5 from keyboard.

Program1: Simple Program to display a message box on button click, No title in title bar of message box. 
private void Btn_Click(object sender, EventArgs e)
        {
            MessageBox.Show("WELCOME TO C#");
        }

Program2: This program shows message "Welcome to C#", in message box, and it title in title bar will be Hello
private void Btn_Click(object sender, EventArgs e)
        {
            MessageBox.Show("WELCOME TO C#","Hello");
        }

Program3: This program display message box with a string message, string title and Buttons Yes No Cancel. 
private void Btn_Click(object sender, EventArgs e)
        {
            MessageBox.Show("WELCOME TO C#","Hello",MessageBoxButtons.YesNoCancel);
        }

Program4: This program display a message box on button click to display the addition of 12+3 
private void Btn_Click(object sender, EventArgs e)
        {
            MessageBox.Show((12+3).ToString());
        }

Program 5: This program creates two variables a and b , with values 12 assigned to each variables and display addition of both variables 
private void Btn_Click(object sender, EventArgs e)
        {
            int a = 12, b = 12;
            MessageBox.Show("a+b=\t"+(a+b).ToString());
        }

Program 6: This program dispaly message "Button Clicked " in a message box when button clicked. 
private void Btn_Click(object sender, EventArgs e)
        {
            txt1.Text = "Button clicked";
           
        }

Program 7: This program create a String variable name , with A.Rafay value assigned to it in button click event, and display Hello A.Rafay in textbox. 
private void Btn_Click(object sender, EventArgs e)
        {
            String name = "A.Rafay";
            txt1.Text = "Hello !"+name;
           
        }


Program 8: This program shows message box with a message, which you type in textbox and changes text color of text box text to black  on button click.
private void Btn_Click(object sender, EventArgs e)
        {
            txt1.ForeColor = Color.Black;
            MessageBox.Show("Hello !" + txt1.Text);
           
        }


Program 9: Simple Program to display a message box on button click, In title bar of message box title will not be shown
private void Btn_Click(object sender, EventArgs e)
        {
           
            int n = Int32.Parse(txt1.Text);
            int square = n * n;
            squarelbl.Text = square.ToString();
           
        }



2. Basic C# Example Programs 

This section covers very simple and basic C# Console Application Programs, which includes Simple input output programs , basic variable programs, comments in c# program. For this section follow the following steps :
  • Create a new C# Console Application 
  • C# generate code for you , remove it first then copy and paste following programs one by one. when you copy any program from below list , make sure you have deleted previous program. 
  • Execute these programs by press F5 or Ctrl+F5 
Program 1: A very basic C# Console programs. 
namespace app1
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Welcome to C#");
            System.Console.WriteLine("Press any key to exit");
            System.Console.ReadKey();
        }
    }
}

Program 2: Comments in C# Console program. 

using System;

namespace app1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to C#");     //This is output function whcih display output result
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();          // this is input function which takes one character from key board

        }
    }
}


Program 3: This program demonstrates use of escape sequence characters. 

using System;

namespace app1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press any key\n");
            char ch =Console.ReadKey().KeyChar;
            Console.WriteLine("\n\nYou Pressed \t" + ch);
            Console.ReadKey();
        }
    }
}




Program 4:This program will display Welcome to C#, each work on a key press. 


using System;

namespace app1
{
    class Prog
    {
        static void Main(string[] args)
        {
            Console.ReadKey();
            Console.WriteLine("Welcome");
            Console.ReadKey();
            Console.WriteLine("To");
            Console.ReadKey();
            Console.WriteLine("C#");
            Console.ReadKey();
        }
    }
}



Program 5:Program to display Rsoft Computer Tutorials each word in key press. 

using System;

namespace app1
{
    class Prog
    {
        static void Main(string[] args)
        {
            Console.ReadKey();
            Console.Write(" Rsoft");
            Console.ReadKey();
            Console.Write("  Computer");
            Console.ReadKey();
            Console.Write(" Tutorials");
            Console.ReadKey();
        }
    }
}



Program 6: C# Program to display basic calculation 

using System;

namespace app1
{
    class Prog
    {
        static void Main(string[] args)
        {
           Console.WriteLine("5+5=\t" + (5 + 5));
           Console.WriteLine("10-3=\t" + (10 - 3));
           Console.WriteLine("2*3=\t" + (2 * 3));
           Console.WriteLine("12/2=\t" + (12/2));
           Console.WriteLine("13%3=\t" + (13 % 3));
            Console.ReadKey();
         

        }
    }
}
         


 Program 7: This program display addition of two variables a and b.
using System;

namespace app1
{
    class Prog
    {
        static void Main(string[] args)
        {
            int a=12, b=3;
           Console.WriteLine("a+b=\t" + (a + b));
            Console.ReadKey();
         

        }
    }
}


Program 8: This program takes two values from user in a and b through ReadLine, convert values to Integer through Convert.ToInt32() and show addition of both variables.

using System;

namespace app1
{
    class Prog
    {
        static void Main(string[] args)
        {
            int a, b;
            Console.WriteLine("Enter Value for a ");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter Value for b ");
            b = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Addition of a and b is \t"+ (a+b));

          
            Console.ReadKey();
         

        }
    }
}


Program 9:Using placeholder for producing output result. 


using System;

namespace app1
{
    class Prog
    {
        static void Main(string[] args)
        {
            int a, b;
            Console.WriteLine("Enter Value for a ");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter Value for b ");
            b = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Addition of {0} and {1} is\t{2}",a,b,a+b);

          
            Console.ReadKey();
         

        }
    }
}



Program 10: Program to take your name in a variable "name" and display Hello and name which you type. 

using System;

namespace app1
{
    class Prog
    {
        static void Main(string[] args)
        {

            String name;
            Console.WriteLine("Please enter your name");
            name = Console.ReadLine();
            Console.WriteLine("Hello !" + name);

          
            Console.ReadKey();
         

        }
    }
}



3. Very Simple C# Console Application Programs 

This is continuation of previous section , this section covers simple example programs related with input and output, variables and data types, placeholders and sizeof() function  
Program 1: declaring variables of type byte
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simpleapp
{
    class Program
    {
        static void Main(string[] args)
        {
            byte b1=12;
            byte b2 = 10;
            Console.WriteLine("The Value of b1 is\t" + b1);
            Console.WriteLine("The Value of b2 is \t" + b2);
            Console.WriteLine("Addition of b1 and b2 is \t" + (b1 + b2));
           
            Console.ReadKey();
           
        }
    }
}

Program 2: variables of type byte, showing output result through placeholders. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simpleapp
{
    class Program
    {
        static void Main(string[] args)
        {
            byte b1=12;
            byte b2 = 10;
            Console.WriteLine("The Value of b1 is\t {0}" , b1);
            Console.WriteLine("The Value of b2 is \t {0}" , b2);
            Console.WriteLine("Addition of b1 and b2 is {0} \t" , b1 + b2);

            Console.ReadKey();
           
        }
    }
}



Program 3: using placeholders to show result of string variable 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simpleapp
{
    class Program
    {
        static void Main(string[] args)
        {
            String name;
            Console.WriteLine("Enter your name");
            name = Console.ReadLine();
            Console.WriteLine("Welcome ! {0}", name);
            Console.ReadKey();
           
        }
    }
}


Byte Example 1
using System;

namespace Simpleapp
{
    class Program
    {
        static void Main(string[] args)
        {
            byte b1, b2;
            Console.WriteLine("Enter value 1: ");
            b1 = Byte.Parse(Console.ReadLine());
            Console.WriteLine("Enter Value 2: ");
            b2 = Byte.Parse(Console.ReadLine());
            Console.WriteLine("{0}+{1}={2}", b1, b2, b1 + b2);
            Console.WriteLine("{0}-{1}={2}", b1, b2, b1 - b2);
            Console.WriteLine("{0}*{1}={2}", b1, b2, b1 * b2);
            Console.WriteLine("{0}/{1}={2}", b1, b2, b1 / b2);
            Console.WriteLine("{0}%{1}={2}", b1, b2, b1 % b2);
            Console.ReadKey();
           
            
        }
    }
}


byte Data type Example 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simpleapp
{
    class Program
    {
        static void Main(string[] args)
        {
            byte b=12;
            Console.WriteLine("Value of b is \t {0}", b);
            Console.WriteLine("Size of b is \t {0}",sizeof(byte));
            Console.WriteLine("Minimum Range of byte is\t{0}",Byte.MinValue);
            Console.WriteLine("Maximum Range of byte is \t{0}",Byte.MaxValue);
            Console.ReadKey();
           
        }
    }
}



4.  Type casting in C#

This section contains csharp sample example programs of data type conversion topic, which is also called type casting. You will be able to use Convert methods, int.parse() method and int.tryParse() method.



PROGRAM 1: using Covert.ToInt32() method to convert string input to integer. 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Data_TypeConversion
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b;
            Console.WriteLine("Enter value 1");
            a =Convert.ToInt32( Console.ReadLine());
            Console.WriteLine("Enter value 2");
            b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("{0}+{1}={2}", a, b, a + b);


            Console.ReadKey();

        }
    }
}
PROGRAM 2: Using parse method to convert string input to integer 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Data_TypeConversion
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b;
            Console.WriteLine("Enter value 1");
            a = Int32.Parse(Console.ReadLine());
            Console.WriteLine("Enter value 2");
            b = Int32.Parse(Console.ReadLine());
            Console.WriteLine("{0}+{1}={2}", a, b, a + b);

            Console.ReadKey();

        }
    }
}

PROGRAM 3 : Using wrapper class Int32

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Data_TypeConversion
{
    class Program
    {
        static void Main(string[] args)
        {

            Int32 obj = new Int32();
            obj = 20;
            int var;
            var = obj;
            Console.WriteLine(var);

            Console.ReadKey();

        }
    }
}

PROGRAM 4: Using Byte wrapper class in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Data_TypeConversion
{
    class Program
    {
        static void Main(string[] args)
        {

            Byte b = new Byte();
            b = 45;
            byte var = b;
            Console.WriteLine(var);


            Console.ReadKey();

        }
    }
}

PROGRAM 5: Using KeyChar to convert key info to key char. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Data_TypeConversion
{
    class Program
    {
        static void Main(string[] args)
        {
            char ch;
            ch = Console.ReadKey().KeyChar;
            Console.WriteLine("\nYou Pressed\t" + ch);
            Console.ReadKey();

        }
    }
}
PROGRAM 6: Checking either pressed character is a alphabetical letter or not. 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Data_TypeConversion
{
    class Program
    {
        static void Main(string[] args)
        {
            char ch;
            Console.WriteLine("Enter any character");
            ch = Console.ReadKey().KeyChar;
            if (Char.IsLetter(ch))
            {
                Console.WriteLine("\nIt is Letter");
            }
            else
                Console.WriteLine("\nNot a letter");
            Console.ReadKey();

        }
    }
}



PROGRAM 7: Checking pressed character is in lower case or upper case. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Data_TypeConversion
{
    class Program
    {
        static void Main(string[] args)
        {
            char ch;
            Console.WriteLine("Enter any character");
            ch = Console.ReadKey().KeyChar;
            if (Char.IsLower(ch))
            {
                Console.WriteLine("\nLower Case Letter");
            }
            else
                Console.WriteLine("\n Upper Case Letter");
            Console.ReadKey();

        }
    }
}


PROGRAM 8: Simple calculator program in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Data_TypeConversion
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("<<<<<<<<<<<< WELCOME TO ADVANCED CALCULATOR>>>>>>>>>>>>");
            Console.WriteLine("\t\t<<<<<<< Made By Aptech Students >>>>>>>>>>");
            Console.WriteLine("\t\t????????<<Batech: IPRO 2015 12 G>>????????\n");
            Console.WriteLine("[1]\t Standard Mode");
            Console.WriteLine("[2]\t Scientific Mode");
            Console.WriteLine("[3]\t Programmer Mode");
            Console.WriteLine("[4]\t Quit");
            Console.WriteLine("Enter Your Choice:");
            char choice = Console.ReadKey().KeyChar;
            if (choice == '1')
            {
                Console.Clear();
                Console.WriteLine("<<<<<<<Welcome to Standard Mode of Calculator>>>>>>");
                Console.WriteLine("[1]\tAddition");
                Console.WriteLine("[2]\tSubtraction");
                Console.WriteLine("[3]\tMultiplication");
                Console.WriteLine("[4]\tDivision");
                Console.WriteLine("[q]\tTo Quit");
                Console.WriteLine("You Choice:");

                char op = Console.ReadKey().KeyChar;
                int n1, n2;
                if (op == '1')
                {
                   
                    Console.WriteLine("You are now in Addition Operatiion");
                    Console.WriteLine("\nEnter Value 1");
                    n1 = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("\nEnter Value 2");
                    n2 = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("{0}+{1}={2}", n1, n2, n1 + n2);
                       
                    }

                else if (op == '2')
                {
                    Console.WriteLine("You are now in Subtraction Operatiion");
                    Console.WriteLine("\nEnter Value 1");
                    n1 = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("\nEnter Value 2");
                    n2 = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("{0}-{1}={2}", n1, n2, n1 - n2);

                }

                else if (op == '3')
                {
                    Console.WriteLine("You are now in Multiplication Operatiion");
                    Console.WriteLine("\nEnter Value 1");
                    n1 = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("\nEnter Value 2");
                    n2 = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("{0}*{1}={2}", n1, n2, n1 * n2);

                }

                else
                    if (op == '4')
                    {
                        Console.WriteLine("You are now in Divide Operatiion");
                        Console.WriteLine("\nEnter Value 1");
                        n1 = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("\nEnter Value 2");
                        n2 = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("{0}/{1}={2}", n1, n2, n1 / n2);

                    }

                    else
                        if (op == 'q')
                        {
                            Environment.Exit(0);
                        }
                        else
                            Console.WriteLine("Wrong Operator");

            }
            else if (choice == '2')
            {
                Console.Clear();
                Console.WriteLine("<<<<<<<Welcome to Scientific Mode of Calculator>>>>>>");
                Console.WriteLine("[1]\tSin");
                Console.WriteLine("[2]\tCos");
                Console.WriteLine("[3]\ttan");
                Console.WriteLine("[4]\tsquare");
                Console.WriteLine("[5]\tCube");
            }
               


            else if (choice == '3')
            {
                Console.Clear();
                Console.WriteLine("<<<<<<<Welcome to Programmer Mode of Calculator>>>>>>");
                Console.WriteLine("[1]\tBin");
                Console.WriteLine("[2]\tHex");
                Console.WriteLine("[3]\tOct");
                Console.WriteLine("[4]\tDec");
               
            }
            else if (choice == '4')
            {
                Environment.Exit(0);
            }
            else
                Console.WriteLine("\nWrong Choice");
           


            Console.ReadKey();

        }
    }
}


1 Comments:

 
TOP