Top Banner

of 33

Assignment3 CAP406

Apr 10, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/8/2019 Assignment3 CAP406

    1/33

    34

    Modern Programming Tools and Tech.-II (CAP406)

    Homework#3

    Based On:Namespaces, collections, Windows controls dialog boxes

    and file handling in binary mode only

    Date of Submission: November 7, 2010

    Submitted By

    Gagandeep Sethi

    Roll Number: RE37D1B45

    BCA-MCA (Term-VII)

    Registration# 3010070017

    Submitted To

    Mr. Akash Bhardwaj

    Lecturer in CAP406

    Department of Computer

    Applications

    Lovely Professional University

  • 8/8/2019 Assignment3 CAP406

    2/33

    COURSE CODE: CAP406

    COURSE NAME: Modern Programming Tools & Techniques II

    Homework No. 3

    PART A

    Question#1 Write a Program to print all prime numbers in the range 99 to

    9999 in a list box.

    Code: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;

    namespace prime_series1{ publicpartialclassForm1 : Form

    { public Form1()

    {

    InitializeComponent();}

    private void Form1_Load(object sender,EventArgs e)

    {

    }

    privatevoid button1_Click(object sender,EventArgs e){

    int range; int k = 2, num; Console.WriteLine("Enter the range ofseries:");

  • 8/8/2019 Assignment3 CAP406

    3/33

    num = int.Parse(textBox2.Text);range = int.Parse(textBox1.Text);

    if (range < num){

    MessageBox.Show("Ending Range Mustbe greater than Starting Range","Input Error");

    } else

    { do

    { while (k

  • 8/8/2019 Assignment3 CAP406

    4/33

    Question#2 Write a program to implement function overloading.

    Code:

    Function Overloading:

    When you have multiple methods in a class having the same name but different

    signatures (parameters), they are known as overloaded methods.

    Following program illustrates function overloading:

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

    namespace func_overloading

  • 8/8/2019 Assignment3 CAP406

    5/33

    { classMy_Class

    {

    publicvoid Method(Boolean b){

    Console.WriteLine("Number in BaseClass is " +b);

    } publicvoid Method(string st)

    { Console.WriteLine("String in BaseClass is " +st);

    }

    } classMy_Derived: My_Class{publicvoid Method(char ch){

    Console.WriteLine("Character in DerivedClass is" + ch);

    } publicvoid Method(double dob)

    {

    Console.WriteLine("Double in DerivedClass

    is " + dob);}

    }

    classProgram: My_Derived{

    staticvoid Main(string[] args){

    My_Derived drive_obj = newMy_Derived();

    drive_obj.Method(true);drive_obj.Method("Gagandeep Sethi");drive_obj.Method('C');

  • 8/8/2019 Assignment3 CAP406

    6/33

    drive_obj.Method(55.567); Console.ReadLine();

    }}

    }

    Explanation:

    Here, base class My_Class contains two overloaded methods of named as

    Method having arguments of Boolean and String type respectively and

    Derived class My_Derived has inherited base class My_Class and it also

    contains two methods of same name as the base class contains i.e. Method

    having argument of type Char and double and when the object of derived class

    is created in main class like:

    My_Derived drive_obj = newMy_Derived();drive_obj.Method(true);drive_obj.Method("Gagandeep Sethi");drive_obj.Method('C');drive_obj.Method(55.567);

    Then corresponding methods will be called according to their signatures and the

    output will be as follows:

  • 8/8/2019 Assignment3 CAP406

    7/33

    Question#3 Write a Program to copy content of a list box to another list box.

    Code: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;

    namespace copy_list{ publicpartialclassForm1 : Form

    { public Form1()

    {InitializeComponent();

    }

    privatevoid button2_Click(object sender,EventArgs e)

    {

  • 8/8/2019 Assignment3 CAP406

    8/33

    listBox1.Items.Add(textBox1.Text);}

    privatevoid button1_Click(object sender,EventArgs e)

    {

    string []a=newstring[100];listBox1.Items.CopyTo(a, 0);listBox2.Items.Add(a[0]);

    }

    }}

    Output:

  • 8/8/2019 Assignment3 CAP406

    9/33

    Question#4 Write a program to implement namespaces.

    Code:

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

    namespace My_Namespaces{ namespace Inner

    { classProgram

    { publicstaticvoid myMessage()

    { Console.ReadLine("Message from InnerNamespace");

    }}

  • 8/8/2019 Assignment3 CAP406

    10/33

    } classNamespaceCall

    { publicstaticvoid main(string[] args)

    {Inner.Program.myMessage();Console.ReadLine();

    }

    }}

    Output:

    Question#5 According to you how the System Namespace and System .dll

    Answer:

    Namespaces are collections of objects, with every Namescape containing different

    sets of objects grouped according to functionality. For example, the System.Data

  • 8/8/2019 Assignment3 CAP406

    11/33

    namespace contains all the classes you will need to interact with data sources.

    Without System.Data, it'd be impossible for ASP.NET to function hand-in-hand

    with ADO.NET. It contains methods you can use to connect to databases and a few

    methods you can use to handle XML files. However if you want ASP.NET to

    manipulate XML data, the System.XML Namespace would come in handy.

    System.Drawing, another namespace, contain classes you can use to manipulate

    images. There are so many Namespaces, it'd be impossible to describe them all in

    one tutorial, and it is important that you learn what a Namespace is.

    Namespaces are contained in files called assemblies. These files have DLL

    extensions. The System Namespace is in System.DLL, but not all namespaces have

    DLLs matching their names. For example, System.Web.UI is in System.Web.DLL.

    However these DLL files are compiled into the Microsoft Intermediate Language

    (MSIL) which is understood by the Common Language Routine (CLR). Therefore

    they are different than normal DLLs. These files can be found in the directorywhere the .NET Framework was installed. With the dissambler Ildasm.exe, you

    can actually see the classes, functions, etc that these Namespaces contain.

    System --> in System.dll

    System.Data --> in System.Data.dll

    System.Deployment --> System.Deployment.dll

    System.Drawing --> System.Drawing.dll

    System.Windows.Forms --> System.Windows.Forms.dll

    PART B

    Question#6 Compare and Contrast the Name and Text Property of textbox

    control.

    Answer:

    Name Property Text Property

    Name property represents a unique name

    of a Text Box control

    Text property of a Text Box represents

    the current text of a Text Box control.

    It is used to access the control in the code. It is used as value or content of the

    text box.

    Example code Snippet: Example code Snippet:

  • 8/8/2019 Assignment3 CAP406

    12/33

    U_Name.Text = "Gagandeep Sethi";

    U_Name.Enabled=False;

    U_Name.ForeColor="Red";

    U_Name.Multiline=True

    Where U_Name is the name of TextBox1

    and through that name we can access

    other properties and methods for the

    textBox1

    textBox1.Text = "Gagandeep Sethi";

    Question#7 Do you feel that a default unnamed global namespace is always

    there in a C# program?

    Answer:

    Whether or not you explicitly declare a namespace in a C# source file, the compiler

    adds a default namespace. This unnamed namespace, sometimes called the global

    namespace, is present in every file. Any identifier in the global namespace is

    available for use in a named namespace.

    Question#8 Using rich Text box, create an editor and Open an existing file

    using OpenFileDialog box then write some text on that file and save same file

    using SaveFileDialog box

    Answer: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.IO;

    namespace Gagan_s_Note_Pad{ publicpartialclassForm1 : Form

  • 8/8/2019 Assignment3 CAP406

    13/33

    { publicstring file1; public Form1()

    {InitializeComponent();

    }

    private void Form1_Load(object sender,EventArgs e)

    {

    }

    private void

    openToolStripMenuItem_Click_1(object sender, EventArgse){

    int size = -1;

    DialogResult result =openFileDialog1.ShowDialog(); // Show the dialog. if (result == DialogResult.OK) // Testresult.

    {

    string file;file = openFileDialog1.FileName;

    try{

    richTextBox1.Text =File.ReadAllText(file);

    size = richTextBox1.Text.Length;}

    catch (IOException){}

    } Console.WriteLine(size); //

  • 8/8/2019 Assignment3 CAP406

    14/33

    Console.WriteLine(result); //

  • 8/8/2019 Assignment3 CAP406

    15/33

    } publicvoid save()

    {

    if (file1 == null){

    saveAs();}

    file1 = openFileDialog1.FileName;

    File.WriteAllText(file1,richTextBox1.Text); MessageBox.Show("file saved");

    }

    privatevoid newToolStripMenuItem_Click(objectsender, EventArgs e)

    { newForm1();

    }

    private voidexitToolStripMenuItem_Click_1(object sender, EventArgs

    e){

    Close();}

    }}

    Output:

  • 8/8/2019 Assignment3 CAP406

    16/33

    When clicked Open Menu:

  • 8/8/2019 Assignment3 CAP406

    17/33

    The contents will be fetched from the selected file into the rich text box as:

    Edit this content and the same file will be modified by clicking on the save menu

    and to create new file with the edited content we have to click to save as option and

    give the new name to that file with extension.

  • 8/8/2019 Assignment3 CAP406

    18/33

    Question#9 Write a program to explore files from disk drives using drive,

    directory and file list box controls.

    Answer:using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;

    using System.IO;

    namespace fso_cs{

    /// /// Summary description for Form1. /// publicclassForm1 : System.Windows.Forms.Form

    { private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; private System.Windows.Forms.Button button5; private System.Windows.Forms.Button button6;

  • 8/8/2019 Assignment3 CAP406

    19/33

    string winDir =System.Environment.GetEnvironmentVariable("windir"); private System.Windows.Forms.ListBox listbox1; /// /// Required designer variable. /// private System.ComponentModel.Containercomponents = null;

    public Form1(){

    //// Required for Windows Form Designer

    support.

    //InitializeComponent();

    //// TO DO: Add any constructor code after

    InitializeComponent call. //

    }

    ///

    /// Clean up any resources being used. /// protectedoverridevoid Dispose(bool disposing)

    { if (disposing)

    { if (components != null)

    {components.Dispose();

    }}

    base.Dispose(disposing);}

    #region Windows Form Designer generated code ///

  • 8/8/2019 Assignment3 CAP406

    20/33

    /// Required method for Designer support - donot modify /// the contents of this method with the codeeditor. /// privatevoid InitializeComponent()

    { this.button1 = newSystem.Windows.Forms.Button(); this.button2 = newSystem.Windows.Forms.Button(); this.button3 = newSystem.Windows.Forms.Button(); this.listbox1 = new

    System.Windows.Forms.ListBox(); this.button4 = newSystem.Windows.Forms.Button(); this.button5 = newSystem.Windows.Forms.Button(); this.button6 = newSystem.Windows.Forms.Button(); this.SuspendLayout(); //

    // button1

    //this.button1.Location = new

    System.Drawing.Point(216, 32);

    this.button1.Name = "button1"; this.button1.Size = newSystem.Drawing.Size(112, 23); this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.Click += newSystem.EventHandler(this.button1_Click); //

    // button2 //

    this.button2.Location = newSystem.Drawing.Point(216, 64);

  • 8/8/2019 Assignment3 CAP406

    21/33

    this.button2.Name = "button2"; this.button2.Size = newSystem.Drawing.Size(112, 23); this.button2.TabIndex = 2; this.button2.Text = "button2"; this.button2.Click += newSystem.EventHandler(this.button2_Click); //

    // button3 //

    this.button3.Location = newSystem.Drawing.Point(216, 96); this.button3.Name = "button3"; this.button3.Size = new

    System.Drawing.Size(112, 23); this.button3.TabIndex = 3; this.button3.Text = "button3"; this.button3.Click += newSystem.EventHandler(this.button3_Click); //

    // listbox1 //

    this.listbox1.Location = newSystem.Drawing.Point(24, 24);

    this.listbox1.Name = "listbox1"; this.listbox1.Size = newSystem.Drawing.Size(176, 199); this.listbox1.TabIndex = 0; //

    // button4 //

    this.button4.Location = newSystem.Drawing.Point(216, 128); this.button4.Name = "button4"; this.button4.Size = newSystem.Drawing.Size(112, 23); this.button4.TabIndex = 4; this.button4.Text = "button4"; this.button4.Click += newSystem.EventHandler(this.button4_Click);

  • 8/8/2019 Assignment3 CAP406

    22/33

    //// button5

    //this.button5.Location = new

    System.Drawing.Point(216, 160); this.button5.Name = "button5"; this.button5.Size = newSystem.Drawing.Size(112, 23); this.button5.TabIndex = 5; this.button5.Text = "button5"; this.button5.Click += newSystem.EventHandler(this.button5_Click); //

    // button6

    // this.button6.Location = newSystem.Drawing.Point(216, 192); this.button6.Name = "button6"; this.button6.Size = newSystem.Drawing.Size(112, 23); this.button6.TabIndex = 6; this.button6.Text = "button6"; this.button6.Click += newSystem.EventHandler(this.button6_Click);

    //// Form1

    //this.AutoScaleBaseSize = new

    System.Drawing.Size(5, 13); this.ClientSize = newSystem.Drawing.Size(360, 273); this.Controls.AddRange(newSystem.Windows.Forms.Control[] {this.button6,this.button5,this.button4,this.button3,

  • 8/8/2019 Assignment3 CAP406

    23/33

    this.button2,this.button1,

    this.listbox1}); this.Name = "Form1"; this.Text = "Form1"; this.Load += newSystem.EventHandler(this.Form1_Load); this.ResumeLayout(false);

    }#endregion

    /// /// The main entry point for the application. ///

    [STAThread] staticvoid Main()

    {

    Application.Run(newForm1());}

    private void button6_Click(object sender,System.EventArgs e)

    { //How to obtain list of files (example usesWindows folder). this.listbox1.Items.Clear(); string[] files =Directory.GetFiles(winDir); foreach (string i in files)

    {addListItem(i);

    }}

    private void button1_Click(object sender,System.EventArgs e)

  • 8/8/2019 Assignment3 CAP406

    24/33

    { //How to read a text file. //try...catch is to deal with a 0 bytefile. this.listbox1.Items.Clear(); StreamReader reader = newStreamReader(winDir + "\\system.ini"); try

    { do

    {addListItem(reader.ReadLine());

    } while (reader.Peek() != -1);

    }

    catch{

    addListItem("File is empty");}

    finally{

    reader.Close();

    }

    }

    private void Form1_Load(object sender,System.EventArgs e)

    { this.button1.Text = "Read Text File"; this.button2.Text = "Write Text File"; this.button3.Text = "View FileInformation"; this.button4.Text = "List Drives"; this.button5.Text = "List Subfolders"; this.button6.Text = "List Files";

    }

  • 8/8/2019 Assignment3 CAP406

    25/33

    private void button5_Click(object sender,System.EventArgs e)

    { //How to get a list of folders (exampleuses Windows folder).

    this.listbox1.Items.Clear(); string[] dirs =Directory.GetDirectories(winDir); foreach (string dir in dirs)

    {addListItem(dir);

    }

    }

    private void button4_Click(object sender,System.EventArgs e)

    { //Demonstrates how to obtain a list of diskdrives. this.listbox1.Items.Clear(); string[] drives =Directory.GetLogicalDrives();

    foreach (string drive in drives){

    addListItem(drive);}

    }

    private void button3_Click(object sender,System.EventArgs e)

    { //How to retrieve file properties (exampleuses Notepad.exe). this.listbox1.Items.Clear(); FileInfo FileProps = newFileInfo(winDir +"\\notepad.exe");

    addListItem("File Name = " +FileProps.FullName);

  • 8/8/2019 Assignment3 CAP406

    26/33

    addListItem("Creation Time = " +FileProps.CreationTime);

    addListItem("Last Access Time = " +FileProps.LastAccessTime);

    addListItem("Last Write TIme = " +FileProps.LastWriteTime);

    addListItem("Size = " + FileProps.Length);FileProps = null;

    }

    privatevoid addListItem(string value){

    this.listbox1.Items.Add(value);}

    private void button2_Click(object sender,System.EventArgs e)

    { //Demonstrates how to create and write to atext file. StreamWriter writer = newStreamWriter("c:\\KBTest.txt");

    writer.WriteLine("File created usingStreamWriter class.");

    writer.Close(); this.listbox1.Items.Clear();

    addListItem("File Written toC:\\KBTest.txt");

    }}

    }

    Question#10 Using rich Text box, create an editor and perform font and color

    setting of text using font and color dialog boxes.Code:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;

  • 8/8/2019 Assignment3 CAP406

    27/33

    using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;

    namespace Gagan_s_Note_Pad{ publicpartialclassForm1 : Form

    { publicstring file1; public Form1()

    {InitializeComponent();

    }

    private void Form1_Load(object sender,EventArgs e)

    {

    }

    private voidopenToolStripMenuItem_Click_1(object sender, EventArgse)

    {

    int size = -1; DialogResult result =openFileDialog1.ShowDialog(); // Show the dialog. if (result == DialogResult.OK) // Testresult.

    { string file;

    file = openFileDialog1.FileName; try

    {richTextBox1.Text =

    File.ReadAllText(file);

    size = richTextBox1.Text.Length;

  • 8/8/2019 Assignment3 CAP406

    28/33

    } catch (IOException)

    {}

    } Console.WriteLine(size); //

  • 8/8/2019 Assignment3 CAP406

    29/33

    } private voidsaveToolStripMenuItem_Click_1(object sender, EventArgse)

    {save();

    } publicvoid save()

    {

    if (file1 == null){

    saveAs();

    }

    file1 = openFileDialog1.FileName;

    File.WriteAllText(file1,richTextBox1.Text); MessageBox.Show("file1 saved");

    }

    privatevoid newToolStripMenuItem_Click(object

    sender, EventArgs e){

    newForm1();}

    private voidexitToolStripMenuItem_Click_1(object sender, EventArgse)

    {Close();

    }

    privatevoidfontToolStripMenuItem_Click(objectsender, EventArgs e)

    {

  • 8/8/2019 Assignment3 CAP406

    30/33

    if (fontDialog1.ShowDialog() ==DialogResult.OK)

    {richTextBox1.Font = fontDialog1.Font;

    }}

    private voidcolorToolStripMenuItem_Click(object sender, EventArgse)

    { if (colorDialog1.ShowDialog() ==DialogResult.OK)

    {

    richTextBox1.ForeColor =colorDialog1.Color;

    }}

    }}

    Output:

  • 8/8/2019 Assignment3 CAP406

    31/33

  • 8/8/2019 Assignment3 CAP406

    32/33

  • 8/8/2019 Assignment3 CAP406

    33/33

    -------------------Assignment 3 Ends Here----------------------------------