Answers to exercises in WinForms & .NET Library (Part1)

1. Create a form that takes loan amount and period, which is either 1 year or 2 years or 3 years.

The interest rates are as follows:

1 year - 10%
2 years - 12%
3 years - 15%

Calculate the total amount to be paid (amount + interest) and find out EMI (Every month installment) and display it to user.


using System;
using System.Drawing;
using System.Windows.Forms;

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

        private void btnCalculateEMI_Click(object sender, EventArgs e)
        {
            try
            {
                double amount = Double.Parse(txtLoanAmount.Text);
                int period = cmbPeriod.SelectedIndex + 1;

                double emi = 0;
                switch (period)
                {
                    case 1: emi = (amount + (amount * 10 / 100)) / 12; break;
                    case 2: emi = (amount + (amount * 12 / 100)) / 24; break;
                    case 3: emi = (amount + (amount * 15 / 100)) / 36; break;
                    default :
                        MessageBox.Show("Please select period for your instalment!", "Error");
                        return;
                }

                MessageBox.Show( String.Format ("Monthly Installment  : {0}", emi), "EMI");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Invalid Input. Please enter a valid amount for loan amount!", "Error");
            }
        }
    } // class
} // namespace

2. Accept a string from user through keyboard and display it vertically.

using System;
namespace st
{
    class StringVertical {
    
         public static void Main() {
             Console.Write("Enter a string : ");
             string st = Console.ReadLine();

             for ( int i  = 0 ; i < st.Length; i ++)
             {
                 Console.WriteLine (  st[i]);
             }
         } // main
    } // class 
} // namespace

3. Accept 10 strings from user and display the highest of all strings.


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

namespace st
{
    class LargestString
    {

        public static void Main()
        {

            string largest = "";
            for (int i = 1; i <= 10; i++)
            {
                Console.Write("Enter a string : ");
                string st = Console.ReadLine();

                if (st.CompareTo(largest) > 0)
                    largest = st;
            } // for

            Console.WriteLine("Largest of all strings : {0}", largest);
        } // main

    } // class 
} // namespace

4. Create a class to store day, month and year. Ensure object of this class are comparable.

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

namespace st
{
    class Date : IComparable
    {
        private int day, month, year;

        public Date(int day, int month, int year)
        {
            this.day = day;
            this.month = month;
            this.year = year;
        }

        public int CompareTo(Date other)
        {
             if ( this.year - other.year != 0 )
                 return  this.year - other.year;

             if (this.month - other.month != 0)
                 return this.month - other.month;

             return this.day - other.day;
        }
    }

    class TestDate
    {

        public static void Main()
        {
            Date d1 = new Date(10, 2, 2011);
            Date d2 = new Date(20, 1, 2011);

            Console.WriteLine(d1.CompareTo(d2));
        }
    } // TestDate
} // namespace

5. Accept a folder from user and a string. Display all files in the folder that contain the given string in the filename. Use a textbox to take folder name and another textbox for string. Use a Listbox to display the list of selected files.

    private void btnSearch_Click(object sender, EventArgs e)
    {
            DirectoryInfo d = new DirectoryInfo(txtFolder.Text);
            FileInfo[] files = d.GetFiles();
            lstFiles.Items.Clear();

            foreach (FileInfo f in files)
            {
                if (f.Name.Contains(txtString.Text))
                    lstFiles.Items.Add(f.Name);
            }
    }


6. Display only non blank lines of the given file.

using System;
using System.IO;

namespace st
{
    class NonBlankLines
    {
        public static void Main()
        {
            Console.Write("Enter a filename : ");
            string filename = Console.ReadLine();

            StreamReader sr = new StreamReader(filename);

            string line = sr.ReadLine();
            while (line != null)
            {

                if (line.Length > 0)
                    Console.WriteLine(line);
                line = sr.ReadLine();
            }
            sr.Close();
        } // Main
    }  // class
} // namespace

7. Display lines that contain the given string in the given file.

using System;
using System.IO;

namespace st
{
    class SearchFile
    {
        public static void Main()
        {
            Console.Write("Enter a filename : ");
            string filename = Console.ReadLine();

            Console.Write("Enter search string  : ");
            string searchstring = Console.ReadLine();


            StreamReader sr = new StreamReader(filename);

            string line = sr.ReadLine();
            while (line != null)
            {

                if (line.Contains(searchstring))
                    Console.WriteLine(line);

                line = sr.ReadLine();
            }
            sr.Close();

        } // Main
    } // class
} // namespace

8. Takes source file, target file, source string and target string. Replace all occurrences of source string to target string while writing content from source file to target file.

using System;
using System.IO;

namespace st
{
    class SearchReplace
    {
        public static void Main()
        {
            Console.Write("Enter a source file : ");
            string srcfilename = Console.ReadLine();

            Console.Write("Enter a target file : ");
            string trgfilename = Console.ReadLine();

            Console.Write("Enter search string  : ");
            string searchstring = Console.ReadLine();

            Console.Write("Enter replace string  : ");
            string replacestring = Console.ReadLine();

            StreamReader sr = new StreamReader(srcfilename);
            StreamWriter sw = new StreamWriter(trgfilename);

            string line = sr.ReadLine();
            while (line != null)
            {
                string newline = line.Replace(searchstring, replacestring);
                sw.WriteLine(newline);
                line = sr.ReadLine();
            }
            sr.Close();
            sw.Close();
        } // Main
    } // class
} // Namespace