Bài Tập Code Lập Trình Trực Quan: Console a. Định nghĩa mảng

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 61

BÀI TẬP CODE LẬP TRÌNH TRỰC QUAN

I. Console
a. Định nghĩa mảng
/Mảng có dộ dài cố định phải chứa các đối tượng cùng loại.
//Một mảng ArrayList có thể lưu trữ các đối tượng thuộc các loại khác nhau.

namespace Baitap3_Container___Muctieu2
{
class Program
{
static void Main(string[] args)
{
ArrayList al = new ArrayList();
al.Add("Hello");
al.Add("15,16,17");
al.Add(new DateTime(2010, 5, 30));
Console.WriteLine (al[0]);
Console.WriteLine();
Console.WriteLine(al[1]);
Console.WriteLine();
Console.WriteLine(al[2]);
Console.WriteLine();
Console.ReadLine();
b. Container
namespace Baitap3_Container___Muctieu3
{ class Program
{
static void Main(string[] args)
{
Queue/*<string>*/ q = new Queue/*<string>*/();
q.Enqueue("ABC");
q.Enqueue("123");
q.Enqueue(new Decimal(15.30));
q.Enqueue(new DateTime(2002,5,10));
Console.WriteLine(q.Dequeue());
Console.WriteLine(q.Dequeue());
Console.WriteLine(q.Dequeue());
Console.WriteLine(q.Dequeue());
Console.ReadLine();
/*foreach ( String a in q)
Console.WriteLine(q.Dequeue());
Console.ReadLine();*/
c. Định nghĩa Lớp
namespace Baitap3_DinhNghiaLop
{
class Program
{
static void Main(string[] args)
{
A myA = new A();
A.Static_Member();
myA.Instance_Member();
Console.WriteLine();
B myB= new B();
myB.Instance_Member();
B.Static_Member();
Console.WriteLine();
Console.WriteLine("Bien X = " + myB.X);
Console.Write(myB.Y);
Console.WriteLine("Bien Y = " );

Lớp I91C – Minh - LTN 1


Console.WriteLine("Bien Z = " + myB.Z);
Console.ReadLine();
}
class A
{
static public void Static_Member()//Khai bao static thi tu ham main co the goi truc tiep phuong thuc
{
Console.WriteLine("Calling from Static Member");
}
public void Instance_Member()//Khai bao public thi ham Main fai new lop A moi goi dc phuong thuc
{
Console.WriteLine("Calling from Instance Member");
}
}
class B : A //Lop B ke thua tu A nen co the goi cac phuong thuc trong class A
{
private int x;
private int y;
private int z;
public int X
{
get { return x; }
}
public int Y
{
set { y=value;}
}
public int Z
{
get { return z; }
set {z=value;}
d. Tính giá trị Max của biến Byte
namespace TinhToan
{
class Tinhtoan
{
static void Main()
{
byte b;
b = byte.MaxValue;
Console.WriteLine("Maximum Byte is " + b.ToString ());
Console.ReadLine();
}
e. Xuất ra 1 chuỗi
namespace XuatChuoi
{
class Program
{
static void Main(string[] args)
{
int[] i = new int[5] { 40, 30, 60, 80, 100 };
int j;
for (j = 0; j < 5; j++)
{
Console.WriteLine("Index = " + j.ToString() + " & Value = " + i[j].ToString());
Console.ReadLine();
}
string[] str = new string[3] { "I", "LOVE", "U" };
int iStr;
for (iStr = 0; iStr < 3; iStr++)
{
Console.WriteLine("Index = " + iStr.ToString() + " & Value = " + str[iStr]);
Lớp I91C – Minh - LTN 2
Console.ReadLine();
}
DateTime[] date = new DateTime[2] { new DateTime(2010, 3, 20,20,30,55), new DateTime(2009, 12, 30,9,40,59) };
int iDate;
foreach (DateTime s in date)
Console.WriteLine("Index = " + s.ToString() + " & Value = " + s.ToString());
Console.ReadLine();

I. Graphics – Buoi 1
a. Tính tổng N

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

private void button_tinh_Click(object sender, EventArgs e)


{
textBox_ketqua.Text = tinhtong().ToString();
}
private void textBox_ketqua_TextChanged(object sender, EventArgs e)
{

}
private int tinhtong()
{

int a= int.Parse(textBox_nhapn.Text);
int tong = 0;
for (int i = 0; i <= a; i++)
{
tong += i;
}
return tong;
}

private void button_thoat_Click(object sender, EventArgs e)


{
this.Close();
}
b. 4 phép tính cơ bản

Lớp I91C – Minh - LTN 3


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

private void button2_Click(object sender, EventArgs e)


{
double x = Convert.ToDouble(txtsothunhat.Text);
double y = Convert.ToDouble(txtsothuhai.Text);
lblKQ.Text = Convert.ToString(x + y);
}

private void button1_Click(object sender, EventArgs e)


{
double x = Convert.ToDouble(txtsothunhat.Text);
double y = Convert.ToDouble(txtsothuhai.Text);
lblKQ.Text = Convert.ToString(x - y);

private void button4_Click(object sender, EventArgs e)


{
double x = Convert.ToDouble(txtsothunhat.Text);
double y = Convert.ToDouble(txtsothuhai.Text);
lblKQ.Text = Convert.ToString(x * y);

private void button3_Click(object sender, EventArgs e)


{
double x = Convert.ToDouble(txtsothunhat.Text);
double y = Convert.ToDouble(txtsothuhai.Text);
if (y != 0)
lblKQ.Text = Convert.ToString(x / y);
else
lblKQ.Text = "Loi";
}

private void button5_Click(object sender, EventArgs e)


{
Application.Exit();
}

private void button6_Click(object sender, EventArgs e)


{

Lớp I91C – Minh - LTN 4


txtsothunhat.Clear();
txtsothuhai.Clear();
}
}
}
c. Đổi Dương lịch sang Âm lịch

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

private void Form1_Load(object sender, EventArgs e)


{
for (int i = 1; i <= 5000; i++)
{
comboBox1.Items.Add(i);
}
comboBox1.SelectedIndex=DateTime.Now.Year-1;
}
private string DocNamAmLich(Int64 Nam)
{
string[] can={"Giáp", "Ất", "Bính", "Đinh", "Mậu", "Kỷ", "Canh", "Tân", "Nhâm", "Quý"};
string[] chi={"Tý", "Sửu", "Dần", "Mẹo", "Thìn", "Tỵ", "Ngọ", "Mùi", "Thân", "Dậu", "Tuất", "Hợi"};
Int64 SoCan, SoChi, n, m;
n = (Nam - 2008) % 10;
m = (Nam - 2008) % 12;
SoCan = (10+4+n)%10; //Nam 2008 co can la 4 (Mau), cong 10 de sau khi chia lay du duoc so duong
SoChi = (12 + 0 + m) % 12;//Nam 2008 co chi la 0 (Ty), cong 12 de sau khi chia lay du duoc so duong
return can[SoCan] + " " + chi[SoChi];
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)


{
Int64 so;
try
{
so = Convert.ToInt64(comboBox1.SelectedItem);
// label1.Text = DocNamAmLich(so);
}
catch
{
MessageBox.Show("Lỗi nhập số", "Báo lỗi:", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}
Lớp I91C – Minh - LTN 5
private void button1_Click_1(object sender, EventArgs e)
{
Int64 so;
try
{
so = Convert.ToInt64(comboBox1.Text);
label1.Text = DocNamAmLich(so);
}
catch
{
MessageBox.Show("Lỗi nhập số", "Báo lỗi:", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}
}
d. Các phép toán số học & logic

namespace Bai1d
{
public partial class Form1 : Form
{
private void button_thoat_Click(object sender, EventArgs e)
{
this.Close();
}
private void button_and_Click(object sender, EventArgs e)
{

}
private int dichchuyentrai()
{
int a = int.Parse(textBox_so1.Text);
int b = int.Parse(textBox_so2.Text);
int c = a << b;
return c;
}
private int dichchuyenphai()
{
int a = int.Parse(textBox_so1.Text);

Lớp I91C – Minh - LTN 6


int b = int.Parse(textBox_so2.Text);
int c = a >> b;
return c;
}
private void button_dichphai_Click(object sender, EventArgs e)
{
textBox_kq.Text = dichchuyentrai().ToString();
}

private void button_dichtrai_Click(object sender, EventArgs e)


{
textBox_kq.Text = dichchuyenphai().ToString();
}

e. Đọc số thành chữ

namespace DoiSoThanhChu

public partial class Form1 : Form

public Form1()

InitializeComponent();

string DocMotSo(ulong so)

string kq = "";

switch (so)

case 0: kq = "không";

break;

case 1: kq = "một";

break;

Lớp I91C – Minh - LTN 7


case 2: kq = "hai";

break;

case 3: kq = "ba";

break;

case 4: kq = "bốn";

break;

case 5: kq = "năm";

break;

case 6: kq = "sáu";

break;

case 7: kq = "bảy";

break;

case 8: kq = "tám";

break;

case 9: kq = "chín";

break;

return kq;

string DocHaiSo(ulong so)

byte HDV=Convert.ToByte(so%10);

byte HC=Convert.ToByte(so/10);

string kq = "";

if (so >= 0 && so <= 9)

kq = DocMotSo(so);

else if (so == 10)

kq = "mười";

else if (so == 15)

kq = "mười lăm";

else if (so >= 11 && so <= 19)

Lớp I91C – Minh - LTN 8


kq = "mười " + DocMotSo(HDV);

else if (so == 20 || so == 30 || so == 40 || so == 50 || so == 60 || so == 70 || so == 80 || so == 90)

kq = DocMotSo(HC) + " mươi";

else if (so == 25 || so == 35 || so == 45 || so == 55 || so == 65 || so == 75 || so == 85 || so == 95)

kq = DocMotSo(HC) + " mươi lăm ";

else

kq = DocMotSo(HC) + " mươi " + DocMotSo(HDV);

return kq;

string DocBaSo(ulong so)

string kq = "";

byte HaiSoSau = Convert.ToByte(so % 100);

byte HC = Convert.ToByte(HaiSoSau / 10);

byte HDV = Convert.ToByte(HaiSoSau % 10);

byte HT = Convert.ToByte(so / 100);

if (so >= 0 && so <= 99)

kq = DocHaiSo(so);

else if (so == 100 || so == 200 || so == 300 || so == 400 || so == 500 || so == 600 || so == 700 || so == 800 || so == 900)

kq = DocMotSo(HT) + " trăm";

else if (HC == 0)

kq = DocMotSo(HT) + " trăm lẻ " + DocMotSo(HDV);

else kq = DocMotSo(HT) + " trăm " + DocHaiSo(HaiSoSau);

return kq;

string DocSauSo(ulong so)

string kq = "";

UInt64 Basodau = Convert.ToUInt64(so / 1000);

UInt64 Basocuoi = Convert.ToUInt64(so % 1000);

Lớp I91C – Minh - LTN 9


byte Haisocuoi = Convert.ToByte(Basocuoi % 100);

byte HC = Convert.ToByte(Haisocuoi / 10);

byte HT = Convert.ToByte(Basocuoi / 100);

if (so >= 0 && so <= 999)

kq = DocBaSo(so);

else if (Basocuoi == 0)

kq = DocBaSo(Basodau) + " ngàn";

else if ((HT == 0) && (HC == 0))

kq = DocBaSo(Basodau) + " ngàn không trăm lẻ " + DocBaSo(Basocuoi);

else if (HT == 0)

kq = DocBaSo(Basodau) + " ngàn không trăm " + DocBaSo(Basocuoi);

else

kq = DocBaSo(Basodau) + " ngàn " + DocBaSo(Basocuoi);

return kq;

private void button1_Click(object sender, EventArgs e)

ulong n = Convert.ToUInt64(textBox1.Text);

lblkq.Text = DocSauSo(n);

private void button2_Click(object sender, EventArgs e)

Application.Exit();

II. Graphics – Buoi 3


a. Toạ độ trên form

Lớp I91C – Minh - LTN 10


private void Form1_MouseMove(object sender, MouseEventArgs e)
{
x.Text = e.X.ToString();
y.Text = e.Y.ToString();

b. Điều khiển Control bằng tay

namespace DieukhienControlbangtay
{
public partial class Form1 : Form
{

int x,x_old;
int y,y_old;
public Form1()
{
InitializeComponent();
}

private void button_up_Click(object sender, EventArgs e)


{
moveup();
}

#region Cac phuong thuc


private void moveup()
{
Lớp I91C – Minh - LTN 11
x = pictureBox1.Location.X;
y = pictureBox1.Location.Y;
pictureBox1.Location = new System.Drawing.Point(x, y - 10);
}
private void movedown()
{
x = pictureBox1.Location.X;
y = pictureBox1.Location.Y;
pictureBox1.Location = new System.Drawing.Point(x, y + 10);
}
private void moveleft()
{
x = pictureBox1.Location.X;
y = pictureBox1.Location.Y;
pictureBox1.Location = new System.Drawing.Point(x-10 ,y);
}
private void moveright()
{
x = pictureBox1.Location.X;
y = pictureBox1.Location.Y;
pictureBox1.Location = new System.Drawing.Point(x + 10, y);
}
private void reset()
{
x = pictureBox1.Location.X;
y = pictureBox1.Location.Y;
pictureBox1.Location = new System.Drawing.Point(x_old,y_old);
}
#endregion

private void button_down_Click(object sender, EventArgs e)


{
movedown();
}

private void button_left_Click(object sender, EventArgs e)


{
moveleft();
}

private void button_right_Click(object sender, EventArgs e)


{
moveright();
}

private void button_res_Click(object sender, EventArgs e)


{
reset();
}

private void checkBox_chay1chieu_CheckedChanged(object sender, EventArgs e)


{
if (checkBox_chay1chieu.Checked)
timer_tg.Enabled = true;
else
timer_tg.Enabled = false;

private void Form1_Load(object sender, EventArgs e)


{
x_old = pictureBox1.Location.X;
Lớp I91C – Minh - LTN 12
y_old = pictureBox1.Location.Y;
}

private void timer_tg_Tick(object sender, EventArgs e)


{
x = pictureBox1.Location.X;
y = pictureBox1.Location.Y;
pictureBox1.Location = new System.Drawing.Point(x + 10, y);
if (x >= this.Width)
x = 0;
pictureBox1.Location = new System.Drawing.Point(x + 10, y);
}

c. Tự động chạy : (Chay 2chieu)


namespace BTChuong3
{
public partial class DienkhienControl_Chay2chieu : Form
{
bool thuan;
public DienkhienControl_Chay2chieu()
{
InitializeComponent();
}
private void timer_h_Tick(object sender, EventArgs e)
{
Point p = pictureBox1.Location;
if (p.X >= (this.Width - pictureBox1.Width))
thuan = false;
else if (p.X < 0)
thuan = true;
if (thuan)
pictureBox1.Location = new Point(p.X + 5, p.Y);
else
pictureBox1.Location = new Point(p.X - 5, p.Y);
}

private void DienkhienControl_Chay2chieu_Load(object sender, EventArgs e)


{
thuan = true;
}
}
}

d. Tự động chạy : (Chay 4chieu)

namespace WindowsApplication1
{
public partial class Form1 : Form
{
Boolean chayquaphai = true;
Boolean chayxuong = true;

public Form1()
{
InitializeComponent();
Lớp I91C – Minh - LTN 13
}
private void timer1_Tick(object sender, EventArgs e)
{
if ((chayquaphai == true) && (chayxuong == true))
{
if ((label1.Location.X + 5 <= this.Width - 60) && (label1.Location.Y + 5 <= this.Height - 70))
{ label1.Location = new Point(label1.Location.X + 5, label1.Location.Y + 3); }
else
chayquaphai = false;
}
else if ((chayquaphai == true) && (chayxuong == false))
{
if ((label1.Location.X + 5 <= this.Width - 70) && (label1.Location.Y - 5 >= -30))
{
label1.Location = new Point(label1.Location.X + 5, label1.Location.Y - 5);
}
else
chayxuong = true;
}
else if ((chayquaphai == false) && (chayxuong == true))
{
if ((label1.Location.X - 5 >= -10) && (label1.Location.Y + 5 <= this.Height - 70))
{
label1.Location = new Point(label1.Location.X - 5, label1.Location.Y + 5);
}
else
chayxuong = false;
}
else if ((chayquaphai == false) && (chayxuong == false))
{
if ((label1.Location.X - 5 >= -20) && (label1.Location.Y - 5 >= -0))
label1.Location = new Point(label1.Location.X-5, label1.Location.Y -4);
else

chayquaphai = true;
}
}
}

}
e. Hộp chữ chạy

namespace Buoi3
{
public partial class Form1 : Form
{
Lớp I91C – Minh - LTN 14
string str;
private void button_reset_Click(object sender, EventArgs e)
{
textbox_nhap.Text = null;
textbox_nhap.Focus();
}
private void hScrollBar_dichuyen_Scroll(object sender, ScrollEventArgs e)
{
timer1.Interval = 100 - hScrollBar_dichuyen.Value;
label1.Text = timer1.Interval.ToString();
}
private void textbox_nhap_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
str = textbox_nhap.Text;
congkhoangtrang();
}
}

private void radioButton_left_CheckedChanged(object sender, EventArgs e)


{
timer1.Enabled = true;
}

private void radioButton_right_CheckedChanged(object sender, EventArgs e)


{
timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)


{
if (radioButton_right.Checked)
{
string chuoidau = str.Substring(0, str.Length - 1);
string chuoicuoi = str.Substring(str.Length - 1, 1);
str = chuoicuoi + chuoidau;
textBox_chay.Text = str;
}
else
if(radioButton_left.Checked)
{
string chuoidau = str.Substring(1, str.Length - 1);
string chuoicuoi = str.Substring(0,1);
str = chuoidau + chuoicuoi;
textBox_chay.Text = str;
}

}
private void congkhoangtrang()
{
str = textbox_nhap.Text;
int len = str.Length;
for (int i = 0; i < 75 - len; i++)
{
str = str + " ";
}
}
private void Form1_Load(object sender, EventArgs e)
{
congkhoangtrang();
timer1.Enabled=true;
Lớp I91C – Minh - LTN 15
f. Hộp thoại thông dụng – Dialog

namespace Dialog
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OpenFileDialog open;

private void btn1TapTin_Click(object sender, EventArgs e)


{
open = new OpenFileDialog();
open.InitialDirectory = "C:\\windows";
open.Title = "Chọn 1 tập tin";
open.Filter = "Các tập tin DLL|*.dll|Các tập tin INI và LOG|*.ini;*.log|Tất cả các tập tin|*.*";
open.ShowDialog();
txtThongBao.Text = open.FileName;
}

private void btnNhieuTapTin_Click(object sender, EventArgs e)


{
open = new OpenFileDialog();
open.Multiselect = true;

open.InitialDirectory = "C:\\windows";
open.Title = "Chọn 1 tập tin";
open.Filter = "Các tập tin DLL|*.dll|Các tập tin INI và LOG|*.ini;*.log|Tất cả các tập tin|*.*";
open.ShowDialog();
string[] text= open.FileNames;
txtThongBao.Text="Có tất cả "+text.Length.ToString()+" tập tin: \r\n";
foreach (string s in text)
{
txtThongBao.Text += s + "\r\n";
}
}
FolderBrowserDialog folder;
private void btn1ThuMuc_Click(object sender, EventArgs e)
{
folder = new FolderBrowserDialog();

Lớp I91C – Minh - LTN 16


folder.SelectedPath = "C:\\windows";

folder.ShowDialog();
txtThongBao.Text= folder.SelectedPath;

}
ColorDialog color;
private void btnMauNen_Click(object sender, EventArgs e)
{
color = new ColorDialog();
DialogResult rs=color.ShowDialog();
if (rs == DialogResult.OK)
txtThongBao.BackColor = color.Color;
}

private void btnMauChu_Click(object sender, EventArgs e)


{
color = new ColorDialog();
DialogResult rs = color.ShowDialog();
if (rs == DialogResult.OK)
txtThongBao.ForeColor = color.Color;
}
FontDialog font;
private void btnFont_Click(object sender, EventArgs e)
{
font = new FontDialog();
DialogResult rs = font.ShowDialog();
if (rs == DialogResult.OK)
txtThongBao.Font = font.Font;
}

private void btnReset_Click(object sender, EventArgs e)


{
txtThongBao.Text = "Ví dụ lập trình C# sử dụng các hộp thoại thông dụng của hệ điều hành Windows";
txtThongBao.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold);
txtThongBao.BackColor = System.Drawing.Color.SandyBrown;
txtThongBao.ForeColor = System.Drawing.Color.Blue;
}

private void btnThoat_Click(object sender, EventArgs e)


{
timer2.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)


{
if (this.Opacity >= 1)
timer1.Enabled = false;
else
this.Opacity += 0.01;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)


{
//e.Cancel = true;
//if (this.Opacity <= 0)
// Application.Exit();
//else
// this.Opacity -= 0.01;
}

private void timer2_Tick(object sender, EventArgs e)


Lớp I91C – Minh - LTN 17
{
if (this.Opacity <= 0)
Application.Exit();
else
this.Opacity -= 0.01;
}
g. Pha màu

namespace buoi4
{

public partial class Form1 : Form


{
byte val_red;
byte val_green;
byte val_blue;
bool haichieu_red;
bool haichieu_green;
bool haichieu_blue;
public Form1()
{
InitializeComponent();
}

#region
private void phanmau(byte red, byte green, byte blue)
{
pictureBox.BackColor = System.Drawing.Color.FromArgb(red, green, blue);
}
#endregion
private void timer_blue_Tick(object sender, EventArgs e)
{
if (radioButton_1chieu.Checked)
{
if (val_blue == 255)
val_blue = 0;
else
val_blue++;
}
else
{
if (val_blue == 0 && haichieu_blue == false)
haichieu_blue = true;
if (val_blue == 255 && haichieu_blue == true)
haichieu_blue = false;
if (haichieu_blue)
val_blue++;
else
val_blue--;
Lớp I91C – Minh - LTN 18
}
label_blue.Text=val_blue.ToString();
vScrollBar_blue.Value = val_blue;
phanmau(val_red, val_green, val_blue);
}

private void timer_green_Tick(object sender, EventArgs e)


{
if (radioButton_1chieu.Checked)
{
if (val_green == 255)
val_green = 0;
else
val_green++;
}
else
{
if (val_green == 0 && haichieu_green == false)
haichieu_green = true;
if (val_green == 255 && haichieu_green == true)
haichieu_green = false;
if (haichieu_green)
val_green++;
else
val_green--;
}
label_green.Text=val_green.ToString();
vScrollBar_green.Value = val_green;
phanmau(val_red, val_green, val_blue);
}

private void timer_red_Tick(object sender, EventArgs e)


{
if (radioButton_1chieu.Checked)
{
if (val_red == 255)
val_red = 0;
else
val_red++;
}
else
{
if (val_red == 0 && haichieu_red == false)
haichieu_red = true;
if (val_red == 255 && haichieu_red == true)
haichieu_red = false;
if (haichieu_red)
val_red++;
else
val_red--;
}
label_red.Text=val_red.ToString();
vScrollBar_red.Value=val_red;
phanmau(val_red, val_green, val_blue);
}

private void checkBox_green_CheckedChanged(object sender, EventArgs e)


{

if (checkBox_green.Checked == true)
timer_green.Start();
else
Lớp I91C – Minh - LTN 19
timer_green.Stop();
}

private void checkBox_red_CheckedChanged(object sender, EventArgs e)


{
if (checkBox_red.Checked == true)
timer_red.Start();
else
timer_red.Stop();
}

private void checkBox_blue_CheckedChanged(object sender, EventArgs e)


{
if (checkBox_blue.Checked==true)
timer_blue.Start();
else
timer_blue.Stop();
}

private void button_ngung_KeyPress(object sender, KeyPressEventArgs e)


{
if (byte.Parse(e.KeyChar.ToString()) == 13)
{
timer_blue.Enabled = !timer_blue.Enabled;
timer_green.Enabled = !timer_green.Enabled;
timer_red.Enabled = !timer_red.Enabled;
}
}

private void button_ngung_Click(object sender, EventArgs e)


{
timer_blue.Enabled = !timer_blue.Enabled;
timer_green.Enabled = !timer_green.Enabled;
timer_red.Enabled = !timer_red.Enabled;
}

private void hScrollBar1_ValueChanged(object sender, EventArgs e)


{
timer_blue.Interval = hScrollBar1.Value;
timer_green.Interval = timer_blue.Interval + 20;
timer_red.Interval = timer_blue.Interval + 40;
}
h. Đồng hồ chạy ngược (nhập giờ hẹn)

namespace HenGio
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Lớp I91C – Minh - LTN 20
private void Form1_Load(object sender, EventArgs e)
{
//textBox1.Text = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss");
textBox1.Text = DateTime.Now.ToLongTimeString();

private void timer1_Tick(object sender, EventArgs e)


{
//label2.Text = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss");
label2.Text = DateTime.Now.ToLongTimeString();
if (checkBox1.Checked == true)
if (textBox1.Text == label2.Text)
MessageBox.Show("Đã tới giờ rồi???Học bài thôi!!!");
}
}
}

i. Đồng hồ chạy ngược (nhập giây hẹn)

namespace DongHoChayNguoc
{
public partial class Form1 : Form
{
DateTime dt = DateTime.Now;
int SoGiayHen,dem;
public Form1()
{
InitializeComponent();
}

private void timer1_Tick(object sender, EventArgs e)


{
label1.Text = DateTime.Now.ToString();
dem++;
label5.Text = Convert.ToString(dem);
dt = dt.AddSeconds(-1);
label2.Text = dt.ToString();
if (dem == SoGiayHen)
{
MessageBox.Show("Hết giờ hẹn rồi", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
label5.Visible = false;

}
}

private void button1_Click(object sender, EventArgs e)


{
SoGiayHen = Convert.ToInt32(textBox1.Text);
dt= DateTime.Now;
dem = 0;
label5.Text = "";
label5.Visible = true;
Lớp I91C – Minh - LTN 21
}
}
}
j. Vẽ Đường thẳng

namespace VeDuongLine
{
public partial class Form1 : Form
{

int X1, Y1;


Color Mau = Color.Blue;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(Mau);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.DrawLine(myPen, 0, 0, 100, 300);
myPen.Dispose();
formGraphics.Dispose();
}

private void Form_MouseDown(object sender, MouseEventArgs e)


{

X1 = e.X;
Y1 = e.Y;

}
private void Form_MouseUp(object sender, MouseEventArgs e)
{
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(Mau);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.DrawLine(myPen, X1, Y1, e.X, e.Y);
myPen.Dispose();
formGraphics.Dispose();

}
private void button2_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
button2.BackColor = colorDialog1.Color;
Lớp I91C – Minh - LTN 22
Mau = colorDialog1.Color;

private void button3_Click(object sender, EventArgs e)


{
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.Clear(DefaultBackColor);
formGraphics.Dispose();
}

private void chọnMàuToolStripMenuItem_Click(object sender, EventArgs e)


{
colorDialog1.ShowDialog();
button2.BackColor = colorDialog1.Color;
Mau = colorDialog1.Color;

private void vẽ1LineToolStripMenuItem_Click(object sender, EventArgs e)


{
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(Mau);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.DrawLine(myPen, 0, 0, 100, 300);
myPen.Dispose();
formGraphics.Dispose();

private void xóaCácLineToolStripMenuItem_Click(object sender, EventArgs e)


{
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.Clear(DefaultBackColor);
formGraphics.Dispose();

private void thoátToolStripMenuItem_Click(object sender, EventArgs e)


{
Application.Exit();
}
}
}
VE NHIEU DUONG THANG

Lớp I91C – Minh - LTN 23


namespace veduongline2
{
public partial class Form1 : Form
{
int dodaynetve;
int X1, Y1;
Color mau = Color.OrangeRed;
bool Dangve = false;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
colorDialog1.ShowDialog();
button1.BackColor = colorDialog1.Color;
mau = colorDialog1.Color;

}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if(Dangve)
{
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(mau,dodaynetve);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.DrawLine(myPen, X1, Y1, e.X, e.Y);
myPen.Dispose();
formGraphics.Dispose();
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
Dangve = false;
}

private void Form1_MouseDown(object sender, MouseEventArgs e)


{
X1 = e.X;
Y1 = e.Y;
Lớp I91C – Minh - LTN 24
Dangve = true;
}

private void button2_Click(object sender, EventArgs e)


{
System.Drawing.Graphics formGraphics=this.CreateGraphics();
formGraphics.Clear(DefaultBackColor);
formGraphics.Dispose();
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)


{
dodaynetve = listBox1.SelectedIndex + 1;

private void Form1_Load(object sender, EventArgs e)


{
listBox1.SelectedIndex = 0;
}

}
}
k. Vẽ Hình Chữ Nhật

namespace VeHinhChuNhat
{
public partial class Form1 : Form
{
Graphics g;
Pen Mypen = new Pen(Color.Blue, 1);
Point p1 = new Point();
//Point p2 = new Point();
SolidBrush Mybrush = new SolidBrush(Color.Blue);

public Form1()
{
InitializeComponent();
g = this.CreateGraphics();
}

private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)


{
this.Refresh();
}
private void button_chonmau_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
Lớp I91C – Minh - LTN 25
{
Mypen.Color = colorDialog1.Color;
Mybrush.Color = colorDialog1.Color;
}
button_chonmau.BackColor = colorDialog1.Color;

private void Form1_MouseUp(object sender, MouseEventArgs e)


{
switch (e.Button)
{
case MouseButtons.Left:
{

int a, b;
a = e.X - p1.X;
b = e.Y - p1.Y;

if (a < 0 && b < 0)


{
a = a * (-1);
b = b * (-1);
g.DrawRectangle(Mypen, e.X, e.Y, a, b);
}
else if (a > 0 && b < 0)
{
b = b * (-1);
g.DrawRectangle(Mypen,p1.X,e.Y, a, b);
}
else if (a < 0 && b >0)
{
a = a * (-1);
g.DrawRectangle(Mypen, e.X, p1.Y, a, b);
}
else
g.DrawRectangle(Mypen, p1.X, p1.Y, e.X - p1.X, e.Y - p1.Y);

//g.Dispose();
//Mypen.Dispose();
}
break;
case MouseButtons.Right:
{
System.Drawing.SolidBrush mybrush1;
mybrush1 = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);

// g.FillRectangle(mybrush1, p1.X, p1.Y, e.X - p1.X, e.Y - p1.Y);


int a, b;
a = e.X - p1.X;
b = e.Y - p1.Y;
if (a < 0 && b < 0)
{
a = a * (-1);
b = b * (-1);
g.FillRectangle(Mybrush, e.X, e.Y, a, b);
}
else if (a<0 && b>0)
{
a = a * (-1);
g.FillRectangle(Mybrush, e.X, p1.Y, a, b);
}
Lớp I91C – Minh - LTN 26
else if (a > 0 && b < 0)
{
b = b * (-1);
g.FillRectangle(Mybrush, p1.X, e.Y, a, b);
}
else
g.FillRectangle(Mybrush, p1.X, p1.Y, e.X - p1.X, e.Y - p1.Y);
}
break;
case MouseButtons.Middle:
{
g.DrawLine(Mypen, p1.X, p1.Y, e.X, e.Y);

}
break;
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
p1.X = e.X;
p1.Y = e.Y;
}
private void button_xoamau_Click(object sender, EventArgs e)
{
this.Refresh();
}
l. Vẽ Hình Eclipse

Lớp I91C – Minh - LTN 27


m. Notepad

Lớp I91C – Minh - LTN 28


namespace NotePad
{
public partial class NotePad : Form
{
public NotePad()
{
InitializeComponent();
}
private void ClearFont()
{
timesNewRomanToolStripMenuItem.Checked = false;
arialToolStripMenuItem.Checked = false;
courierToolStripMenuItem.Checked = false;
fontToolStripMenuItem.Checked = false;
}

private void timesNewRomanToolStripMenuItem_Click(object sender, EventArgs e)


{
ClearFont();
timesNewRomanToolStripMenuItem.Checked = true;
if (richTextBoxNoiDung.SelectedText.ToString() != "")
{
//Thay đổi font tại vị trí được tô đen
richTextBoxNoiDung.SelectionFont = new Font("Times New Roman", richTextBoxNoiDung.Font.Size,
richTextBoxNoiDung.Font.Style);
}
else
richTextBoxNoiDung.Font= new Font("Times New Roman", richTextBoxNoiDung.Font.Size,
richTextBoxNoiDung.Font.Style);
}

private void arialToolStripMenuItem_Click(object sender, EventArgs e)


{
ClearFont();
arialToolStripMenuItem.Checked = true;
if (richTextBoxNoiDung.SelectedText.ToString() != "")
{
//Thay đổi font tại vị trí được tô đen
richTextBoxNoiDung.SelectionFont = new Font("Arial", richTextBoxNoiDung.Font.Size,
richTextBoxNoiDung.Font.Style);
}
else
richTextBoxNoiDung.Font = new Font("Arial", richTextBoxNoiDung.Font.Size, richTextBoxNoiDung.Font.Style);
}
Lớp I91C – Minh - LTN 29
private void courierToolStripMenuItem_Click(object sender, EventArgs e)
{
ClearFont();
courierToolStripMenuItem.Checked = true;
if (richTextBoxNoiDung.SelectedText.ToString() != "")
{
//Thay đổi font tại vị trí được tô đen
richTextBoxNoiDung.SelectionFont = new Font("Courier", richTextBoxNoiDung.Font.Size,
richTextBoxNoiDung.Font.Style);
}
else
richTextBoxNoiDung.Font = new Font("Courier", richTextBoxNoiDung.Font.Size, richTextBoxNoiDung.Font.Style);
}

private void fontToolStripMenuItem_Click(object sender, EventArgs e)


{
ClearFont();
fontToolStripMenuItem.Checked = true;
FontDialog f = new FontDialog();
DialogResult result=f.ShowDialog();
if (result == DialogResult.Cancel)
return;
if (richTextBoxNoiDung.SelectedText.ToString() != "")
richTextBoxNoiDung.SelectionFont = f.Font;
else
richTextBoxNoiDung.Font = f.Font;
}

private void ClearColor()


{
redToolStripMenuItem.Checked = false;
greenToolStripMenuItem.Checked = false;
blueToolStripMenuItem.Checked = false;
colorToolStripMenuItem1.Checked = false;
}

private void redToolStripMenuItem_Click(object sender, EventArgs e)


{
ClearColor();
redToolStripMenuItem.Checked = true;
if (richTextBoxNoiDung.SelectedText.ToString() != "")
{
//Text được chọn có màu gì thì thay thế bằng màu Red
richTextBoxNoiDung.SelectionColor = Color.Red;
}
else
richTextBoxNoiDung.ForeColor = Color.Red;
}

private void greenToolStripMenuItem_Click(object sender, EventArgs e)


{
ClearColor();
greenToolStripMenuItem.Checked = true;
if (richTextBoxNoiDung.SelectedText.ToString() != "")
{
richTextBoxNoiDung.SelectionColor = Color.Green;
}
else
richTextBoxNoiDung.ForeColor = Color.Green;
}

Lớp I91C – Minh - LTN 30


private void blueToolStripMenuItem_Click(object sender, EventArgs e)
{
ClearColor();
blueToolStripMenuItem.Checked = true;
if (richTextBoxNoiDung.SelectedText.ToString() != "")
{
richTextBoxNoiDung.SelectionColor = Color.Blue;
}
else
richTextBoxNoiDung.ForeColor = Color.Blue;
}

private void colorToolStripMenuItem1_Click(object sender, EventArgs e)


{
ClearColor();
colorToolStripMenuItem1.Checked = true;
ColorDialog c = new ColorDialog();
DialogResult result = c.ShowDialog();
if (result == DialogResult.Cancel)
return;
if (richTextBoxNoiDung.SelectedText.ToString() != "")
richTextBoxNoiDung.SelectionColor = c.Color;
else
richTextBoxNoiDung.ForeColor = c.Color;
}

private void undoToolStripMenuItem_Click(object sender, EventArgs e)


{
richTextBoxNoiDung.Undo();
}

private void cutToolStripMenuItem_Click(object sender, EventArgs e)


{
richTextBoxNoiDung.Cut();
}

private void copyToolStripMenuItem_Click(object sender, EventArgs e)


{
richTextBoxNoiDung.Copy();
}

private void pasteToolStripMenuItem_Click(object sender, EventArgs e)


{
richTextBoxNoiDung.Paste();
}

private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)


{
richTextBoxNoiDung.SelectAll();
}

private void timeDateToolStripMenuItem_Click(object sender, EventArgs e)


{
richTextBoxNoiDung.Text = DateTime.Now.ToString();
}

About a;
private void aToolStripMenuItem_Click(object sender, EventArgs e)
{
a = new About();
a.Show();
timer1.Enabled=true;
Lớp I91C – Minh - LTN 31
}

private void timer1_Tick(object sender, EventArgs e)


{
a.Close();
timer1.Dispose();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openToolStripMenuItem.Checked = true;
saveToolStripMenuItem.Checked = false;
try
{
OpenFileDialog open = new OpenFileDialog();
open.InitialDirectory = @"C:\";
open.Title = "Chon 1 file text";
open.Filter = "Text File|*.txt|Rich Text Format|*.rtf|All Word Documents|*.doc";
open.FilterIndex = 1;
if (open.ShowDialog() == DialogResult.Cancel)
richTextBoxNoiDung.Text = "";
else
{
StreamReader sr = new StreamReader(open.FileName, Encoding.UTF8);
richTextBoxNoiDung.Text = sr.ReadToEnd();
sr.Close();

lblStatus.Text = open.FileName;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
openToolStripMenuItem.Checked = true;
saveToolStripMenuItem.Checked = false;

SaveFileDialog save = new SaveFileDialog();


save.Title = "Luu file lai";
save.Filter = "Text File|*.txt|Rich Text Format|*.rtf|Document|*.doc|Tất cả các file|*.*";
save.FilterIndex = 1;
save.OverwritePrompt = true;
if (save.ShowDialog() != DialogResult.Cancel)
{
StreamWriter sw = new StreamWriter(save.FileName, false, Encoding.UTF8);
foreach (string line in richTextBoxNoiDung.Lines)
sw.WriteLine(line);
sw.Close();

lblStatus.Text = save.FileName;
}
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
richTextBoxNoiDung.Text = "";
richTextBoxNoiDung.Focus();
Lớp I91C – Minh - LTN 32
}

private void NotePad_Load(object sender, EventArgs e)


{
richTextBoxNoiDung.Focus();
}

private void InDam_Click(object sender, EventArgs e)


{
richTextBoxNoiDung.SelectionFont = new Font(richTextBoxNoiDung.SelectionFont.Name,
richTextBoxNoiDung.SelectionFont.Size,
richTextBoxNoiDung.SelectionFont.Style ^ FontStyle.Bold);
}

private void InNghieng_Click(object sender, EventArgs e)


{
richTextBoxNoiDung.SelectionFont = new Font(richTextBoxNoiDung.SelectionFont.Name,
richTextBoxNoiDung.SelectionFont.Size,
richTextBoxNoiDung.SelectionFont.Style ^ FontStyle.Italic);
}

private void GachDuoi_Click(object sender, EventArgs e)


{
richTextBoxNoiDung.SelectionFont = new Font(richTextBoxNoiDung.SelectionFont.Name,
richTextBoxNoiDung.SelectionFont.Size,
richTextBoxNoiDung.SelectionFont.Style ^ FontStyle.Underline);
}

private void pagesetup_Click(object sender, EventArgs e)


{
pageSetupDialog.Document = printDocument;
pageSetupDialog.PageSettings = printDocument.DefaultPageSettings;

pageSetupDialog.AllowMargins = true;
pageSetupDialog.AllowOrientation = true;
pageSetupDialog.AllowPaper = true;
pageSetupDialog.AllowPrinter = true;

pageSetupDialog.ShowNetwork = true;
pageSetupDialog.ShowHelp = true;
pageSetupDialog.EnableMetric = false;

if (pageSetupDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
printDocument.DefaultPageSettings = pageSetupDialog.PageSettings;
}

private void print_Click(object sender, EventArgs e)


{
printDialog.Document = printDocument;
if (printDialog.ShowDialog() == DialogResult.OK)
{
printDocument.Print();
}
}

n. MenuStrip

Lớp I91C – Minh - LTN 33


namespace BaiThiMau
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

Lớp I91C – Minh - LTN 34


}

private void aboutUsToolStripMenuItem_Click(object sender, EventArgs e)


{
AboutBox1 aboutbox = new AboutBox1();
aboutbox.MdiParent = this;
aboutbox.Show();

private void tôMàuTrắngChoMenuFileToolStripMenuItem_Click(object sender, EventArgs e)


{
//menuStrip1.Font
menuStrip1.BackColor = Color.White;
}

private void tôMàuVàngChoMenuFileToolStripMenuItem_Click(object sender, EventArgs e)


{
menuStrip1.BackColor = Color.Yellow;
}

private void tôMàuĐỏChoMenuFileToolStripMenuItem_Click(object sender, EventArgs e)


{
menuStrip1.BackColor = Color.Red;
}

private void vẽLineToolStripMenuItem_Click(object sender, EventArgs e)


{
VeLine.Form1 frm1 = new VeLine.Form1();
frm1.MdiParent = this;
frm1.Show();
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)


{
Application.Exit();
}
o. ListBoxMultiSelect

private void LapTrinh_ListBox_Load(object sender, EventArgs e)


Lớp I91C – Minh - LTN 35
{
for (int i = 0; i < 20; i++)
{
listBox1.Items.Add(i);
}
label1.Text = listBox1.Items.Count.ToString() + " phần tử";
label2.Text = listBox2.Items.Count.ToString() + " phần tử";
}
private void btn_chontatca_Click(object sender, EventArgs e)
{
for (int i = 0; i < listBox1.Items.Count; i++)
listBox1.SetSelected(i, true);
}
private void btn_chonchiso_Click(object sender, EventArgs e)
{
try
{
listBox1.SelectedIndex = Convert.ToInt16(txt_ChonLaiChiSo.Text);
}
catch
{
MessageBox.Show("Nhập số sai", "Báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btn_bochon_Click(object sender, EventArgs e)
{
for (int i = 0; i < listBox1.Items.Count; i++)
listBox1.SetSelected(i, false);
}
private void btn_bochochiso_Click(object sender, EventArgs e)
{
try
{
listBox1.SetSelected(Convert.ToInt16(txt_BoChonTaiChiSo.Text), false);
}
catch
{
MessageBox.Show("Nhập số sai", "Báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btn_xoatatca_dachon_Click(object sender, EventArgs e)
{
while (listBox1.SelectedItems.Count > 0)
{
listBox1.Items.Remove(listBox1.SelectedItem);
}
label1.Text = listBox1.Items.Count.ToString() + " phần tử";
}
private void btn_xoataichiso_Click(object sender, EventArgs e)
{
try
{
listBox1.Items.RemoveAt(Convert.ToInt16(txt_XoaTaiChiSo.Text));
label1.Text = listBox1.Items.Count.ToString();
}
catch
{
MessageBox.Show("Nhập số sai", "Báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btn_chepqua_Click(object sender, EventArgs e)
{
Lớp I91C – Minh - LTN 36
foreach (object ob in listBox1.SelectedItems)
{
listBox2.Items.Add(ob.ToString());
}
label2.Text = listBox2.Items.Count.ToString() + " phần tử";
}
private void btn_chuyenqua_Click(object sender, EventArgs e)
{
while (listBox1.SelectedItems.Count > 0)
{
listBox2.Items.Add(listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);
}
label1.Text = listBox1.Items.Count.ToString() + " phần tử";
label2.Text = listBox2.Items.Count.ToString() + " phần tử";
}
private void btn_themvaocuoi_Click(object sender, EventArgs e)
{
listBox2.Items.Add(txt_ChuoiThemVao.Text);
label2.Text = listBox2.Items.Count.ToString() + " phần tử";
}
private void btn_themvaotai_Click(object sender, EventArgs e)
{
try
{
listBox2.Items.Insert(Convert.ToInt16(txt_ThemVaoTai.Text), txt_ChuoiThemVao.Text);
label2.Text = listBox1.Items.Count.ToString() + " phần tử";
}
catch
{
MessageBox.Show("Nhập số sai", "Báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btn_Clear_Click(object sender, EventArgs e)
{
listBox2.Items.Clear();
label2.Text = listBox2.Items.Count.ToString() + " phần tử";
}
private void label1_Click(object sender, EventArgs e)
{
label1.Text = listBox1.Items.Count.ToString() + " phần tử";
}
private void btn_Reset_Click(object sender, EventArgs e)
{

listBox1.Items.Clear();
for (int i = 0; i < 20; i++)
{
listBox1.Items.Add(i);
}
label1.Text = listBox1.Items.Count.ToString() + " phần tử";
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
listBox2.Sorted = checkBox1.Checked;
}
}
}
p. Xem Hình Slideshow

Lớp I91C – Minh - LTN 37


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

//string ThuMuc;

private void button1_Click(object sender, EventArgs e)


{
OpenFileDialog of =new OpenFileDialog();
of.InitialDirectory = @"C:\WINDOWS\Web\Wallpaper";
of.Multiselect = true;
of.Filter = "Các tập tin JPG, JPEG, BMP, WMF|*.JPG;*.JPEG;*.BMP;*.WMF|Tất cả các tập tin|*.*";
of.ShowDialog();
string[] CacFile = of.FileNames;
for(int i=0;i<CacFile.Length;i++)
{
listBox1.Items.Add(CacFile[i]);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
label2.Text = listBox1.SelectedItem.ToString();
pictureBox1.Image = Image.FromFile(label2.Text);
}
catch { }
}

private void button2_Click(object sender, EventArgs e)


{

timer1.Enabled = false;
button3.Text = "Chạy\r\ntự động";
listBox1.Items.Clear();
pictureBox1.Image = null;
label2.Text = "";
timer1.Interval = 500;

Lớp I91C – Minh - LTN 38


trackBar1.Value = 500;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (listBox1.SelectedIndex < listBox1.Items.Count-1)
listBox1.SelectedIndex += 1;
else
listBox1.SelectedIndex = 0;
}
private void button3_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
if (timer1.Enabled == false)
{
timer1.Enabled = true;
button3.Text = "Dừng";
}
else
{
timer1.Enabled = false;
button3.Text = "Chạy\r\ntự động";
}
}
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
timer1.Interval = trackBar1.Value;
}
private void button4_Click(object sender, EventArgs e)
{
if (button4.Text == "Full screen\r\n(Escape)")
{
this.FormBorderStyle = FormBorderStyle.None;
this.TopMost = true;
this.WindowState = FormWindowState.Maximized;
pictureBox1.Dock = DockStyle.Fill;
button4.Text = "Normal view\r\n(Escape)";
}
else
{
this.FormBorderStyle = FormBorderStyle.Sizable;
this.TopMost = false;
this.WindowState = FormWindowState.Normal;
pictureBox1.Dock = DockStyle.None;
button4.Text = "Full screen\r\n(Escape)";
}
}
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
button4.PerformClick();
}
q. Đổ Xúc Xắc

Lớp I91C – Minh - LTN 39


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;

namespace XucXac
{
public partial class Form1 : Form
{
int So, Doan,LanThang,LanThua, LanDoan;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{

Random rd = new Random();


So = rd.Next(1, 7);
LanDoan += 1;
label1.Image = Image.FromFile(Directory.GetCurrentDirectory() + "\\HinhXucXac\\die" + So + ".gif");
if (So == Doan)
{
LanThang += 1;
listBox1.Items.Add(LanDoan.ToString()+ ". Thắng ( đoán " + Doan + ", ra " + So + " )");
}
else
{
LanThua += 1;
listBox1.Items.Add(LanDoan.ToString()+ ". Thua ( đoán " + Doan + ", ra " + So + " )");
}
//using System.Globalization;
NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
nfi.PercentDecimalDigits = 4;
Lớp I91C – Minh - LTN 40
Double TiLeThang = (double)LanThang / LanDoan;
Double TiLeThua = (double)LanThua / LanDoan;
label5.Text = "Lần đoán: " + LanDoan.ToString();
label3.Text = "Lần thắng: " + LanThang.ToString() + " (" + TiLeThang.ToString("P", nfi) + ")";
label4.Text = "Lần thua: " + LanThua.ToString() + " (" + TiLeThua.ToString("P", nfi) + ")";
listBox1.SelectedIndex = listBox1.Items.Count - 1;

private void button2_Click(object sender, EventArgs e)


{
label2.Image = Image.FromFile(Directory.GetCurrentDirectory() + "\\HinhXucXac\\die1.gif");
Doan = 1;
}

private void button3_Click(object sender, EventArgs e)


{
label2.Image = Image.FromFile(Directory.GetCurrentDirectory() + "\\HinhXucXac\\die2.gif");
Doan = 2;
}

private void button5_Click(object sender, EventArgs e)


{
label2.Image = Image.FromFile(Directory.GetCurrentDirectory() + "\\HinhXucXac\\die3.gif");
Doan = 3;
}

private void button6_Click(object sender, EventArgs e)


{
label2.Image = Image.FromFile(Directory.GetCurrentDirectory() + "\\HinhXucXac\\die4.gif");
Doan = 4;
}

private void button7_Click(object sender, EventArgs e)


{
label2.Image = Image.FromFile(Directory.GetCurrentDirectory() + "\\HinhXucXac\\die5.gif");
Doan = 5;
}

private void button4_Click(object sender, EventArgs e)


{
label2.Image = Image.FromFile(Directory.GetCurrentDirectory() + "\\HinhXucXac\\die6.gif");
Doan = 6;
}

private void Form1_Load(object sender, EventArgs e)


{
button2.PerformClick();
}

private void button8_Click(object sender, EventArgs e)


{
LanDoan = 0;
LanThang = 0;
LanThua = 0;
listBox1.Items.Clear();
label3.Text = "";
label4.Text = "";
label5.Text = "";
}
}
Lớp I91C – Minh - LTN 41
}

1) LICH THE GIOI

namespace LichTheGioi
{
public partial class Form1 : Form
{
DateTime dt;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
dt = DateTime.Now;
switch (comboBox1.SelectedIndex)
{
case 0: break;
case 1: dt = dt.AddHours(1); break;
case 2: dt = dt.AddHours(-12); break;
case 3: dt = dt.AddHours(2); break;
case 4: dt = dt.AddHours(-6); break;
}
label1.Text = "Nam " + dt.Year.ToString();
label2.Text = "Thang " + dt.Month.ToString();
label3.Text = dt.Day.ToString();
switch (dt.DayOfWeek)
{
case DayOfWeek.Monday: label4.Text = "Thu hai"; break;
Lớp I91C – Minh - LTN 42
case DayOfWeek.Tuesday: label4.Text = "Thu ba"; break;
case DayOfWeek.Wednesday: label4.Text = "Thu tu"; break;
case DayOfWeek.Thursday: label4.Text = "Thu nam"; break;
case DayOfWeek.Friday: label4.Text = "Thu sau"; break;
case DayOfWeek.Saturday: label4.Text = "Thu bay"; break;
case DayOfWeek.Sunday: label4.Text = "Chu Nhat"; break;

}
label5.Text = dt.ToString("hh:mm:ss");

}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("Viet Nam");
comboBox1.Items.Add("Trung Quoc");
comboBox1.Items.Add("My");
comboBox1.Items.Add("Nhat");
comboBox1.Items.Add("Phap");
comboBox1.SelectedIndex = 0;
}
}
}

2) IN BẢNG SỐ XE

namespace In_bang_so_xe
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string BS;
listBox1.Items.Clear();
Cursor.Current = Cursors.WaitCursor;
for (int i = 1; i <= 9999; i++)
{
BS = i.ToString();
Lớp I91C – Minh - LTN 43
switch (BS.Length)
{
case 1: BS = "000" + BS; break;
case 2: BS = "00" + BS; break;
case 3: BS = "0" + BS; break;
}
listBox1.Items.Add(BS);
}
label1.Text = listBox1.Items.Count.ToString();
}

private void button4_Click(object sender, EventArgs e)


{
string BS, s1, s2, s3, s4;
listBox1.Items.Clear();
Cursor.Current = Cursors.WaitCursor;
for (int i = 1; i <= 9999; i++)
{
BS = i.ToString();
switch (BS.Length)
{
case 1: BS = "000" + BS; break;
case 2: BS = "00" + BS; break;
case 3: BS = "0" + BS; break;
}
s1 = BS.Substring(0, 1);
s2 = BS.Substring(1, 1);
s3 = BS.Substring(2, 1);
s4 = BS.Substring(3, 1);
if (s1 == s4 && s2 == s3)
listBox1.Items.Add(BS);
}
label1.Text = listBox1.Items.Count.ToString();
}

private void button2_Click(object sender, EventArgs e)


{
string BS, s1, s2, s3, s4;
listBox1.Items.Clear();
Cursor.Current = Cursors.WaitCursor;
for (int i = 1; i <= 9999; i++)
{
BS = i.ToString();
switch (BS.Length)
{
case 1: BS = "000" + BS; break;
case 2: BS = "00" + BS; break;
case 3: BS = "0" + BS; break;
}

s1 = BS.Substring(0, 1);
s2 = BS.Substring(1, 1);
s3 = BS.Substring(2, 1);
s4 = BS.Substring(3, 1);
Int32 S = (Convert.ToByte(s1) + Convert.ToByte(s2) + Convert.ToByte(s3) + Convert.ToByte(s4));
if (S % 10 == 9)
listBox1.Items.Add(BS);
}
label1.Text = listBox1.Items.Count.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
Lớp I91C – Minh - LTN 44
string BS, s1, s2, s3, s4;
listBox1.Items.Clear();
Cursor.Current = Cursors.WaitCursor;
for (int i = 1; i <= 9999; i++)
{
BS = i.ToString();
switch (BS.Length)
{
case 1: BS = "000" + BS; break;
case 2: BS = "00" + BS; break;
case 3: BS = "0" + BS; break;
}
s1 = BS.Substring(0, 1);
s2 = BS.Substring(1, 1);
s3 = BS.Substring(2, 1);
s4 = BS.Substring(3, 1);
if ((s1 == s3 && s1 != s2) || (s2 == s4 && s2 != s3))
listBox1.Items.Add(BS);
}
label1.Text = listBox1.Items.Count.ToString();
}
private void button5_Click(object sender, EventArgs e)
{
string BS, s1, s2, s3, s4;
listBox1.Items.Clear();
Cursor.Current = Cursors.WaitCursor;
for (int i = 1; i <= 9999; i++)
{
BS = i.ToString();
switch (BS.Length)
{
case 1: BS = "000" + BS; break;
case 2: BS = "00" + BS; break;
case 3: BS = "0" + BS; break;
}
s1 = BS.Substring(0, 1);
s2 = BS.Substring(1, 1);
s3 = BS.Substring(2, 1);
s4 = BS.Substring(3, 1);

if (((Convert.ToInt32(s1) == (Convert.ToInt32(s2) - 1)) && (Convert.ToInt32(s2) == (Convert.ToInt32(s3) - 1))


|| (Convert.ToInt32(s2) == (Convert.ToInt32(s3) - 1)) && (Convert.ToInt32(s3) == (Convert.ToInt32(s4) - 1))))
listBox1.Items.Add(BS);
}
label1.Text = listBox1.Items.Count.ToString();
}
private void button6_Click(object sender, EventArgs e)
{
string BS, s1, s2, s3, s4;
listBox1.Items.Clear();
Cursor.Current = Cursors.WaitCursor;
for (int i = 1; i <= 9999; i++)
{
BS = i.ToString();
switch (BS.Length)
{
case 1: BS = "000" + BS; break;
case 2: BS = "00" + BS; break;
case 3: BS = "0" + BS; break;
}
s1 = BS.Substring(0, 1);
s2 = BS.Substring(1, 1);
Lớp I91C – Minh - LTN 45
s3 = BS.Substring(2, 1);
s4 = BS.Substring(3, 1);

if ((Convert.ToInt32(s1) < Convert.ToInt32(s2) && Convert.ToInt32(s2) < Convert.ToInt32(s3) &&


Convert.ToInt32(s3) < Convert.ToInt32(s4)))
listBox1.Items.Add(BS);
}
label1.Text = listBox1.Items.Count.ToString();
}
}
}

3) PHUONG TRÌNH BẬC 1

namespace phuongtrinhbac1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
double a = 0;
double b = 0;
double kq=0;
a = double.Parse(textBox1.Text);
b = double.Parse(textBox2.Text);
textBox1.Text = "";
textBox2.Text = "";
if (a == 0)
{
if (b == 0)
label5.Text = "Phương trình vô số nghiệm";
else
label5.Text = "Phương trình vô nghiệm";
}
else
kq = -b/a;
label5.Text = "Phương trình có nghiệm là:" + kq.ToString();

}
}
}
4) IN MA ASCII

Lớp I91C – Minh - LTN 46


namespace InMaASCII
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
for (int n = 65; n <= 90; n++)
{
listBox1.Items.Add("Mã ASCII của " + Convert.ToChar(n) + " là " + n);
}
listBox1.Items.Count.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
for (int n = 97; n <= 122; n++)
{
listBox1.Items.Add("Mã ASCII của " + Convert.ToChar(n) + " là " + n);
}
listBox1.Items.Count.ToString();
}

private void button3_Click(object sender, EventArgs e)


{
listBox1.Items.Clear();
for (int n = 48; n <= 57; n++)
{

listBox1.Items.Add("Mã ASCII của " + Convert.ToChar(n) + " là " + n);


}
listBox1.Items.Count.ToString();

private void button4_Click(object sender, EventArgs e)


{
listBox1.Items.Clear();
for (int n = 65; n <= 90; n++)
{
Lớp I91C – Minh - LTN 47
listBox1.Items.Add("Mã ASCII của " + Convert.ToChar(n) + " là " + n);
}
listBox1.Items.Add("---------");
for (int n = 97; n <= 122; n++)
{
listBox1.Items.Add("Mã ASCII của " + Convert.ToChar(n) + " là " + n);
}
listBox1.Items.Add("---------");
for (int n = 48; n <= 57; n++)
{
listBox1.Items.Add("Mã ASCII của " + Convert.ToChar(n) + " là " + n);
}
listBox1.Items.Count.ToString();

}
private void button5_Click(object sender, EventArgs e)
{
Int16 n = Convert.ToInt16(textBox1.Text);
label1.Text = Convert.ToChar(n).ToString();
}
}

5) ĐỀ THI MẪU

namespace Dethi
{
public partial class Dethi : Form
{
public Dethi()
{
InitializeComponent();
}
private void aboutF1ToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox ab = new AboutBox();
ab.MdiParent = this;
ab.Show();
}
private void thoátCtrXToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
Lớp I91C – Minh - LTN 48
private void thuNhỏToolStripMenuItem_Click(object sender, EventArgs e)
{
if (thuNhỏToolStripMenuItem.Checked)
{ this.WindowState = FormWindowState.Normal; }
else
{ this.WindowState = FormWindowState.Maximized; }
}

private void inBảngSốXeToolStripMenuItem_Click(object sender, EventArgs e)


{
In_bang_so_xe.Form1 f1 = new In_bang_so_xe.Form1();
f1.MdiParent = this;
f1.Show();
}
private void phaMàuToolStripMenuItem_Click(object sender, EventArgs e)
{

}
}
}

6) ĐỔI NGÀY THÁNG NĂM (VIỆT –ANH)

namespace NgayThangNam
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 31; i++)
{
comboBox1.Items.Add(i);
}
comboBox1.Text = Convert.ToString(DateTime.Now.Day);
for (int i = 1; i <= 12; i++)
{
comboBox2.Items.Add(i);
}
comboBox2.Text = Convert.ToString(DateTime.Now.Month);
for (int i = 1900; i <= 2100; i++)
{
comboBox3.Items.Add(i);
}
comboBox3.Text = Convert.ToString(DateTime.Now.Year);

Lớp I91C – Minh - LTN 49


DateTime dt = new DateTime(Convert.ToInt32(comboBox3.Text), Convert.ToInt32(comboBox2.Text),Convert.ToInt32(
comboBox1.Text));
label1.Text = ngaythuTiengAnh(dt);
label2.Text = ngaythuTiengViet(dt);
this.Text = dt.ToString("dd/MM/yyyy");
}
private string ngaythuTiengViet(DateTime dt)
{
string thu = "";
switch (dt.DayOfWeek)
{
case DayOfWeek.Saturday: thu = "Thứ bảy";
break;
case DayOfWeek.Sunday: thu = "Chủ Nhật";
break;
case DayOfWeek.Monday: thu = "Thứ hai";
break;
case DayOfWeek.Tuesday: thu = "Thứ ba";
break;
case DayOfWeek.Wednesday: thu = "Thứ tư";
break;
case DayOfWeek.Thursday: thu = "Thứ năm";
break;
case DayOfWeek.Friday: thu = "Thứ sáu";
break;
}
return thu + " ,ngày " + dt.ToString("dd") + " tháng " + dt.ToString("MM") + " năm " + dt.ToString("yyyy");
}
private string ngaythuTiengAnh(DateTime dt)
{
string kt;
if ((dt.Day == 1) || (dt.Day == 21) || (dt.Day == 31))
kt = "st";
if ((dt.Day == 2) || (dt.Day == 22))
kt = "nd";
if ((dt.Day == 3) || (dt.Day == 23))
kt = "rd";
else kt = "th";
return dt.ToString("dddd") + " " + dt.ToString("MMMM") + " " + dt.ToString("dd") + kt+" " + dt.ToString("yyyy");
}

private void button1_Click(object sender, EventArgs e)


{
try
{
DateTime dt = new DateTime(Convert.ToInt32(comboBox3.Text), Convert.ToInt32(comboBox2.Text),
Convert.ToInt32(comboBox1.Text));
label1.Text = ngaythuTiengAnh(dt);
label2.Text = ngaythuTiengViet(dt);
this.Text = dt.ToString("dd/MM/yyyy");
}
catch
{
MessageBox.Show("Không có ngày này", "có lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}

7) XỬ LÝ CHUỔI:

Lớp I91C – Minh - LTN 50


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

private void textBox1_TextChanged(object sender, EventArgs e)


{

private void button1_Click(object sender, EventArgs e)


{
textBox1.SelectionStart = Convert.ToInt16(textBox2.Text) - 1;
textBox1.SelectionLength = Convert.ToInt16(textBox3.Text);
textBox1.Focus();
}

private void button2_Click(object sender, EventArgs e)


{
label3.Text = Convert.ToString(textBox1.SelectionStart + 1);
label4.Text = Convert.ToString(textBox1.SelectionLength);
textBox1.Focus();
}
}
}

8) TRUY XUẤT CHUỖI

namespace TruyXuatFile
{
public partial class Form1 : Form
Lớp I91C – Minh - LTN 51
{
string filename;
string strLine;
string strAll;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
saveFileDialog1.ShowDialog();
filename = saveFileDialog1.FileName;
textBox1.Text = filename;

FileStream aFile = new FileStream(filename, FileMode.Create);


StreamWriter sw = new StreamWriter(aFile);

sw.Write("File vừa tạo ra");


sw.Close();
aFile.Close();
}

private void button2_Click(object sender, EventArgs e)


{
saveFileDialog1.ShowDialog();
//openFileDialog1.ShowDialog();
filename = saveFileDialog1.FileName;
textBox1.Text = filename;

FileStream aFile = new FileStream(filename, FileMode.Create);


StreamWriter sw = new StreamWriter(aFile);

strAll = richTextBox1.Text;
sw.Write(strAll);
sw.Close();
aFile.Close();
}

private void button3_Click(object sender, EventArgs e)


{
openFileDialog1.ShowDialog();
filename = openFileDialog1.FileName;
textBox1.Text = filename;

FileStream aFile = new FileStream(filename, FileMode.Open);


StreamReader sr = new StreamReader(aFile);

strAll = sr.ReadToEnd();
richTextBox1.Text = strAll;
sr.Close();
aFile.Close();
}
}
}

10) ĐỆ QUY VÀ VÒNG LẶP

Lớp I91C – Minh - LTN 52


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

private void button1_Click(object sender, EventArgs e)


{
Cursor.Current = Cursors.WaitCursor;
UInt64 n;
string kq="";
n = Convert.ToUInt64(textBox1.Text);
for (UInt64 i = 1; i <= n; i++)
{
kq += i + "! = " + GiaiThua(i).ToString() + "\n";
}
richTextBox1.Text = kq;

private void button2_Click(object sender, EventArgs e)


{
Cursor.Current = Cursors.WaitCursor;
UInt64 n;
string kq = "";
n=Convert.ToUInt64(textBox1.Text);

for (UInt64 i = 1; i <= n; i++)


{
kq += "Phần tử thứ " + i + ": " + Fibo(i).ToString() + "\n";
}
richTextBox1.Text = kq;
}

private void button3_Click(object sender, EventArgs e)


{
Cursor.Current = Cursors.WaitCursor;
UInt64 n;
UInt64 x=1;
string kq = "";
n = Convert.ToUInt64(textBox1.Text);

Lớp I91C – Minh - LTN 53


for (UInt64 i = 1; i <= n; i++)
{
x = x * i;
kq += i + "! = " + x + "\n";
}
richTextBox1.Text = kq;
}

private void button4_Click(object sender, EventArgs e)


{
Cursor.Current = Cursors.WaitCursor;
UInt64 n;
UInt64 x=0;
UInt64 y=1;
UInt64 tam;
richTextBox1.Text = "";
string kq = "Phần tử thứ 1: 0\nPhần tử thứ 2: 1\n";
n = Convert.ToUInt64(textBox1.Text);

for (UInt64 i = 3; i <= n; i++)


{
tam = y;
y = x + y;
x = tam;

kq += "Phần tử thứ " + i + ": " + y + "\n";


}
richTextBox1.Text = kq;
}
//Hàm Fibo viết đệ quy
public UInt64 Fibo(UInt64 so)
{
if (so == 1)
return 0;
else
if (so == 2)
return 1;
else
return (Fibo(so - 2) + Fibo(so - 1));
}
//Hàm giai thừa viết đệ quy
public UInt64 GiaiThua(UInt64 so)
{
if (so == 1)
return 1;
else
return so * GiaiThua(so - 1);
}
}
}

10) CỘNG TRỪ GIỜ,PHÚT, GIÂY, NGÀY, THÁNG, NĂM

namespace NgayThangNam
{
public partial class Form1 : Form

Lớp I91C – Minh - LTN 54


{
public Form1()
{
InitializeComponent();
}

private void timer1_Tick(object sender, EventArgs e)


{
dateTimePicker1.Value = DateTime.Now;

private void Form1_Load(object sender, EventArgs e)


{
comboBox1.SelectedIndex = 2;
}

private void button1_Click(object sender, EventArgs e)


{
DateTime d = dateTimePicker1.Value;
double db = Convert.ToDouble(textBox1.Text);

switch (comboBox1.SelectedIndex)
{
case 0:
d = d.AddHours(db);
break;
case 1:
d = d.AddMinutes(db);
break;
case 2:
d = d.AddSeconds(db);
break;
case 3:
d = d.AddDays(db);
break;
case 4:
d = d.AddMonths(Convert.ToInt16(db));
break;
case 5:
d = d.AddYears(Convert.ToInt16(db));
break;
}
label1.Text = d.ToString("dd/MM/yyyy hh:mm:ss");
}

private void button2_Click(object sender, EventArgs e)


{
if (timer1.Enabled )
{
timer1.Enabled = false;
button2.Text = "Chạy đồng hồ";
}
else
{
timer1.Enabled = true;
button2.Text = "Dừng đồng hồ";
}
}
}
}

Lớp I91C – Minh - LTN 55


11) NGUYÊN TỐ CHÍNH PHƯƠNG

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

private void button1_Click(object sender, EventArgs e)


{
label1.Text = "";
try
{
Cursor.Current = Cursors.WaitCursor;
UInt64 SoNhap = Convert.ToUInt64(textBox1.Text);
if (XetNguyenTo(SoNhap) == true)
label1.Text = "là số nguyên tố";
else
label1.Text = "không là số nguyên tố";
}
catch
{
label1.Text = "có lỗi";
}
}
private bool XetNguyenTo(UInt64 So)
{
bool KQ = true;
if (So > 1)
{
for (UInt64 i = 2; i <= Convert.ToUInt64(So/2); i++)
{
if (So % i == 0)
{
KQ = false;
break;
}
}
}
return KQ;
}
private bool XetChinhPhuong(UInt64 So)
{
double d = Math.Sqrt(So);

Lớp I91C – Minh - LTN 56


if (d % 1 == 0)
return true;
else
return false;

}
private void button3_Click(object sender, EventArgs e)
{
try
{
Cursor.Current = Cursors.WaitCursor;
UInt64 n1 = Convert.ToUInt64(textBox2.Text);
UInt64 n2 = Convert.ToUInt64(textBox3.Text);
listBox1.Items.Clear();
label2.Text = "";
for (UInt64 i = n1; i <= n2; i++)
{
if (XetNguyenTo(i) == true)
listBox1.Items.Add(i);
}
label2.Text = listBox1.Items.Count.ToString();
}
catch
{
label2.Text = "có lỗi";
}

private void button2_Click(object sender, EventArgs e)


{
label1.Text = "";
try
{
Cursor.Current = Cursors.WaitCursor;
UInt64 SoNhap = Convert.ToUInt64(textBox1.Text);
if (XetChinhPhuong(SoNhap) == true)
label1.Text = "là số chính phương";
else
label1.Text = "không là số chính phương";
}
catch
{
label1.Text = "có lỗi";
}
}
private void button4_Click(object sender, EventArgs e)
{
try
{
Cursor.Current = Cursors.WaitCursor;
UInt64 n1 = Convert.ToUInt64(textBox2.Text);
UInt64 n2 = Convert.ToUInt64(textBox3.Text);
listBox1.Items.Clear();
label2.Text = "";
for (UInt64 i = n1; i <= n2; i++)
{
if (XetChinhPhuong(i) == true)
listBox1.Items.Add(i);
}
label2.Text = listBox1.Items.Count.ToString();
}
Lớp I91C – Minh - LTN 57
catch
{
label2.Text = "có lỗi";
}

}
}
}

12) ĐỆ QUY VÒNG LẶP

namespace DeQui
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private ulong GiaiThua(ulong so)
{
if (so == 1)
return 1;
else
return so * GiaiThua(so - 1);
}
private void button1_Click(object sender, EventArgs e)
{
ulong x;
string kq="";
x = Convert.ToUInt64(textBox1.Text);
for (ulong i = 1; i <= x; i++)
{
kq += i + "!=" + Convert.ToString(GiaiThua(i)) + '\n';
richTextBox2.Text = kq;
}

private void button2_Click(object sender, EventArgs e)


{
ulong n;
ulong x = 1;
string kq = "";
Lớp I91C – Minh - LTN 58
n = Convert.ToUInt64(textBox1.Text);
for (ulong i = 1; i <= n; i++)
{
x = x * i;
kq += i + "!=" + x + '\n';
richTextBox4.Text = kq;
}

}
private ulong Fibo(ulong so)
{
if (so == 1)
return 0;
else
if(so == 2)
return 1;
else
return (Fibo(so - 2) + Fibo(so - 1));
}
private void button4_Click(object sender, EventArgs e)
{
ulong x;
string kq = "ptu thu 1:0\nptu thu 2:1\n";
x = Convert.ToUInt64(textBox2.Text);
for (ulong i = 3; i <= x; i++)
{
kq += "ptu thu " + i + ":" + Convert.ToString(Fibo(i)) + '\n';
richTextBox1.Text = kq;
}
}

private void button3_Click(object sender, EventArgs e)


{
ulong x = 0;
ulong y = 1;
ulong tam;
// richTextBox3.Text="";
string kq="ptu thu 1:0\nptu thu 2:1\n";
ulong n=Convert.ToUInt64(textBox2.Text);
for(ulong i=3;i<=n;i++)
{
tam=y;
y=x+y;
x=tam;
kq+="ptu thu "+i+":"+y+'\n';
richTextBox3.Text=kq;
}
}

}
}
13) LẤY KÝ TỰ

Lớp I91C – Minh - LTN 59


namespace laykytu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int i=-1;
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "Cộng hòa xã hội chủ nghĩa Việt Nam";
}

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)


{
i++;

if (i < textBox1.Text.Length)
{

e.KeyChar=Convert.ToChar(textBox1.Text.Substring(i,1));
}
else
e.KeyChar=Convert.ToChar(Keys.None);
}
}
}

STT Trang

1. Tính tổng N.................................................................................3


2. 4 Phép tính cơ bản.......................................................................3
3. Đổi ương Lich sang Âm Lịch......................................................5
4. Các phép toán số học và logic.....................................................6
5. Đọc số thành chữ.........................................................................7
6. Tọa độ trên form..........................................................................11
Lớp I91C – Minh - LTN 60
7. Điều khiển Control bằng tay........................................................11
8. Tự động chạy(chạy 2chiều).........................................................13
9. Tự động chạy (chạy 4chiều)........................................................13
10. Hộp chữ chạy..............................................................................14
11. Hộp thoại thông dụng(dialog).....................................................16
12. Pha màu.......................................................................................18
13. Đồng hồ chạy ngược(nhập giờ hẹn)............................................21
14. Đồng hồ chạy ngược(nhập giây hẹn)...........................................21
15. Vẽ đường thẳng...........................................................................22
16. Vẽ nhiều đường...........................................................................24
17. Vẽ hình chữ nhật.........................................................................24
18. Vẽ hình Eclipse...........................................................................26
19. Notepad.......................................................................................27
20. MenuStrip....................................................................................32
21. ListBoxMultiSelect.....................................................................34
22. Xem hình Slideshow...................................................................37
23. Đổ xúc xắc..................................................................................38
24. Lịch thế giới................................................................................41
25. In bảng số xe...............................................................................42
26. Phương trình bậc 1......................................................................45
27. In mã ASCII................................................................................46
28. Đề thi mẫu...................................................................................47
29. Đổi ngày tháng năm(Việt-Anh)...................................................48
30. Xử lý chuổi..................................................................................50
31. Truy xuất chuỗi...........................................................................50
32. Đệ quy và vòng lặp......................................................................52
33. Cộng trừ giờ,phút, giây, ngày, tháng, năm..................................53
34. Nguyên tố chính phương.............................................................55
35. Đệ quy vòng lặp..........................................................................57
36. Lấy ký tự.....................................................................................58

Lớp I91C – Minh - LTN 61

You might also like