Friday, May 25, 2012

Context Menu Strip Control In C#

Context Menu Strip:
C# context menu strips are the menus that pop up when the user clicks with the right hand mouse button over a control or area in a Windows based form. They are called context menus because the menu is usually specific to the object over which the mouse was clicked. Context menus are also sometimes referred to as Popup Menus or Shortcut menus. 
 
Now I am going to explain how to add context strip to our form and how to add events to that menu items using an example.  
 
Example program:
 
Steps:
1)      Open a windows application.
2)      Drag the Context strip menu from tool box into form
3)      Double Click on Context Menu Strip  and enter the text in the available box on form as shown below
                            
 
 
 
    4)      Enter number menu items u want add for that context menu strip. In this example I am adding 4 items those are 1) add 2) Sub 3) Clear 4) Exit.
   5)      Add three text boxes and labels as look like below.
 
   6)      Open the Form Properties (by right click with mouse and select properties). Select Context Menu Strip Property and select ContextMenuStrip1 from drop down list. 
  7)      To write the code for menu items Double Click on each item and write the following code in event handlers.
 
private void addToolStripMenuItem_Click(object sender, EventArgs e)
 {
   int result = int.Parse(textBox1.Text) +    int.Parse(textBox2.Text);
   textBox3.Text = Convert.ToString(result);
 }
private void subToolStripMenuItem_Click(object sender, EventArgs e)
 {
    int result = int.Parse(textBox1.Text) - int.Parse(textBox2.Text);
    textBox3.Text = Convert.ToString(result);
 }
  private void xToolStripMenuItem_Click(object sender, EventArgs e)
  {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
  }
  private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
  {
            Application.Exit();
  } 
 
Output:  


 
 
Reason for Output:
1)      Entered 100 in text box1
2)      Entered 200 in text box2
3)      Selected Add from Context Menu Strip.
      4)      Result 300  is displayed in text box3

 
 

Thursday, May 17, 2012

Checking Valid date or not using C#

In this post I am going to explain about how to check the given string is valid date or not.For VB.NET we have the predefined function called “ISDATE()” however for the C# we don’t have any predefined function to check the given string is valid or not.So in this post I am going to Explain given string is valid or not.
To do this Create an console application and copy the following code.


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
           string s1 = "29-FEB-20112";
           IsDate(s1);
        }

        public static void IsDate(string s1)
        {
            bool bSuccess = false;
            DateTime d1;
          
            bSuccess = DateTime.TryParse(s1, out d1);
            if (bSuccess == true)
            {
                Console.WriteLine(d1.ToString());
               

            }
            else
            {
                Console.WriteLine("Not a date at all");
            }
            Console.Read();
        }
    }
}


If you observe the above program I have used the DateTime.TryParse(). It Converts the specified string representation of a date and time to its Datetime equivalent using the specified culture-specific format information and formatting style, and returns a value that indicates whether the conversion succeeded. It takes Two Argument one is string which need to convert.Second one is the out varable of Datetime.If the conversion is success the date will store in the out varaible and returns true else returns false.

Output: 


Tuesday, May 15, 2012

Reading CSV file and Displaying Data

In this article I am going to explain about how to read the comma separated value file and how to display the values. one of my application I got this requirement Reading CSV file and displaying Data.
CSV stands for Comma Separated Values, sometimes also called Comma Delimited. A CSV file is a specially formatted plain text file which stores spreadsheet or basic database-style information in a very simple format, with one record on each line, and each field within that record separated by a comma.
Let us create a sample CSV file with the following Data and having the columns ID,FirstName,LastName and save this file as 1.csv
The following figure will give the CSV file that I have used in the program.

Then after designing the CSV create a console application and add the following namespace .
using System.IO;
ReadAllLines method can be used to read the contents of a CSV file to array. Each line of the text file will become the elements of the array. ReadAllLines method opens a text file, reads all lines of the file into a string array, and then closes the file.
Then write the following code to read the CSV.


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
           ReadFile("C:\\Documents and Settings\\Administrator\\Desktop\\1.csv");
        }

        public static void ReadFile(string Filepath)
        {
            string[] Filecontent = File.ReadAllLines(Filepath);
            int lno = 0;
            foreach (string line in Filecontent)
            {
                string[] Linevalues = line.Split(',');
                Console.WriteLine("Line  : " + lno.ToString() + "values are as follows");
                Console.WriteLine("______________________________________");
                Console.WriteLine("ID : "+ Linevalues[0].ToString());
                Console.WriteLine("First Name : "+ Linevalues[1].ToString());
                Console.WriteLine("Last Name : "+ Linevalues[2].ToString());
                Console.WriteLine("______________________________________");
                lno = lno + 1;
            }
            Console.ReadKey();
        }
    }
}

Output: