Lập trình sự kiện và giới thiệu chung về Microsoft Visual Studio 2005 (C#)
Trang 1Bài 1: Lập trình sự kiện và giới thiệu chung về
Microsoft Visual Studio 2005 (C#)
- _ Giới thiệu về lập trình hướng sự kiện
- _ Giới thiệu một số sự kiện cơ bản: Form_Load, Button_ Click, Key_Press,
I Giới thiệu chung về Control trên windows
- Label
- Button
- Textbox
- Listbox
- Combobox
1 Bài tập 1: Viết chương trình tính tổng hai số
O Khởi động Microsoft Visual Studio 2005
& Microsoft Visual Stud ie Microsoft Office >
(f\ Microsoft 5QL 5erver ›
Rational Rose Enterp Edition (f\ Microsoft 5QL 5erver 2005 >
(@) Microsoft SQL Server - Switch >
(\ Microsoft Visual Studio 6.0 >
ñllPrograms *
(\ Mozilla Firefox > Microsoft ¥isual Studio 2005
\W £?) Notepad++ >
O Tao project: Tr menu File chon New\Project
Trang 2New Project
Project types: Templates: 3
+i- Visual| Basi Visual Studio installed templates
= Visual C#
Windows Eater tele (Class Library
4) Smart Device Windows Control Library 3) Web Control Library
Starter Kits (ca) Empty Project FAlcrystal Reports Application
4) Visual C++
4) Other Project Types My Templates
4 ]Search Online Templates
4 project For creating an application with a Windows user interface | Name: | WindowsApplication1 |
Solution Name: | WindowsApplication1 | Create directory For solution
[_] Add to Source Control
[ OK | | Cancel |
Sau khi tạo project xong:
Bailap1_CSharp - Microsoft Visual Studio
File Edit View Project Build Debug Data Tools Window Community Help
ot | SSI | Tt oh ult | 3 | i: | ofa 2s 00 gy es | Lob) [|| ch | Ed | 2s
Toolbox ~ x _ Form1.cs [Design]* - 5tart Page ~ XX | Ge an gy + Developer Express: © Form System Windows.Forms.Form + | 3
'+| Developer Express: |: : : : - E
= =SI| : Chuong trình tỉnh tong hai so gã: |2 {| E o + Developer Express: : 2 6 sd — ¡|| @E Ae 5
— All Windows Forms False wv Allš
Fe BackgroundWorker MinimizeBox True 48
Ýz?! BindingNavigator MinimumSize 0, 0 a
27 Bindings 1 Opacity 100% ra
Button RightToLeft No = CheckBox RightToLeftLayou False
8:2 CheckedListBox ShawIcon True zy
Ei ColorDialog ShowInTaskbar True
E Size ^na 22n MI jE= ComboBox
fe) ContextMenuStrip vị Determines whether a form has a
2 Bk Server Explorer |32 Toolbox maximize box in the upper-right corner
O Thiết kế giao diện như sau (trên thanh công cụ Toolbox kéo thả các control vào form):
Trang 3
#2 BaiTap1_ CSharp - Microsoft Visual Studio
File Edit View Project Build Debug Data Format Tools Window Community Help
ởI x š¡] x ¿ÿ li 8 x 3aA%5 2Ø2-%- -Ì ỳ Debug > Any CPU x [4# cl
Toolbox w~i X Form1.cs [Design]* | Start Page ~ X Properties w~i X Si + Developer Express: ^ btnTinhTong System.Windows.For + | 3!
+) Developer Express: : = =a) = =
#' Developer Ew 8 4.1114]/J-847111.8:111,8/-11-8/7- E] @:: 7S 3
—' All Windows | Lahel \N (ApplicationSett ^llš
lề Pointer \ Số hạng thủ nhất (DataBindings) a [> BackgroundWorker (Name) PHUN 4# tz?' BindingNavigator Số hạng thứ hai | | AccessibleDescr a
£27 BindingSource AccessibleName ui
Tổng AccessibleRole Default =
a ; Q Tinhtong Dp Thpat —
8v CheckedListBox » Autollipsis False v 4) ColorDialog À AccessibleName
== ComboBox v \S The name thet will be reported to
“a Server Explorer |3¢* Toolbox ee ee
O Trên cửa số properties đặt lại tên (thuộc tinh name) va caption (thuộc tinh text) cho các control
thông qua thuộc tính Text
7 Với label: đặt lại thuộc tính text, không cần đặt thuộc tinh name
txtTong
đặt lại thuộc tính text như giao diện trên
O Đăng ký và xử lý sự kiện cho button Tính tổng
Trang 4
Devel “ btnTinhTong System Windows.Form: + | 5! Devel = fitter meni male Clie = : _ x @=-[A|]\ = h =
= All Wi (DataBindings) A) 6
Số hạng thủ nhất | AutoSizeChange = | s
a Bindi Số hạng thd hai BackgroundImac A
Lộ Bind Tong é | i BindingContextC : =
Chec G Tinhténg B | D- - Thoá | od
Chec oO m Oo v
Ss) Color Clien Z
PE) Cont «| Occurs when the component is clicked
2S 31
O Đăng ký và xử lý sự kiện cho button Tính tổng
_¥Formi.cs | Formi.cs [Design] | Start Page
4g BaiTap1_CSharp.frmTinhTong v | | QF
Elusing System;
using System.Collections.Generic;
using System ComponentModel;
using System.Data;
using System.Drawing;
using System Text;
using System Windows.Forms;
Eo namespace BaiTapl CSharp
{
public partial class frmTinhTong : Form
{
= public frmTinhTong(}
{ InitializeComponent {);
a private void btnTinhTong Click(object sender, EventArgs e)
{ int Tong = Int32.Parse(txtSohang1.Text) + Int32.Parse(txtSohang2.Text); txtTong.Text = Tong ToString();
O Nếu người dùng nhập dữ liệu không hợp lệ?
O Cần phải kiểm tra ràng buộc dữ liệu hợp lệ trước khi tính tổng
Trang 5private void btnTinhTong Click(object sender, EventArgs e)
{
try {
int Tong = Int32.Parse(txtSohangl.Text) + Int32.Parse(txtSohang2.Text);
txtTong.Text = Tong To5tr ing [| ;
}catch(Exception ex)
{
MessageBox Show("Lai: " + ex Message, "Thong bao loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
O Đăng ký và xử lý sự kiện cho button Thoát
private void btnThoat Click(object sender, EventArgs e)
{
Clnse []z
}
2 Bài tập 2: Thiết kế giao điện và cài đặt chương trình như sau:
EÊÌ Chuong trinh May tinh don gian E]
Phép toán
Số hạng thủ nhất | © Céng -
© Chia
| Thost | [ Tinh |
3 Bài tập 3: Viết chương trình Calculator đơn giản như sau:
Trang 6E Calculator i Edit View Help
| Backspace CE E
MC 7 8 J / sqrt
MS 1 2 3 : li»