Sie sind auf Seite 1von 33

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 Submitted To
Gagandeep Sethi Mr. Akash Bhardwaj
Roll Number: RE37D1B45 Lecturer in CAP406
BCA-MCA (Term-VII) Department of Computer
Registration# 3010070017 Applications
Lovely Professional University

34
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
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender,


EventArgs e)
{

private void button1_Click(object sender,


EventArgs e)
{
int range;
int k = 2, num;
Console.WriteLine("Enter the range of
series:");
num = int.Parse(textBox2.Text);
range = int.Parse(textBox1.Text);
if (range < num)
{
MessageBox.Show("Ending Range Must
be greater than Starting Range","Input Error");
}
else
{
do
{
while (k <= num - 1)
{
if (num % k == 0)
{
break;
}
k = k + 1;
if (num == k)
{
listBox1.Items.Add(num
);

break;
}

}
num = num + 1;
k = 2;
} while (num <= range);

}
Console.ReadLine();

}
}
}
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
{
class My_Class
{

public void Method(Boolean b)


{
Console.WriteLine("Number in BaseClass is " +
b);
}
public void Method(string st)
{
Console.WriteLine("String in BaseClass is " +
st);
}
}
class My_Derived: My_Class
{
public void Method(char ch)
{
Console.WriteLine("Character in DerivedClass is
" + ch);
}
public void Method(double dob)
{

Console.WriteLine("Double in DerivedClass
is " + dob);
}

class Program: My_Derived


{
static void Main(string[] args)
{
My_Derived drive_obj = new
My_Derived();
drive_obj.Method(true);
drive_obj.Method("Gagandeep Sethi");
drive_obj.Method('C');
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 = new My_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:
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
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button2_Click(object sender,


EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
}

private void button1_Click(object sender,


EventArgs e)
{

string []a=new string[100];


listBox1.Items.CopyTo(a, 0);
listBox2.Items.Add(a[0]);

}
}
Output:
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
{
class Program
{
public static void myMessage()
{
Console.ReadLine("Message from Inner
Namespace");
}
}
}
class NamespaceCall
{
public static void 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
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 directory
where 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 Text property of a Text Box represents
of a Text Box control 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:


U_Name.Text = "Gagandeep Sethi"; textBox1.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

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
{
public partial class Form1 : Form
{
public string file1;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender,


EventArgs e)
{

private void
openToolStripMenuItem_Click_1(object sender, EventArgs
e)
{

int size = -1;


DialogResult result =
openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test
result.
{
string file;
file = openFileDialog1.FileName;
try
{
richTextBox1.Text =
File.ReadAllText(file);

size = richTextBox1.Text.Length;
}
catch (IOException)
{
}
}
Console.WriteLine(size); // <-- Shows file
size in debugging mode.
Console.WriteLine(result); // <-- For
debugging use only.

private void menuStrip1_ItemClicked(object


sender, ToolStripItemClickedEventArgs e)
{

private void
saveAsToolStripMenuItem_Click_1(object sender,
EventArgs e)
{
saveAs();

public void saveAs()


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

string name = saveFileDialog1.FileName;


// Write to the file name selected.
// ... You can write the text from a
TextBox instead of a string literal.
File.WriteAllText(name,
richTextBox1.Text);
}
}
private void
saveToolStripMenuItem_Click_1(object sender, EventArgs
e)
{
save();
}
public void save()
{

if (file1 == null)
{
saveAs();
}

file1 = openFileDialog1.FileName;

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

private void newToolStripMenuItem_Click(object


sender, EventArgs e)
{
new Form1();
}

private void
exitToolStripMenuItem_Click_1(object sender, EventArgs
e)
{
Close();
}
}
}

Output:
When clicked Open Menu:
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.
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>
/// Summary description for Form1.
/// </summary>
public class Form1 : 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;
string winDir =
System.Environment.GetEnvironmentVariable("windir");
private System.Windows.Forms.ListBox listbox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container
components = null;

public Form1()
{
//
// Required for Windows Form Designer
support.
//
InitializeComponent();

//
// TO DO: Add any constructor code after
InitializeComponent call.
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code


/// <summary>
/// Required method for Designer support - do
not modify
/// the contents of this method with the code
editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new
System.Windows.Forms.Button();
this.button2 = new
System.Windows.Forms.Button();
this.button3 = new
System.Windows.Forms.Button();
this.listbox1 = new
System.Windows.Forms.ListBox();
this.button4 = new
System.Windows.Forms.Button();
this.button5 = new
System.Windows.Forms.Button();
this.button6 = new
System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new
System.Drawing.Point(216, 32);

this.button1.Name = "button1";
this.button1.Size = new
System.Drawing.Size(112, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new
System.Drawing.Point(216, 64);
this.button2.Name = "button2";
this.button2.Size = new
System.Drawing.Size(112, 23);
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.Click += new
System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new
System.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 += new
System.EventHandler(this.button3_Click);
//
// listbox1
//
this.listbox1.Location = new
System.Drawing.Point(24, 24);
this.listbox1.Name = "listbox1";
this.listbox1.Size = new
System.Drawing.Size(176, 199);
this.listbox1.TabIndex = 0;
//
// button4
//
this.button4.Location = new
System.Drawing.Point(216, 128);
this.button4.Name = "button4";
this.button4.Size = new
System.Drawing.Size(112, 23);
this.button4.TabIndex = 4;
this.button4.Text = "button4";
this.button4.Click += new
System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new
System.Drawing.Point(216, 160);
this.button5.Name = "button5";
this.button5.Size = new
System.Drawing.Size(112, 23);
this.button5.TabIndex = 5;
this.button5.Text = "button5";
this.button5.Click += new
System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new
System.Drawing.Point(216, 192);
this.button6.Name = "button6";
this.button6.Size = new
System.Drawing.Size(112, 23);
this.button6.TabIndex = 6;
this.button6.Text = "button6";
this.button6.Click += new
System.EventHandler(this.button6_Click);
//
// Form1
//
this.AutoScaleBaseSize = new
System.Drawing.Size(5, 13);
this.ClientSize = new
System.Drawing.Size(360, 273);
this.Controls.AddRange(new
System.Windows.Forms.Control[] {

this.button6,

this.button5,

this.button4,

this.button3,
this.button2,

this.button1,

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

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{

Application.Run(new Form1());
}

private void button6_Click(object sender,


System.EventArgs e)
{
//How to obtain list of files (example uses
Windows 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)
{
//How to read a text file.
//try...catch is to deal with a 0 byte
file.
this.listbox1.Items.Clear();
StreamReader reader = new
StreamReader(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 File
Information";
this.button4.Text = "List Drives";
this.button5.Text = "List Subfolders";
this.button6.Text = "List Files";
}
private void button5_Click(object sender,
System.EventArgs e)
{
//How to get a list of folders (example
uses 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 disk
drives.
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 (example
uses Notepad.exe).
this.listbox1.Items.Clear();
FileInfo FileProps = new FileInfo(winDir +
"\\notepad.exe");
addListItem("File Name = " +
FileProps.FullName);
addListItem("Creation Time = " +
FileProps.CreationTime);
addListItem("Last Access Time = " +
FileProps.LastAccessTime);
addListItem("Last Write TIme = " +
FileProps.LastWriteTime);
addListItem("Size = " + FileProps.Length);
FileProps = null;
}

private void addListItem(string value)


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

private void button2_Click(object sender,


System.EventArgs e)
{
//Demonstrates how to create and write to a
text file.
StreamWriter writer = new
StreamWriter("c:\\KBTest.txt");
writer.WriteLine("File created using
StreamWriter class.");
writer.Close();
this.listbox1.Items.Clear();
addListItem("File Written to
C:\\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;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Gagan_s_Note_Pad
{
public partial class Form1 : Form
{
public string file1;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender,


EventArgs e)
{

private void
openToolStripMenuItem_Click_1(object sender, EventArgs
e)
{

int size = -1;


DialogResult result =
openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test
result.
{
string file;
file = openFileDialog1.FileName;
try
{
richTextBox1.Text =
File.ReadAllText(file);

size = richTextBox1.Text.Length;
}
catch (IOException)
{
}
}
Console.WriteLine(size); // <-- Shows file
size in debugging mode.
Console.WriteLine(result); // <-- For
debugging use only.

private void menuStrip1_ItemClicked(object


sender, ToolStripItemClickedEventArgs e)
{

private void
saveAsToolStripMenuItem_Click_1(object sender,
EventArgs e)
{
saveAs();

public void saveAs()


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

string name = saveFileDialog1.FileName;


// Write to the file name selected.
// ... You can write the text from a
TextBox instead of a string literal.
File.WriteAllText(name,
richTextBox1.Text);
}
}
private void
saveToolStripMenuItem_Click_1(object sender, EventArgs
e)
{
save();

}
public void save()
{

if (file1 == null)
{
saveAs();
}

file1 = openFileDialog1.FileName;

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

private void newToolStripMenuItem_Click(object


sender, EventArgs e)
{
new Form1();
}

private void
exitToolStripMenuItem_Click_1(object sender, EventArgs
e)
{
Close();
}

private void fontToolStripMenuItem_Click(object


sender, EventArgs e)
{
if (fontDialog1.ShowDialog() ==
DialogResult.OK)
{
richTextBox1.Font = fontDialog1.Font;
}
}

private void
colorToolStripMenuItem_Click(object sender, EventArgs
e)
{
if (colorDialog1.ShowDialog() ==
DialogResult.OK)
{
richTextBox1.ForeColor =
colorDialog1.Color;
}
}
}
}
Output:
-------------------Assignment 3 Ends Here----------------------------------

Das könnte Ihnen auch gefallen