Lab thực hành mạng căn bản môn lập trình mạng căn bản 2016 Lab thực hành mạng căn bản môn lập trình mạng căn bản 2016 Lab thực hành mạng căn bản môn lập trình mạng căn bản 2016 Lab thực hành mạng căn bản môn lập trình mạng căn bản 2016 Lab thực hành mạng căn bản môn lập trình mạng căn bản 2016
Trang 1Trang 1
THỰC HÀNH TUẦN 1
I Tiếp cận môi trường lập trình Windows Forms với ngôn
ngữ C#
IDE: Microsoft Visual Studio 2010
Nội dung:
- Cách tạo 1 Project
- Thay đổi Tiêu đề cửa sổ
- Cách chạy 1 ứng dụng
- Thay đổi màu của Form
- Quy định kích thước của Form
- Vị trí của Form
- Khóa MaximizeBox của Form
Thực hành:
Đầu tiên bạn mở Visual Studio 2010 lên > Click chọn New Project
Trang 2Trang 2
Chọn Windows Forms Application > OK (Xem ảnh minh họa)
Trang 3Trang 3
Mặc định ta sẽ có được 1 cửa sổ đầu tiên là Form 1
Trang 4Trang 4
Nếu để ý bạn sẽ thấy Tiêu đề của Form hiện tại là: Form 1 Bây giờ chúng ta
sẽ thay đổi tiêu đề này thành: Ứng Dụng Đầu Tiên Để làm được điều này,
bạn Click chuột vào cửa sổ của Form 1 (để nó hiểu là mình muốn làm việc với cửa sổ này)
Bạn nhìn góc phải cửa sổ làm việc sẽ thấy cửa sổ Properties Tại
mục Text đang có tiêu đề là Form 1
Trang 5Trang 5
Bạn sữa chữ Form 1 thành Ứng Dụng Đầu Tiên
Bây giờ bạn nhấn phím F5 trên bàn phím để xem ứng dụng của mình khi chạy
sẽ như thế nào
Rõ ràng bạn thấy tiêu đề ứng dụng đã đổi thành Ứng Dụng Đầu Tiên
Tắt cửa sổ đang chạy này để quay trở lại màn hình thiết kế
Chúng ta cũng có thể thay đổi các thuộc tính của Form bằng code
Trang 6Trang 6
Trong cửa sổ Solutin Explorer, bạn Click vào nút View code để chuyển sang
cửa sổ viết code
Trong sự kiện Form Load bạn thêm code sau để thay đổi màu của Form:
Code:
this BackColor = Color Brown ;
Bạn nhấn F5 để xem kết quả
Tiếp tục bạn thêm code sau để thay đổi tiêu đề của Form
Trang 7Trang 7
Code:
this Text = "Thay doi ten tieu de form";
Bạn nhấn F5 để xem kết quả
Thêm code sau để quy định kích thước Form
Code:
this Size = new Size ( 450 , 125 );
Thêm code sau để qui định vị trí Form
Code:
this Location = new Point ( 300 , 300 );
Thêm code sau để khóa nút Maximize của Form
Code:
this MaximizeBox = false ;
Toàn bộ code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.BackColor = Color.Brown;
this.Text = " Thay doi ten tieu de form ";
this.Size = new Size(450, 125);
this.Location = new Point(300, 300);
Trang 8Trang 8
this.MaximizeBox = false;
}
}
}
II Lập trình với ngôn ngữ C# ở mức cơ bản 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
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
{
Trang 9Trang 9
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
{
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);
Trang 10Trang 10
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
//
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);
Trang 11Trang 11
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 System.Windows.Forms.TextBox s1;
private System.Windows.Forms.TextBox s2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button sumBtn;
private System.Windows.Forms.TextBox sum;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
}
}
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
Trang 12Trang 12
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:
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”
………
Nhập 9: “Chín”
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 13Trang 13
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:
public partial class Form1 Form
{
public Form1()
{
}
private void btnTinh_Click(object sender, EventArgs e)
{
string text = txtN.Text;
int n = int.Parse(text);
long s = 0
for int i = 1 i <= n; i++)
{
s = s + i;
}
txtTong.Text s.ToString();
}
// private void btnThoat_Click(object sender, EventArgs e)
// {
// Close();
}
Hướng dẫn gợi ý: Sử dụng vòng lặp từ 1 đến n
Bài tập về nhà
Trang 14Trang 14
1 Nhập vào 2 số, tìm UCLN và BCNN của 2 số đó
2 Viết chương trình giải hệ phương trình bậc 2 , với 3 hằng số và kết quả hiện thị trên Form
F(x) = ax2 + bx + c
3 Nhập vào 1 mảng 1 chiều trong TextBox, mỗi phần tử cách nhau bằng dấu “;”
Xuất ra mảng 1 chiều và tìm phần tử lớn nhất trong mảng đó (Hiển thị trên Form)