Anda di halaman 1dari 15

Visual Programmi ng

September 5

2011
Assignment

Submitted by: Rishabh Saini I.T.3-B,Reg No.:1080920052

Program 1: Develop a console application to explain hierarchical inheritance Solution:


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

namespace Assignmentprog1 { class Program { static void Main(string[] args) { ashok t = new ashok(); t.print1(); t.print2(); t.show(); honda c = new honda(); c.show1(); c.show2(); c.show(); } } class vehicle { public void show() { Console.WriteLine("I am the Biggest and the heaviest"); } } class car : vehicle { public void show1() { Console.WriteLine("I m a small running home of all humans"); } } class truck : vehicle { public void print1() { Console.WriteLine("all big transporters use me"); } } class honda : car { public void show2() { Console.WriteLine("My best IS HONDA CIVIC"); } } class ashok : truck { public void print2() { Console.WriteLine("I make big vehicles in India");

Console.ReadLine(); } } }

Output:

Program 2: Develop a windows application to form a calculator which performs all arithmetic operations. Solution: Properties:
Control Label1 Label2 Label3 Label4 PropertyName First num Second num Result result

Radiobutton 1 Radiobutton2 Radiobutton3 Radiobutton4 Button1 Button2

Add Subtract Multiply Divide Do Clear

Coding:
using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms;

namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void textBox1_TextChanged(object sender, EventArgs e) { } private void textBox2_TextChanged(object sender, EventArgs e) { } private void textBox3_TextChanged(object sender, EventArgs e) { } private void radioButton1_CheckedChanged(object sender, EventArgs e) { } private void radioButton2_CheckedChanged(object sender, EventArgs e) { } private void radioButton3_CheckedChanged(object sender, EventArgs e)

{ } private void radioButton4_CheckedChanged(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { if (radioButton1.Checked==true) { int a, b, c; a = Convert.ToInt16(textBox1.Text); b = Convert.ToInt16(textBox2.Text); c = a + b; label4.Text = Convert.ToString(c); } if (radioButton2.Checked==true) { int a, b, c; a = Convert.ToInt16(textBox1.Text); b = Convert.ToInt16(textBox2.Text); c = a - b; label4.Text = Convert.ToString(c); } if (radioButton3.Checked==true) { int a, b, c; a = Convert.ToInt16(textBox1.Text); b = Convert.ToInt16(textBox2.Text); c = a * b; label4.Text = Convert.ToString(c); } if (radioButton4.Checked==true) { int a, b, c; a = Convert.ToInt16(textBox1.Text); b = Convert.ToInt16(textBox2.Text); c = a / b; label4.Text = Convert.ToString(c); } } private void button1_Click(object sender, EventArgs e) { textBox1.Text = " "; textBox2.Text = " "; label4.Text = " "; } } }

Output:

Program 3:Design a MDI text editor(any child window) application using c# windows application. Solution:

Coding:
using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms;

namespace WindowsFormsApplication1 { public partial class Form1 : Form { private int childNumber = 0; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form child = new Form(); child.MdiParent = this; child.Text = "Harsh" + childNumber++; child.Show(); } } }

Output:

Problem 4:Design a windows application that illustrate list control and user defined dialog box for displaying the first name, last name, job title of the employee in a company. Write the steps in designing the application with properties designed. Solution: Properties:

Control Label1 Label2 Label3 Button1 Button2

Property name First Name Last Name Job Title Ok Cancel

Steps: 1. At first we need a private member to hold the employee object the dialog is working on. 2. Then we are using the public employee property. So that Other code use dialog can get the employee data.

Coding:
using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms;

namespace Employee1 { public class employee { public string FirstName; public string LastName; public string JobTitle; public override string ToString() { return FirstName + " " + LastName + " " + JobTitle; } }; public partial class Dialog : Form { public Dialog() { InitializeComponent(); } private employee _employee = null; public employee staffMember { get {

if (_employee == null) { _employee = new employee(); } _employee.FirstName = textBox1.Text;

_employee.LastName = textBox2.Text; _employee.JobTitle = textBox3.Text; return _employee; } set {

_employee = textBox1.Text textBox2.Text textBox3.Text

value; = _employee.FirstName; = _employee.LastName; = _employee.JobTitle;

} } } }

3.

Now double click the Form1.cs in the solution explorer to bring up the main form in the designer window. Drop some control onto the form so that it looks like..

Properties: Control List Box Label1 Label2 Label3 Button1 Button2 Button3 Property Name Employee List firstName LastName JobTitle Add Edit Delete

4. The Code is written to add new item to the list box. Double click the add button to drag into its click event Handler. All we need to do here is show the dialog and then add the new employee object,it creates into the list box. Coding:
using using using using using using System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms;

namespace Employee1 { public partial class Form1 : Form { public Form1()

{ } InitializeComponent();

private void button1_Click(object sender, EventArgs e) { dialog newEmployeeDialog = new dialog(); if (newEmployeeDialog.ShowDialog()==dialogResult.OK) { listBox1.Items.Add(newEmployeeDialog.staffMember); } } private void button3_Click(object sender, EventArgs e) { if (listBox1.SelectedIndex == -1) return; if (MessageBox.Show("Really Delete", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == dialogResult.Yes) { listBox1.Items.Remove(listBox1.SelectedItem); }

} private void button2_Click(object sender, EventArgs e) { if (listBox1.SelectedIndex == -1) return; int employeeNum = listBox1.SelectedIndex; dialog newEmployeeDialog = new dialog(); newEmployeeDialog.staffMember = (employee)listBox1.SelectedItem; if (newEmployeeDialog.ShowDialog()==dialogResult.OK) { listBox1.Items.RemoveAt(employeeNum); listBox1.Items.Insert(employeeNum, newEmployeeDialog.staffMember); listBox1.SelectedIndex = employeeNum; } } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { if (listBox1.SelectedIndex!=-1) { employee currentEmployee=(employee)listBox1.SelectedItem; textBox1.Text = currentEmployee.FirstName; textBox2.Text=currentEmployee.LastName; textBox3.Text = currentEmployee.JobTitle;

} else { } }

textBox1.Text=textBox2.Text=textBox3.Text="";

private void Form1_Load(object sender, EventArgs e) { } }

OutPut:

Program 5: Design a windows application to illustrate tree view control to list out odd and even number. Also design to add, image, colour, etc. Solution:

Steps: 1. Open a new form and drag button1 called load. 2. Drag tree view from toolbox. 3. Double click on load button and write the coding. Coding:
using using using using using System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms;

namespace treeview { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { }

private void button1_Click(object sender, EventArgs e) { treeView1.Nodes.Clear(); treeView1.ShowNodeToolTips = true; TreeNode evenNumbers = treeView1.Nodes.Add("even numbers"); TreeNode oddNumbers = treeView1.Nodes.Add("odd numbers"); /* TreeNode evenNumbers = treeView1.Nodes.Add("even", "even numbers", 0, 1); TreeNode oddNumbers = treeView1.Nodes.Add("odd", "odd numbers", 0, 1);*/ evenNumbers.BackColor = Color.Red; evenNumbers.ForeColor = Color.Yellow; evenNumbers.ToolTipText = "the evens"; oddNumbers.BackColor = Color.Blue; oddNumbers.ForeColor = Color.Red; oddNumbers.ToolTipText = "the odds"; for (int i = 1; i < 500; i++) { if (i % 2 == 0) { evenNumbers.Nodes.Add(i.ToString()); } else { oddNumbers.Nodes.Add(i.ToString()); } } } } }

Output: