ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
Khoa Công Nghệ Phần Mềm
THỰC HÀNH C#
(Có hướng dẫn chi tiết )
I. Nội dung
1. Tiếp cận môi trường lập trình C#
- IDE: Microsoft Visual Studio 2015
2. Lập trình với ngôn ngữ C# ở mức cơ bản:
- Đọc hiểu chương trình C# ở mức cơ bản
- Nắm các kiểu dữ liệu trong C#
Kiểu dữ liệu nguyên thủy: int, long, double, char….
Kiểu dữ liệu tham chiếu: Array, List, Class …
- Nắm được các cấu trúc điều khiển trong lập trình
Cấu trúc điều khiển tuần tự
Cấu trúc điều khiển rẽ nhánh if
Cấu trúc điều khiển rẽ nhánh if… else
Cấu trúc điều khiển vòng lặp
- Nắm được các kỹ thuật xử lý trên mảng 1 chiều
Trang 1
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
Khoa Công Nghệ Phần Mềm
II. Ví dụ
1. Chương trình xuất chữ “Hello World”
2. namespace HelloWorld
3. {
4.
class Program
5.
{
6.
static void Main(string[] args)
7.
{
8.
Console.WriteLine("Hello World");
9.
}
10.
}
11. }
2. Viết chương trình nhập 2 số nguyên và tính tổng 2 số đó (Sử dụng Windows
Form Application)
Class Form 1
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string s1Text = s1.Text;
string s2Text = s2.Text;
int s1Int = int.Parse(s1Text);
int s2Int = int.Parse(s2Text);
long sumLong = 0;
sumLong = s1Int + s2Int;
sum.Text = sumLong.ToString();
}
}
}
Form1.Designer.cs
namespace WindowsFormsApplication1
{
partial class Form1
Trang 2
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
Khoa Công Nghệ Phần Mềm
{
private void InitializeComponent()
{
this.s1 = new System.Windows.Forms.TextBox();
this.s2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.sumBtn = new System.Windows.Forms.Button();
this.sum = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// s1
//
this.s1.Location = new System.Drawing.Point(176, 102);
this.s1.Multiline = true;
this.s1.Name = "s1";
this.s1.Size = new System.Drawing.Size(163, 33);
this.s1.TabIndex = 0;
//
// s2
//
this.s2.Location = new System.Drawing.Point(176, 174);
this.s2.Multiline = true;
this.s2.Name = "s2";
this.s2.Size = new System.Drawing.Size(163, 35);
this.s2.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 13F);
this.label1.Location = new System.Drawing.Point(44, 113);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(102, 22);
this.label1.TabIndex = 2;
this.label1.Text = "Số thứ nhất";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 13F);
this.label2.Location = new System.Drawing.Point(44, 187);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(91, 22);
this.label2.TabIndex = 3;
this.label2.Text = "Số thứ hai";
//
// sumBtn
//
this.sumBtn.Location = new System.Drawing.Point(365, 129);
this.sumBtn.Name = "sumBtn";
this.sumBtn.Size = new System.Drawing.Size(83, 47);
this.sumBtn.TabIndex = 4;
this.sumBtn.Text = "Tính";
this.sumBtn.UseVisualStyleBackColor = true;
this.sumBtn.Click += new System.EventHandler(this.button1_Click);
//
// sum
//
Trang 3
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
Khoa Công Nghệ Phần Mềm
this.sum.Location = new System.Drawing.Point(176, 251);
this.sum.Multiline = true;
this.sum.Name = "sum";
this.sum.Size = new System.Drawing.Size(163, 35);
this.sum.TabIndex = 5;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 13F);
this.label3.Location = new System.Drawing.Point(44, 264);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(82, 22);
this.label3.TabIndex = 6;
this.label3.Text = "Kết quả: ";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F);
this.label4.Location = new System.Drawing.Point(86, 34);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(313, 26);
this.label4.TabIndex = 7;
this.label4.Text = "TÍNH TỔNG HAI SỐ NGUYÊN";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(500, 329);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.sum);
this.Controls.Add(this.sumBtn);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.s2);
this.Controls.Add(this.s1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private
private
private
private
private
private
private
private
}
System.Windows.Forms.TextBox s1;
System.Windows.Forms.TextBox s2;
System.Windows.Forms.Label label1;
System.Windows.Forms.Label label2;
System.Windows.Forms.Button sumBtn;
System.Windows.Forms.TextBox sum;
System.Windows.Forms.Label label3;
System.Windows.Forms.Label label4;
}
Trang 4
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
Khoa Công Nghệ Phần Mềm
Bài tập có gợi ý:
Bài 1: Viết chương trình nhập vào 2 số a và b, cho biết số lớn nhất và số nhỏ nhất
trong 2 số a, b với giao diện như sau:
Hướng dẫn gợi ý: Sử dụng cấu trúc If… else.
using
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
namespace _2soab
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Trang 5
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
Khoa Công Nghệ Phần Mềm
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(textA.Text);
int b = Convert.ToInt32(textB.Text);
int max = 0;
if (a > max)
max = a;
if (b > max)
max = b;
int min = a;
if (b < min)
min = b;
textMax.Text = Convert.ToString(max);
textMin.Text = Convert.ToString(min);
}
}
}
Bài 2: Viết chương trình nhập vào 3 số a , b và c cho biết số lớn nhất và số nhỏ nhất
trong 3 số a, b, c với giao diện như sau:
Trang 6
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
using
using
using
using
using
using
using
using
using
Khoa Công Nghệ Phần Mềm
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
namespace lonbe3abc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
{
}
private
{
int
int
int
void button1_Click(object sender, EventArgs e)
a = Convert.ToInt32(textA.Text);
b = Convert.ToInt32(textB.Text);
c = Convert.ToInt32(textC.Text);
int max
if (a >
max
if (b >
max
if (c >
max
= 0;
max)
= a;
max)
= b;
max)
= c;
int min
if (b <
min
if (c <
min
= a;
min)
= b;
min)
= c;
textMax.Text = Convert.ToString(max);
textMin.Text = Convert.ToString(min);
}
}
Trang 7
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
Khoa Công Nghệ Phần Mềm
}
Bài 3: Nhập vào một số nguyên từ 0 đến 9, hiển thị bằng chữ với các số trên.
Ví dụ:
-
Nhập 1: “Một”
-
Nhập 2: “Hai”
-
Nhập 3: “Ba”
………………
-
using
using
using
using
using
using
using
using
using
Nhập 9: “Chín”
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
namespace bai03
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
Trang 8
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
Khoa Công Nghệ Phần Mềm
}
private void button1_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(textnhap.Text);
switch(a)
{
case 1:
richTextdoc.Text="mot";
break;
case 2:
richTextdoc.Text="hai";
break;
case 3:
richTextdoc.Text="ba";
break;
case 4:
richTextdoc.Text="bon";
break;
case 5:
richTextdoc.Text="nam";
break;
case 6:
richTextdoc.Text="sau";
break;
case 7:
richTextdoc.Text="bay";
break;
case 8:
richTextdoc.Text="tam";
break;
case 9:
richTextdoc.Text="chim";
break;
default : richTextdoc.Text="nhap sai";
break;
}
}
}
}
Hướng dẫn gợi ý: Sử dụng cấu trúc switch…. case
Bài 4: Viết chương trình gồm 1 form trong đó có một button và 1 label hiển thị số lầ
khi nhấn button đó.
Trang 9
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
using
using
using
using
using
using
using
using
using
Khoa Công Nghệ Phần Mềm
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
namespace bai04
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int dem = 0;
private void button1_Click(object sender, EventArgs e)
{
dem++;
doc.Text = "bạn đã click : " + dem;
}
}
}
Bài 5: Viết chương trình nhập vào giá trị nguyên dương N, tính tổng
S = 1 + 2 + 3 + 4 + ……. + N
Với giao diện như sau:
using
using
using
using
using
using
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
Trang 10
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
Khoa Công Nghệ Phần Mềm
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace bai05
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private
{
int
int
for
{
void button1_Click(object sender, EventArgs e)
s = 0, i ;
a = Convert.ToInt32(textnhap.Text);
(i = 0; i <=a; i++)
s = s + i;
}
textketqua.Text = Convert.ToString(s);
}
}
}
Hướng dẫn gợi ý: Sử dụng vòng lặp từ 1 đến n
Bài 6: Viết chương trình nhập x, n và tính tổng:
S(x,n) = x1 + x2 + x3 + x4 + …… + xn
Trang 11
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
using
using
using
using
using
using
using
using
using
Khoa Công Nghệ Phần Mềm
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
namespace btvn_01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// tao them de quy luy thua
int mulythua(int x,int n)
{
if(n==0) return 1;
return x*mulythua(x,n-1);
}
//tong
private
{
int
int
int
for
{
void button1_Click(object sender, EventArgs e)
s = 0;
n = Convert.ToInt32(textN.Text);
x = Convert.ToInt32(textX.Text);
(int i = 1; i <= n; i++)
mulythua(x,i);
s=s+mulythua(x,i);
Trang 12
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
Khoa Công Nghệ Phần Mềm
}
textSum.Text = Convert.ToString(s);
}
}
}
Bài 7: Viết chương trình giải hệ phương trình bậc 2
F(x) = ax2 + bx + c
Trang 13
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
using
using
using
using
using
using
using
using
using
Khoa Công Nghệ Phần Mềm
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
namespace btvn_02
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double x;
double x1, x2, delta;
double d;
Trang 14
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
Khoa Công Nghệ Phần Mềm
private void button1_Click(object sender, EventArgs e)
{
double a
double b
double c
if (a ==
{
= Convert.ToInt32(textA.Text);
= Convert.ToInt32(textB.Text);
= Convert.ToInt32(textC.Text);
0)
richTextNghiem.Text = "Phuong trinh vo nghiem";
}
else
{
delta = (b * b - 4 * a * c);
d = System.Math.Sqrt(delta);
if (d == 0)
{
x = -c / a;
richTextNghiem.Text = "Phuong co 1 nghiem x=-(c/a) " + x;
}
else if (d > 0)
{
x1 = (-b + d) / (2 * a);
x2 = (-b - d) / (2 * a);
richTextNghiem.Text = "Phuong trinh co 2 nghiem X1 va X2 :";
textX1.Text = Convert.ToString(x1);
textX2.Text = Convert.ToString(x2);
}
else
richTextNghiem.Text = "Phuong trinh vo so nghiem";
}
}
private void label3_Click(object sender, EventArgs e)
{
}
}
}
Trang 15
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
Khoa Công Nghệ Phần Mềm
Trang 16
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
Khoa Công Nghệ Phần Mềm
ề bài 1: Sử dụng comboBox để lưu trữ 1 danh sách các loài hoa. Khi chọn
tên 1 loài hoa bất kì sẽ hiển thị thông tin về hoa như: tên, màu sắc, và mô tả về hoa,
cộng bức ảnh của loài hoa đó.
Yêu cầu: Sử dụng pictureBox hiển thị ảnh, ImageListBox lưu trữ ảnh, GroupBox
quản lý thông tin về hoa. Khi chọn hoa thì các thuộc tính mô tả về hoa được ẩn đi
Mục đích: Nắm rõ cách sử dụng comboBox, pictureBox, ImageListBox. Thuộc tính
của GroupBox
Link Demo 1
Đề bài 2: Sử dụng TreeView hiển thị danh sách các phần mềm Antivirut theo dạng
"Cây" thư mục. Listview hiển thị thông tin chi tiết về sản phẩm khi được chọn ở bên
TreeView.
1 NumberupDown Control dùng để chọn số lượng sản phẩm muốn mua, 1
DoimanupDown Control để lựa chọn thời gian sử dụng sản phẩm.
Yêu cầu: Khi chọn sản phẩm muốn mua bên ListView thì mới cho khách hàng tiếp
tục chọn số lượng sản phẩm và thời gian sử dụng, TextBox hiển thị giá tiền khi chọn
thời gian sử dụng và số lượng sản phẩm muốn mua. Đưa ra thông tin chi tiết về sản
phẩm mà khách hàng chọn mua khi nhấn button "Submit".
Mục đích: Nắm rõ cách sử dụng TreeView, ListView các sự kiện của TreeView,
ListView. Các thuộc tính của TextBox,
Link Demo 2
Đề bài 3: Giả lập 1 chuơng trình gửi email đơn giản.
Yêu cầu: Sử dụng richTextBox để hiển thị nội dung nhập email. Các button chứa các
thuộc tính text: in đậm, in nghiêng, gạch chân. Các button chứa kiểu định dạng văn
bản: canh trái, canh phải, canh giữa.
Mục đích: Nắm rõ cách sử dụng thuộc tính FontStyle, sự kiện của RickTextBox.
Link Demo 3
Đề bài 4: Giả lập 1 ứng dụng bán hàng đơn giản trong đó bạn là nhân viên bán
hàng.
Yêu cầu: Sử dụng các textBox để nhập thông tin về mã sản phẩm, 1 textBox hiển thị
mã của sản phẩm.1 DateTimePicker để hiển thị ngày làm hóa đơn. 1 texbox nhập
tên khách hàng. 1 groupBox chứa các thông tin về tên sản phẩm, đơn giá, và số
Trang 17
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
Khoa Công Nghệ Phần Mềm
lượng hàng khách mua. 1 button tính tổng tiền khi khách hàng nhấn nút Submit.1
button Report sẽ báo cáo dữ liệu vừa nhập sang 1 form mới.Form mới cài đặt 1
MonthCalendar để hiển thị ngày tháng. Khi chọn tháng(hoặc ngày) khác thì sẽ hiển
thị hàng đã bán trong tháng (ngày đó).
Mục đích: Nắm rõ các điều khiển Control, method của form, sự kiện của textBox, sự
kiện của MonthCalendar
Link Demo 4
Đề bài 5: Giả lập 1 ứng dụng nhập dữ liệu đơn giản. Trong đó bạn có 1 ô nhập liệu,
các button cho phép thao tác các nhiệm vụ tuơng ứng: mở file, tạo file mới, lưu file,
chỉnh font cho chữ, chỉnh màu sắc.
Yêu cầu: sử dụng openFileDilog, SaveFileDilog ...để thực hiện yêu cầu.
Mục đích: Nắm rõ các thuộc tính, phuơng thức của openFileDilog, SaveFileDilog,
FontFileDilog, ColorFileDilog, PrintFileDilog, và các sự kiện của chúng.
Link Demo 5
Đề bài 6: Phát triển từ bài tập số 5 bạn hãy phát triển ứng dụng thành 1 chương
trình cho phép người sử dụng nhập dữ liệu vào và lưu lại nội dung dữ liệu thành 1
file có đuôi do người sử dụng tự định nghĩa. Ví dụ file có tên là "vn-zoom.cShap".
Chương trình sẽ cho phép mở file bạn vừa tạo
Mục đích yêu cầu như ví dụ 5.
Link Demo 6
Đề 7: Xây dựng form(form1) nhập liệu cơ bản gồm tên tuổi địa chỉ, 1 button Add gọi
1 form khác(form2). Form đc gọi thêm này sử dụng 1 combobox liệt kê danh sách
hàng hóa, 1 textBox hiển thị đơn giá của sản phẩm, 1textBox nhập số liệu hàng
muốn mua, 1 textBox tính tiền. 1 button Ok. Khi người nhập liệu click vào buttonOK
thì trả về form 1: Tổng tiền, liệt kê chi tiết hàng hóa trong 1 listView. Nếu tiếp tục
nhấn Addnew thì lại tiến hành mua thêm hàng. Tiếp tục tính tiền lại cho khách và liệt
kê thêm danh sách sản phẩm khách hàng chọn mua
Link Demo 7
Đề bài 8: Thực hiện việc Thao tác cơ bản với cơ sở dữ liệu. Thêm, Sửa, Xóa 1
trường Trong cơ sở dữ liệu.
Yêu cầu + Mục đích: như trên
Link Demo
Trang 18
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
Khoa Công Nghệ Phần Mềm
Đề bài 9: Sử dụng DataAdapter và DataSet thực hiện việc thao tác với cơ sở dữ
liệu. Hiển thị dữ liệu trên form, tìm kiếm dữ liệu...
Link Down
Đề bài 10: Tạo 1 ứng dụng đơn giản để quản lý sinh viên.
Giao diện được thiết kế như hình
Các bạn thiết kế 1 ComboBox để hiển thị tên lớp, 1 textBox hiển thị tên giáo viên chủ
nhiệm, 1 textBox hiển thị số sinh viên có trong lớp.. 1 listBox hiển thị thông tin chi tiết
về sinh viên bao gồm tên, ngày sinh, địa chỉ.
Thiết kế cơ sở dữ liệu như sau.
Bảng Student
Trang 19
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
Khoa Công Nghệ Phần Mềm
Bảng StudentClass
Trang 20
- Xem thêm -