Bài Tập Code Lập Trình Trực Quan: Console a. Định nghĩa mảng
Bài Tập Code Lập Trình Trực Quan: Console a. Định nghĩa mảng
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 = " );
I. Graphics – Buoi 1
a. Tính tổng N
namespace TinhTongN
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
private int tinhtong()
{
int a= int.Parse(textBox_nhapn.Text);
int tong = 0;
for (int i = 0; i <= a; i++)
{
tong += i;
}
return tong;
}
namespace NamAmLich
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
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);
namespace DoiSoThanhChu
public Form1()
InitializeComponent();
string kq = "";
switch (so)
case 0: kq = "không";
break;
case 1: kq = "một";
break;
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;
byte HDV=Convert.ToByte(so%10);
byte HC=Convert.ToByte(so/10);
string kq = "";
kq = DocMotSo(so);
kq = "mười";
kq = "mười lăm";
else
return kq;
string kq = "";
kq = DocHaiSo(so);
else if (so == 100 || so == 200 || so == 300 || so == 400 || so == 500 || so == 600 || so == 700 || so == 800 || so == 900)
else if (HC == 0)
return kq;
string kq = "";
kq = DocBaSo(so);
else if (Basocuoi == 0)
else if (HT == 0)
else
return kq;
ulong n = Convert.ToUInt64(textBox1.Text);
lblkq.Text = DocSauSo(n);
Application.Exit();
namespace DieukhienControlbangtay
{
public partial class Form1 : Form
{
int x,x_old;
int y,y_old;
public Form1()
{
InitializeComponent();
}
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 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;
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();
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;
}
namespace buoi4
{
#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);
}
if (checkBox_green.Checked == true)
timer_green.Start();
else
Lớp I91C – Minh - LTN 19
timer_green.Stop();
}
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();
namespace DongHoChayNguoc
{
public partial class Form1 : Form
{
DateTime dt = DateTime.Now;
int SoGiayHen,dem;
public Form1()
{
InitializeComponent();
}
}
}
namespace VeDuongLine
{
public partial class Form1 : Form
{
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 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;
}
}
}
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();
}
int a, b;
a = e.X - p1.X;
b = 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);
}
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
About a;
private void aToolStripMenuItem_Click(object sender, EventArgs e)
{
a = new About();
a.Show();
timer1.Enabled=true;
Lớp I91C – Minh - LTN 31
}
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;
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
}
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;
}
n. MenuStrip
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
//string ThuMuc;
timer1.Enabled = false;
button3.Text = "Chạy\r\ntự động";
listBox1.Items.Clear();
pictureBox1.Image = null;
label2.Text = "";
timer1.Interval = 500;
namespace XucXac
{
public partial class Form1 : Form
{
int So, Doan,LanThang,LanThua, LanDoan;
public Form1()
{
InitializeComponent();
}
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();
}
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);
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
}
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; }
}
}
}
}
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);
7) XỬ LÝ CHUỔI:
namespace TruyXuatFile
{
public partial class Form1 : Form
Lớp I91C – Minh - LTN 51
{
string filename;
string strLine;
string strAll;
public Form1()
{
InitializeComponent();
}
strAll = richTextBox1.Text;
sw.Write(strAll);
sw.Close();
aFile.Close();
}
strAll = sr.ReadToEnd();
richTextBox1.Text = strAll;
sr.Close();
aFile.Close();
}
}
}
namespace NgayThangNam
{
public partial class Form1 : Form
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");
}
namespace NguyenToChinhPhuong
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
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";
}
}
}
}
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 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;
}
}
}
}
13) LẤY KÝ TỰ
if (i < textBox1.Text.Length)
{
e.KeyChar=Convert.ToChar(textBox1.Text.Substring(i,1));
}
else
e.KeyChar=Convert.ToChar(Keys.None);
}
}
}
STT Trang