c# Lab Ready for Print Out

Preview:

DESCRIPTION

6th semister lab c#.NET.....wat a pain

Citation preview

EXP NO : 1

USAGE OF STRUCTURES

AIM: To write a program in C# Language to use structures.

ALGORITHM:

1. Get number of students, say n.2. Get the details for n students.3. Compute their average marks of two subjects and show as a report.

1

FLOWCHART: USAGE OF STRUCTURE

Start

Create a structure for student with required

fields.

Input all the fields inside the student class using the method getdata().

Display all the details inside the student class

using the method showdata().

Call the defined function using the object created for student class inside

the main class.

Stop

2

PROGRAM:

using System; public struct stud { public String name;

public int m1,m2; public double avg;

};

public class stud {

stud [ ]s=new stud[15];public void getData (int n){ for(int i=0;i<=1;i++){ Console.WriteLine("Enter The Name"); s[i].name=Console.ReadLine(); Console.WriteLine("Enter The Mark1"); s[i].m1=Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter The Mark2"); s[i].m2=Convert.ToInt32(Console.ReadLine()); s[i].avg=(s[i].m1+s[i].m2)/2;}

}

public void showData (int n) {

for(int i=0;i<n;i++)Console.WriteLine("Name:"+s[i].name+ “\nMark1:”+s[i].m1

“\nMark2:”+s[i].m2 \nAverage:”+s[i].avg “\n”); } class str1 { public static void Main(string [ ]args) { int i; student p=new student(); Console.WriteLine (“Enter the number of Students:”); i=Convert.Toint32 (Console.ReadLine()); p.getData(i); p.showData(i); } }

3

OUTPUT:

Enter the number of Students: 2Enter the Name: ABCDEnter the mark 1: 60Enter the mark 2: 80Enter the Name: EFGHEnter the mark 1: 80Enter the mark 2: 90

Name : ABCDMark 1 : 60Mark 2 : 80Average : 70

Name : EFGHMark 1 : 80Mark 2 : 90Average : 85

4

RESULT: Thus the program was written & output was verified.

5

EXP NO.:2

CUSTOM NAMESPACES

AIM: To write a program in C# Language to create Custom Namespaces.

ALGORITHM:

1. Create a namespace that does the addition operation.2. Create another namespace that does the difference operation.3. Compile them into a dll using

csc/t:library/out: <dllfilename>.dll <namespacefile>.cs4. Write a C# that uses the add and difference function and compile it

as csc/reference: <dllfilename>.dll <mainprogramfilename>.cs

6

FLOWCHART: CUSTOM NAMESPACE

Start

Add(a,b)

Sub(a,b)

Create object of class trail1 and call the function

usins object

Stop

Add(a,b)

Compute a+b

Print the value

Return

7

sub(a,b)

Compute a-b

Print the value

Return

PROGRAM:

ns.csnamespace all{ using System; public class triall { public void add (int x, int y) { Console.WriteLine (“The Sum is: {0}”,(x+y)); } public void sub (int x, int y) { Console.WriteLine (“The Diff is: {0}”,(x-y)); } }}

csc /t:library /out:ns.dll ns.cs

main.csusing all;class me{ public static void Main( String[ ] s ) { triall x=new triall(); int a,b; Console.WriteLine(“Enter the 2 values: “); a = Convert.ToInt32(Console.ReadLine()); b = Convert.ToInt32(Console.ReadLine()); x.add (a,b); x.sub (a,b); }}

csc / reference : ns.dll main.cs

8

OUTPUT:

Enter the 2 values :100200

The Sum is : 300The Diff is : -100

9

RESULT: Thus the program was written & output was verified.

10

EXP NO.: 3

MULTILEVEL INHERITANCE

AIM: To write a program in C# Language to demonstrate Multilevel Inheritance.

ALGORITHM:

1. Create a base class b1.2. Create a base class b2 and inherit b1.3. Create a base class b3 and inherit b2.4. In the main class, create an object of b3 and call all the functions

defined.

11

FLOWCHART:MULTILEVEL INHERITANCE

Start

Div(a,b)

Addi(a,b)

Subt(a,b)

Mult(a,b)

Create object of class div.

Stop

Addi(a,b)

Int z;Z=a+b;

Print the value of z

Return

subt(a,b)

Int z;Z=a-b;

Print the value of z

Return

12

PROGRAM:using System;public class add{ public void addi(int a ,int b) { int z; z=a+b; Console.WriteLine("after doing addition"+z); }}public class sub:add{ public void subt(int a,int b) { int z; z=a-b; Console.WriteLine("after doing subtration"+z)}}public class mul:sub{ public void mult(int a,int b) { int z; z=a*b; Console.WriteLine("after doing Multiplication"+z) }}public class div:mul{ public void divi(int a,int b) { int z; z=a/b; Console.WriteLine("after doing divison"+z); }}

public class calcution{ public static void Main(String [ ]args) { int a,b; div x=new div(); Console.WriteLine ("Enter The A value"); a=Convert.ToInt32 (Console.ReadLine()); Console.WriteLine ("Enter The B value"); b=Convert.ToInt32 (Console.ReadLine()); x.addi (a,b); x.subt (a,b); x.mult (a,b); x.divi (a,b); }}

13

OUTPUT:

Enter the value of A: 5Enter the value of B: 5

After Addition : 10After Subtraction: 0After Multiplication : 25After Division : 1

14

RESULT: Thus the program was written & output was verified.

15

EXP NO.: 4

INTERFACES

AIM: To write a program in C# Language to demonstrate Interfaces

ALGORITHM:

1. Create 4 interfaces.2. Inherit them to a class & define the functions declared in those

interfaces.3. In the main program, create an object of derived class and call all the

functions defined.

16

FLOWCHART: INTERFACE

Start

Create 4 Interface and declare methods

corresponding to it..

Create a class and define all the methods inside it.

Create a main class declare object of derived

class

Call the defined function using the object created

for the derived class inside the main class.

Stop

17

PROGRAM:

using System; interface i1 { void add(); }

interface i2 { void sub(); }

interface i3 { void mul(); }

interface i4 { void div(); }

class xyz:i1,i2,i3,i4 { public int a,b; public void get() { Console.WriteLine("Enter The A value"); a=Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter The B value"); b=Convert.ToInt32(Console.ReadLine()); } public void add() { int z; z=a+b; Console.WriteLine("after doing addition"+z); }

public void sub() { int z; z=a-b; Console.WriteLine("after doing subtraction"+z); }

public void mul() {

18

int z; z=a*b; Console.WriteLine("after doing multiplication"+z); }

public void div() { int z; z=a/b; Console.WriteLine("after doing division"+z); } }

class multiple { public static void Main(String []args) { xyz obj=new xyz(); obj.get(); i1 obj1=(i1) obj; obj1.add();

i2 obj2=(i2) obj; obj2.sub();

i3 obj3=(i3) obj; obj3.mul();

i4 obj4=(i4) obj; obj4.div();

} }

19

OUTPUT:

Enter the value of A: 5Enter the value of B: 5

After Addition : 10After Subtraction: 0After Multiplication : 25After Division : 1

20

RESULT: Thus the program was written & output was verified.

21

EXP NO.: 5

DELEGATES

AIM: To write a program in C# Language to demonstrate the usage of Delegates.

ALGORITHM:

1. Declare a delegate.2. Create a new class and define 2-3 methods with same signature as of the

delegate.3. Write the main program & create an object of delegate.4. Pass the function names as arguments (parameter) to the delegate.

22

FLOWCHART: DELEGATES.

Start

Create object of class b1

Print1()

Create object of delegate and call

the functions using object of b1 and

delegate.

Stop

Print2()

Print the satatement.

Print1()

Print the satatement.

Print2()

23

PROGRAM:

using system; public delegate void dell(); public class b1 { public void print1() { Console.WriteLine (“This is Function 1\n”); } public void print2() { Console.WriteLine (“This is Function 2\n”); } } class main { public static void Main() { b1 a = new b1(); dell x = new dell(a.print1); x+ = new dell(a.print2); x(); Console.ReadLine(); } }

24

OUTPUT:

This is Function 1This is Function 2

25

RESULT: Thus the program was written & output was verified.

26

EXP NO.: 6 MOUSE EVENTS

AIM: To write a program in C# Language to demonstrate the Mouse Events.

ALGORITHM:

1. In the main class, inherit Form.2. in the constructor of the class, define the EventHandlers for the

MouseUp and MouseMove.3. Write different functions to respond to the MouseUp & MouseMove

events.

27

PROGRAM:

using System; using System.Windows.Forms;

class WinF:Form { public WinF() { Top=100; Left=75; Height=100; Width=500; this.MouseUp+=new MouseEventHandler(OnMo); this.MouseMove+=new MouseEventHandler(OnMM); } public void OnMo(object O,MouseEventArgs M) { string s=string.Format("CurrentPosition:{0},{1}",M.X,M.Y); MessageBox.Show(s); } public void OnMM(object O,MouseEventArgs M) { this.Text=string.Format("CurrentPosition:{0},{1}",M.X,M.Y); } public static void Main() { Application.Run(new WinF()); } }

28

OUTPUT:

29

RESULT: Thus the program was written & output was verified.

30

EXP NO.: 7

MENUS

AIM: To write a program in C# Language to demonstrate the creation of Menus.

ALGORITHM:

1. In the main class, inherit Form.2. In the constructor of the class, define the Menus3. Write the various events for the Menu.4. Include the Menu in the Form

31

PROGRAM:

using System; using System.Windows.Forms;

class WinFF:Form { public WinFF() { MainMenu M=new MainMenu(); MenuItem M1=new MenuItem("file"); MenuItem M2=new MenuItem("open"); MenuItem M3=new MenuItem("save"); MenuItem M4=new MenuItem("exit"); MenuItem M5=new MenuItem("Edit");

M1.MenuItems.Add(M2); M1.MenuItems.Add(M3); M1.MenuItems.Add(M4); M.MenuItems.Add(M1); this.Menu = M;

} public static void Main() { Application.Run(new WinFF()); } }

32

OUTPUT:

33

34

RESULT: Thus the program was written & output was verified.

EXP NO.: 8

TEXT BOX

AIM: To write a program in C# Language to demonstrate the usage of Text Boxes.

ALGORITHM:

1. In the main class, inherit Form2. In the constructor of the class, define the properties of the text boxes.3. Write the various events for the Buttons.4. Include the Menu in the Form

35

PROGRAM:

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

class teb:Form { TextBox T1=new TextBox(); TextBox T2=new TextBox(); TextBox T3=new TextBox(); Button B1=new Button(); Button B2=new Button(); Button B3=new Button();

public teb() { T1.Location=new System.Drawing.Point(10,10); T1.Size=new System.Drawing.Size(150,100); T1.Multiline=true; T1.ScrollBars=ScrollBars.Both; B1.Location=new System.Drawing.Point(180,10); B1.Text="Reveal";

T2.Location=new System.Drawing.Point(10,120); T2.Size=new System.Drawing.Size(150,20); T2.PasswordChar='*'; B2.Location=new System.Drawing.Point(180,120); B2.Text="Reveal";

T3.Location=new System.Drawing.Point(10,120); T3.Size=new System.Drawing.Size(50,10); T3.CharacterCasing=CharacterCasing.Upper; B3.Location=new System.Drawing.Point(110,34); B3.Text="Upper";

this.Click+=new EventHandler(Button_B1); this.Click+=new EventHandler(Button_B2); this.Click+=new EventHandler(Button_B3);

this.Controls.Add(T1); this.Controls.Add(T2); this.Controls.Add(T3);

36

this.Controls.Add(B1); this.Controls.Add(B2); this.Controls.Add(B3); }

public void Button_B1(object O, EventArgs M) { MessageBox.Show(T1.Text); }

public void Button_B2(object O, EventArgs M) { MessageBox.Show(T2.Text); }

public void Button_B3(object O, EventArgs M) { MessageBox.Show(T3.Text); }

public static void Main() { Application.Run(new teb()); } }

37

OUTPUT:

Sample Output

38

39

RESULT: Thus the program was written & output was verified.

EXP NO.: 9

LIST BOX

AIM: To write a program in C# Language to demonstrate the usage of List Boxes.

ALGORITHM:

1. In the main class, inherit Form2. In the constructor of the class, define the properties of the List boxes.3. Write the various events for the List Box.4. Include the Menu in the Form.

40

PROGRAM:

using System; using System.Windows.Forms; using System.Drawing; public class Win1:Form { public ListBox lb1; public void myMClick (object O, MouseEventArgs M) { lb1.Items.Add (“LAST Mouse Click @ X:” +M.X ” Y:” +M.Y); } public void myMMove (object O, MouseEventArgs M) { lb1.Items.Add (“LAST Mouse Move @ X:” +M.X ” Y:” +M.Y); } public Win1( ) { Top = 0; Left = 0; Height = 300; Width = 300; MouseUp+ = new MouseEventHandler(OnMClick); MouseMove+ = new MouseEventHandler(OnMMove); lb1 = new ListBox(); lb1.Location = new System.Drawing.Point(2,2); lb1.Size = new System.Drawing.Size(250,250); lb1.Text = "lb1"; Controls.Add(lb1); } } public class main { public static void Main( ) { Application.Run (new Win1( )); } }

41

OUTPUT:

42

RESULT: Thus the program was written & output was verified.

43

EXP NO.:10

SELECT BOXES & RADIO BUTTONS

AIM: To write a program in C# Language to demonstrate the usage of Select Boxes.

ALGORITHM:

1. In the main class, inherit Form2. In the constructor of the class, define the properties of the Select boxes

and Radio buttons.3. Write the various events for them.4. Include the Menu in the Form.

44

Program: -

using System;using System.Windows.Forms;

class Win: Form{

RadioButton r1 = new RadioButton();RadioButton r2 = new RadioButton();CheckBox cb1 = new CheckBox();CheckBox cb2 = new CheckBox();GroupBox gb = new GroupBox();

public Win(){

gb.Controls.Add(r2);

this.Controls.Add(cb1); cb1.Location=new System.Drawing.Point(300,200);this.Controls.Add(cb2); cb2.Location=new System.Drawing.Point(200,300);gb.Controls.Add(r1);gb.Controls.Add(r2);this.Controls.Add(gb);gb.Enter+=new EventHandler(onEnter);

}

public void onEnter(Object o, EventArgs e){

MessageBox.Show("HELLO WELCOME");}

public static void Main(){

Application.Run(new Win());}

}

45

OUTPUT

46

47

Result: -The program has been created and run successful

48