You are on page 1of 23

0

SNo. Practical Date Teacher Sign.

Calculator using Windows Form.


01

Convert Decimal number into Binary on


02
Windows Form

Login form with MS Access database using


03
Windows Form

Edit/Delete data from Database using


04
Windows Form

Read Data from database using Windows


05
Form

Text Writer and Text Reader Using Windows


06
Form

07 SMTP mail client (sending an email using SMTP)

Program to Calculate Size of any Website using


08
Windows Form

Download files from web using Windows


09
Form

Make Directories using C# on Windows


10
Form
1

Practical 1: Calculator using Windows Form.


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​ WindowsFormsApplication1
{
​public​ ​partial​ ​class​ ​Calc​ : Form
{
​double​ FirstNumber;
​string​ operation;
​public​ Calc()
{
InitializeComponent();

​private​ ​void​ Form1_Load(​object​ sender, EventArgs e)


{
label1.BackColor = Color.Transparent;
}

​private​ ​void​ label1_Click(​object​ sender, EventArgs e)


{

​private​ ​void​ textBox1_TextChanged(​object​ sender, EventArgs e)


{

​private​ ​void​ button7_Click(​object​ sender, EventArgs e)


{
​if​ (textBox2.Text == ​"0"​ && textBox2.Text != ​null​)
{
textBox2.Text = ​"9"​;
}
​else
{
textBox2.Text += ​"9"​;
}
}

​private​ ​void​ button16_Click(​object​ sender, EventArgs e)


{
textBox1.Text += textBox2.Text + ​"%"​;
FirstNumber = Convert.ToDouble(textBox2.Text);
textBox2.Text = ​"0"​;
operation = ​"%"​;
}

​private​ ​void​ button5_Click(​object​ sender, EventArgs e)


{
​if​(textBox2.Text == ​"0"​ && textBox2.Text != ​null​)
{
2

textBox2.Text = ​"7"​;
}
​else
{
textBox2.Text += ​"7"​;
}
}

​private​ ​void​ button6_Click(​object​ sender, EventArgs e)


{
​if​ (textBox2.Text == ​"0"​ && textBox2.Text != ​null​)
{
textBox2.Text = ​"8"​;
}
​else
{
textBox2.Text += ​"8"​;
}
}

​private​ ​void​ button9_Click(​object​ sender, EventArgs e)


{
​if​ (textBox2.Text == ​"0"​ && textBox2.Text != ​null​)
{
textBox2.Text = ​"4"​;
}
​else
{
textBox2.Text += ​"4"​;
}
}

​private​ ​void​ button10_Click(​object​ sender, EventArgs e)


{
​if​ (textBox2.Text == ​"0"​ && textBox2.Text != ​null​)
{
textBox2.Text = ​"5"​;
}
​else
{
textBox2.Text += ​"5"​;
}
}

​private​ ​void​ button11_Click(​object​ sender, EventArgs e)


{
​if​ (textBox2.Text == ​"0"​ && textBox2.Text != ​null​)
{
textBox2.Text = ​"6"​;
}
​else
{
textBox2.Text += ​"6"​;
}
}

​private​ ​void​ button13_Click(​object​ sender, EventArgs e)


{
​if​ (textBox2.Text == ​"0"​ && textBox2.Text != ​null​)
{
textBox2.Text = ​"1"​;
}
​else
{
3

textBox2.Text += ​"1"​;
}
}

​private​ ​void​ button14_Click(​object​ sender, EventArgs e)


{
​if​ (textBox2.Text == ​"0"​ && textBox2.Text != ​null​)
{
textBox2.Text = ​"2"​;
}
​else
{
textBox2.Text += ​"2"​;
}
}

​private​ ​void​ button15_Click(​object​ sender, EventArgs e)


{
​if​ (textBox2.Text == ​"0"​ && textBox2.Text != ​null​)
{
textBox2.Text = ​"3"​;
}
​else
{
textBox2.Text += ​"3"​;
}
}

​private​ ​void​ button17_Click(​object​ sender, EventArgs e)


{
​if​ (textBox2.Text == ​"0"​ && textBox2.Text != ​null​)
{
textBox2.Text = ​"0"​;
}
​else
{
textBox2.Text += ​"0"​;
}
}

​private​ ​void​ button18_Click(​object​ sender, EventArgs e)


{
​if​ (textBox2.Text == ​"0"​ && textBox2.Text != ​null​)
{
textBox2.Text = ​"00"​;
}
​else
{
textBox2.Text += ​"00"​;
}
}

​private​ ​void​ textBox2_TextChanged(​object​ sender, EventArgs e)


{

​private​ ​void​ button4_Click(​object​ sender, EventArgs e)


{
​this​.Close();
}

​private​ ​void​ button12_Click(​object​ sender, EventArgs e)


{
4

textBox1.Text += textBox2.Text + ​"+"​;


FirstNumber = Convert.ToDouble(textBox2.Text);
textBox2.Text = ​"0"​;
operation = ​"+"​;
}

​private​ ​void​ button20_Click(​object​ sender, EventArgs e)


{
​double​ SecondNumber;
​double​ Result;

textBox1.Text = ​null​;
SecondNumber = Convert.ToDouble(textBox2.Text);

​if​ (operation == ​"+"​)


{
Result = (FirstNumber + SecondNumber);
textBox2.Text = Convert.ToString(Result);
}

​else​ i
​ f​ (operation == ​"-"​)
{
Result = (FirstNumber - SecondNumber);
textBox2.Text = Convert.ToString(Result);
}

​else​ i
​ f​ (operation == ​"*"​)
{
Result = (FirstNumber * SecondNumber);
textBox2.Text = Convert.ToString(Result);
}

​else​ i ​ f​ (operation == ​"%"​)


{
​if​ (SecondNumber == 0)
{
Console.Write(​"Oops it's not possible"​);
}
​else
{
Result = (FirstNumber % SecondNumber);
textBox2.Text = Convert.ToString(Result);
}
}

​else​ i ​ f​ (operation == ​"/"​)


{
​if​ (SecondNumber == 0)
{
Console.Write(​"Oops it's not possible"​);
}
​else
{
Result = (FirstNumber / SecondNumber);
textBox2.Text = Convert.ToString(Result);
}
}

​private​ ​void​ button8_Click(​object​ sender, EventArgs e)


{
textBox1.Text += textBox2.Text + ​"-"​;
FirstNumber = Convert.ToDouble(textBox2.Text);
5

textBox2.Text = ​"0"​;
operation = ​"-"​;
}

​private​ ​void​ button3_Click(​object​ sender, EventArgs e)


{
textBox1.Text += textBox2.Text + ​"*"​;
FirstNumber = Convert.ToDouble(textBox2.Text);
textBox2.Text = ​"0"​;
operation = ​"*"​;
}

​private​ ​void​ button2_Click(​object​ sender, EventArgs e)


{
textBox1.Text += textBox2.Text + ​"/"​;
FirstNumber = Convert.ToDouble(textBox2.Text);
textBox2.Text = ​"0"​;
operation = ​"/"​;
}

​private​ ​void​ button19_Click(​object​ sender, EventArgs e)


{
​if​ (textBox2.Text == ​"0"​ && textBox2.Text != ​null​)
{
textBox2.Text = ​"."​;
}

​else
{
textBox2.Text += ​"."​;
}
}

​private​ ​void​ button1_Click(​object​ sender, EventArgs e)


{
textBox1.Text = ​null​;
textBox2.Text = ​"0"​;
}

​private​ ​void​ pictureBox1_Click(​object​ sender, EventArgs e)


{

​private​ ​void​ label1_Click_1(​object​ sender, EventArgs e)


{

​private​ ​void​ label1_Click_2(​object​ sender, EventArgs e)


{

}
}
}
6

Output:
7

Practical 2: Convert Decimal number into Binary on Windows Form


using​ System;
using​ System.Collections.Generic;
using​ System.ComponentModel;
using​ System.Data;
using​ System.Drawing;
using​ System.Linq;
using​ System.Text;
using​ System.Threading.Tasks;
using​ System.Windows.Forms;

namespace​ WindowsFormsApp5
{
​public​ ​partial​ ​class​ ​Form4​ : Form
{
​public​ Form4()
{
InitializeComponent();
}

​private​ ​void​ button1_Click(​object​ sender, EventArgs e)


{
​int​ x;
​string​ y = ​null​;
x = ​int​.Parse(textBox1.Text);
​while​ (x > 0)
{
y = (x%2).ToString() + y;

x /= 2;
}
textBox2.Text = y;
}
}
}

Output:
8

Practical 3: Login form with MS Access database using Windows Form


using​ System;
using​ System.Collections.Generic;
using​ System.ComponentModel;
using​ System.Data;
using​ System.Drawing;
using​ System.Linq;
using​ System.Text;
using​ System.Threading.Tasks;
using​ System.Windows.Forms;
using​ System.Data.OleDb;

namespace​ WindowsFormsApp5
{
​public​ ​partial​ ​class​ ​Form5​ : Form
{
​public​ OleDbConnection connection1 = ​new​ OleDbConnection();
​public​ Form5()
{
InitializeComponent();
connection1.ConnectionString= ​@"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=D:\\Pass.accdb;Persist Security Info=False"​;
}
​private​ ​void​ button1_Click(​object​ sender, EventArgs e)
{
connection1.Open();
OleDbCommand com1 = ​new​ OleDbCommand();
com1.Connection = connection1;
com1.CommandText = ​"select * from id where username='"​ + textBox1.Text + ​"'and password='"
+ textBox2.Text + ​"'"​;
OleDbDataReader reader1 = com1.ExecuteReader();
​bool​ access = ​false​;
​while​ (reader1.Read())
{
access = ​true​;
}
​if​ (access)
{
MessageBox.Show(​"Login Successful"​);
}
​else
{
MessageBox.Show(​"Incorrect Username or Password"​);
}
textBox1.Clear();
textBox2.Clear();
connection1.Close();
}
}
}
9

Output:
10

Practical 4: Edit/Delete data from Database using Windows Form


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.Data.OleDb;
namespace​ WindowsFormsApplication1
{
​public​ ​partial​ ​class​ ​Form1​ : Form
{
​private​ OleDbConnection con = ​new​ OleDbConnection();
​public​ Form1()
{
InitializeComponent();
con.ConnectionString = ​@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/DB/Cs.accdb;
Persist Security Info=False;"​;
}

​private​ ​void​ Form1_Load(​object​ sender, EventArgs e)


{

​private​ ​void​ button1_Click(​object​ sender, EventArgs e)


{

con.Open();
OleDbCommand com1 = ​new​ OleDbCommand();
com1.Connection = con; com1.CommandText = ​"insert into csinfo(sname,rollno) values('"​ +
textBox1.Text + ​"','"​ + textBox2.Text + ​"')"​;
com1.ExecuteNonQuery();
MessageBox.Show(​"Submitted"​);
textBox1.Text = ​null​;
textBox2.Text = ​null​;
con.Close();
}

​private​ ​void​ button2_Click(​object​ sender, EventArgs e)


{
con.Open();
OleDbCommand com = ​new​ OleDbCommand();
com.Connection = con;
​string​ query = ​"DELETE FROM csinfo WHERE rollno = '"​ + textBox2.Text + ​"'"​;
com.CommandText = query;
com.ExecuteNonQuery();
MessageBox.Show(​"Data Delete Successfully"​);
textBox1.Text = ​null​;
textBox2.Text = ​null​;
con.Close();

}
}
11

Output:
12
13

Practical 5: Read Data from database using Windows Form


using​ System;
using​ System.Collections.Generic;
using​ System.ComponentModel;
using​ System.Data;
using​ System.Drawing;
using​ System.Linq;
using​ System.Text;
using​ System.Threading.Tasks;
using​ System.Windows.Forms;
using​ System.Data.OleDb;
namespace​ WindowsFormsApp7
{
​public​ ​partial​ ​class​ ​Form1​ : Form
{
OleDbConnection con = ​new​ OleDbConnection();
​public​ Form1()
{
InitializeComponent();
con.ConnectionString = ​@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/DB/Cs.accdb;
Persist Security Info=False;"​;
}

​private​ ​void​ button1_Click(​object​ sender, EventArgs e)


{
con.Open();
OleDbCommand com = ​new​ OleDbCommand();
com.Connection = con;
​string​ query = ​"SELECT * FROM csinfo"​;
com.CommandText = query;
OleDbDataReader dataReader = com.ExecuteReader();
​while​ (dataReader.Read())
{
String str1 = dataReader[​"sname"​].ToString();
String str2 = dataReader[​"rollno"​].ToString();
textBox1.Text = textBox1.Text + str1 + ​","​;
textBox2.Text = textBox2.Text + str2 + ​"\n"​;
}
con.Close();
}
}
}
14

Output:

Practical 6: Text Writer and Text Reader Using Windows Form


15

using​ System;
using​ System.Collections.Generic;
using​ System.ComponentModel;
using​ System.Data;
using​ System.Drawing;
using​ System.Linq;
using​ System.Text;
using​ System.Threading.Tasks;
using​ System.Windows.Forms;

namespace​ WindowsFormsApp5
{
​public​ ​partial​ ​class​ ​Form2​ : Form
{
​public​ Form2()
{
InitializeComponent();
}

​private​ ​void​ Form2_Load(​object​ sender, EventArgs e)


{

​private​ ​void​ button1_Click(​object​ sender, EventArgs e)


{
​string​ file_name = ​"D:\\"​ + textBox1.Text + ​".txt"​;

​if​ (System.IO.File.Exists(file_name) == ​true​)


{
System.IO.StreamReader objReader;
objReader = ​new​ System.IO.StreamReader(file_name);

textBox2.Text = objReader.ReadToEnd();
objReader.Close();
}

​else
{
MessageBox.Show(​"No such file"​ + file_name);
}
}

​private​ ​void​ textBox1_TextChanged(​object​ sender, EventArgs e)


{

​private​ ​void​ button1_Click_1(​object​ sender, EventArgs e)


{
​string​ file_name = ​"D:\\"​ + textBox1.Text + ​".txt"​;

System.IO.StreamWriter objWriter;
objWriter = ​new​ System.IO.StreamWriter(file_name);

objWriter.Write(textBox2.Text + textBox3.Text);
objWriter.Close();
textBox3.Clear();
MessageBox.Show(​"Wrote File"​);
}

​private​ ​void​ button2_Click(​object​ sender, EventArgs e)


{
​string​ file_name = ​"D:\\"​ + textBox1.Text + ​".txt"​;
16

System.IO.StreamWriter objWriter1;
objWriter1 = ​new​ System.IO.StreamWriter(file_name);

objWriter1.Write(​""​);
objWriter1.Close();
}
}
}

Output:
17

Practical 7: SMTP mail client (sending an email using SMTP)


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.Net.Mail;

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

​private​ ​void​ Form1_Load(​object​ sender, ​EventArgs​ e)


{

​private​ ​void​ button1_Click(​object​ sender, ​EventArgs​ e)


{
​try
{
​MailMessage​ mail = ​new​ ​MailMessage​();
​SmtpClient​ SmtpServer = ​new​ ​SmtpClient​(​"smtp.gmail.com"​);

mail.From = ​new​ ​MailAddress​(textBox1.Text);


mail.To.Add(textBox2.Text);
mail.Subject = ​"Test Mail"​;
mail.Body = textBox5.Text;
SmtpServer.Port = 25;
SmtpServer.Credentials = ​new​ System.Net.​NetworkCredential​(textBox3.Text,
textBox4.Text);
SmtpServer.EnableSsl = ​true​;
SmtpServer.Send(mail);
​MessageBox​.Show(​"mail Send"​);
}

​catch​ (​Exception​ ex)


{
​MessageBox​.Show(ex.ToString());
}
}

​private​ ​void​ textBox1_TextChanged(​object​ sender, ​EventArgs​ e)


{

​private​ ​void​ textBox2_TextChanged(​object​ sender, ​EventArgs​ e)


{

​private​ ​void​ textBox3_TextChanged(​object​ sender, ​EventArgs​ e)


{
18

​private​ ​void​ textBox5_TextChanged(​object​ sender, ​EventArgs​ e)


{

​private​ ​void​ textBox4_TextChanged(​object​ sender, ​EventArgs​ e)


{

}
}
}

Output:
19

Practical 8: Program to Calculate Size of any Website using Windows Form


using​ System;
using​ System.Collections.Generic;
using​ System.ComponentModel;
using​ System.Data;
using​ System.Drawing;
using​ System.Linq;
using​ System.Text;
using​ System.Threading.Tasks;
using​ System.Windows.Forms;
using​ System.Net;

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

​private​ ​void​ button1_Click(​object​ sender, EventArgs e)


{
WebClient client = ​new​ WebClient();
client.Headers[​"User-Agent"​] = ​"Mozilla/4.0 (Compatible; Windows NT 5.1;MSIE
6.0)"​+​"(Compatible; MSIE 6.0; Windows NT 5.1; "​ + ​".NET CLR1.1.4322; .NET CLR 2.0.50727)"​;
​byte​[] arr = client.DownloadData(textBox1.Text);
MessageBox.Show(arr.Length.ToString());

}
}
}

Output:
20

Practical 9: Download files from web using Windows Form


using​ System;
using​ System.Collections.Generic;
using​ System.ComponentModel;
using​ System.Data;
using​ System.Drawing;
using​ System.Linq;
using​ System.Text;
using​ System.Threading.Tasks;
using​ System.Windows.Forms;
using​ System.Net;

namespace​ WindowsFormsApp6
{
​public​ ​partial​ ​class​ ​Form2​ : Form
{
​public​ Form2()
{
InitializeComponent();
}

​private​ ​void​ button1_Click(​object​ sender, EventArgs e)


{
WebClient wc = ​new​ WebClient();

wc.DownloadFile(textBox1.Text, ​@"D:\\Download.pdf"​);

MessageBox.Show(​"DONE"​);

}
}
}

Output:
21

Practical 10: Make Directories using C# on Windows Form


using​ System;
using​ System.Collections.Generic;
using​ System.ComponentModel;
using​ System.Data;
using​ System.Drawing;
using​ System.Linq;
using​ System.Text;
using​ System.Threading.Tasks;
using​ System.Windows.Forms;
using​ System.IO;

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

​private​ ​void​ button1_Click(​object​ sender, EventArgs e)


{
​string​ root = ​@"C:\"​ + textBox1.Text;

​if​ (!Directory.Exists(root))
{
Directory.CreateDirectory(root);
MessageBox.Show(​"Directory Successfully Created"​);
}

​else
{
MessageBox.Show(​"Directory Already Exists"​);
}

​private​ ​void​ textBox1_TextChanged(​object​ sender, EventArgs e)


{

}
}
}
22

Output:

You might also like