2. Mục tiêu
1. Common Dialog Controls, Menus và Toolbar, Context
Menus
2. ListView, TreeView, DataGridView.
3. Các thuộc tính và phương thức
HBU
QLDA PM
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
2
3. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Điều khiển nâng cao
Common Dialog Controls
Menus và Toolbar
Context Menus
HBU
Common Dialog 1
LTGD
3
4. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Common Dialog Controls
MessageBox
OpenFileDialog
SaveFileDialog
FontDialog
ColorDialog
User Control
HBU
Common Dialog 1
LTGD
4
5. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
MessageBox
A pop-up dialog which displays a message
Dùng lớp MessageBox, phương thức tĩnh Show để
hiển thị dialog
Các thành phần
HBU
Common Dialog 1
LTGD
5
6. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
MessageBox
Các Phương thức gọi: text and OK button
HBU
Common Dialog 1
LTGD
MessageBox.Show(string) – box with simple string and OK button
https://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.show(v=vs.110).aspx
MessageBox.Show("vui lòng nhập tên");
6
7. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
MessageBox
Các Phương thức gọi: text and caption and OK button
HBU
Common Dialog 1
LTGD
https://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.show(v=vs.110).aspx
MessageBox.Show(string, string)
– Displays a message box with specified text and caption and OK button
MessageBox.Show("vui lòng nhập tên","cảnh báo");
7
8. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
MessageBox
Các Phương thức gọi: text and caption and buttons
HBU
Common Dialog 1
LTGD
https://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.show(v=vs.110).aspx
MessageBox.Show(string, string, MessageBoxButtons))
– Displays a message box with specified text and caption and buttons.
MessageBox.Show("vui lòng nhập tên","cảnh báo",MessageBoxButtons.AbortRetryIgnore)
8
9. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
MessageBox
Các button hiển thị theo message
o Cho phép user chọn lựa các phản ứng với message
o Được định nghĩa trong MessageBoxButtons
HBU
Common Dialog 1
LTGD
https://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.show(v=vs.110).aspx
9
10. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
MessageBox
Các button hiển thị theo message
HBU
Common Dialog 1
LTGD
https://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.show(v=vs.110).aspx
10
11. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
MessageBox
Xử lý các sự kiện của Button trên MessageBox
HBU
Common Dialog 1
LTGD
https://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.show(v=vs.110).aspx
var result = MessageBox.Show("vui lòng nhập tên", "cảnh báo",
MessageBoxButtons.YesNo);
if (result == DialogResult.No)
{
// xử lý khi người dùng click vào nút no
}
11
12. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
MessageBox
Các Phương thức gọi: text, caption, buttons, icon
HBU
Common Dialog 1
LTGD
MessageBox.Show(string, string, MessageBoxButtons, MessageBoxIcon)
–Displays a message box with the specified text, caption, buttons, icon
https://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.show(v=vs.110).aspx
MessageBox.Show("vui lòng nhập tên", "cảnh báo",
MessageBoxButtons.OK,MessageBoxIcon.Warning);
12
13. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
MessageBox
Các icon:
HBU
Common Dialog 1
LTGD
https://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.show(v=vs.110).aspx
13
14. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
MessageBox
Các icon:
HBU
Common Dialog 1
LTGD
https://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.show(v=vs.110).aspx
14
15. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Common Dialog
HBU
Common Dialog 1
LTGD
Cho phép một ngừoi dùng chọn một màu
từ bảng màu.
Cho phép một nguời dùng mở một hộp
thoại chọn tập tin
Hộp dialog này hiển thị tất cả font hiện có
trên hệ thống, cho phép nguời dùng chọn
một font dể dùng trong
ứng dụng
Cho phép một nguời dùng lưu một tập tin
Cho phép một nguời dùng mở một tập tin
sử dụng hộp thoại mở tập tin chuẩn
15
16. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Common Dialog – Bốn bước sử dialog
Cấu hình dialog (kéo thả control và form, thiết lập các
properties)
Hiện dialog bằng phương thức ShowDialog()
Xác định người dùng nhấn OK hay Apply (đựa trên
kiểu trả về DialogResult)
Viết code thực hiện dựa trên hành động của user trên
dialog
HBU
Common Dialog 1
LTGD
16
17. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Common Dialog – cách sử dụng
HBU
Common Dialog 1
LTGD
Kéo thả control vào form
Properties
Control hiển thị ở khung bên dưới form
Code tự động sinh ra ở CommonDialog.Designer.cs
17
18. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Common Dialog – cách hiển thị hộp thoại chọn
Sử dụng hàm
DialogResult DialogName.ShowDialog()
Các giá trị của DialogResult
Cancel: Người dùng chọn nút Cancel trên Dialog hoặc nhấn nút
(X) trên dialog hoặc nhấn phím escape
OK: Trả về khi người dùng chọn nút Ok trên dialog
Yes/No: Khi người dùng chọn nút Yes/No trên dialog. Thường
dùng để người dùng chọn Yes/No
….
HBU
Common Dialog 1
LTGD
18
19. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Common Dialog – cách hiển thị hộp thoại chọn
HBU
Common Dialog 1
LTGD
if(DialogName.ShowDialog()=DialogResult .OK){
// xu ly code
}
Else {
// xu ly code
}
19
20. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
OpenFileDialog
Sử dụng để chọn file lưu trên đĩa
Có thể kéo thả từ Toolbox hoặc viết code
HBU
Common Dialog 1
LTGD
20
21. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
OpenFileDialog - Thuộc tính, phương thức
Thuộc tính
FileName: Lấy tên file được chọn
FileNames: Lấy tên tất cả các file được chọn
Filter: Xác định kiểu file cần mở
InitialDirectory: Thư mục khởi tạo
Multiselect: Cho phép chọn nhiều file
Title: Tiêu đề của dialog
Phương thức
ShowDialog: Hiển thị dialog
Sự kiện
FileOk: Xuất hiện khi user click vào OK HBU
Common Dialog 1
LTGD
21
22. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
OpenFileDialog - Demo
HBU
Common Dialog 1
LTGD
22
23. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
FolderBrowserDialog
HBU
Common Dialog 1
LTGD
23
24. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
FolderBrowserDialog
Sử dụng để mở hộp thoại chọn thư mục
Các thuộc tính và phương thức
HBU
Common Dialog 1
LTGD
24
25. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
FolderBrowserDialog - demo
HBU
Common Dialog 1
LTGD
25
26. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
SaveFileDialog
Sử dụng khi muốn lưu thông tin xuống file
Thuộc tính
FileName: Lấy tên file được chọn
Filter: Xác định kiểu file cần mở
InitialDirectory: Thư mục khởi tạo
Title: Tiêu đề của dialog
HBU
Common Dialog 1
LTGD
26
27. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
SaveFileDialog
Methods:
HBU
Common Dialog 1
LTGD
27
28. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
SaveFileDialog - demo
HBU
Common Dialog 1
LTGD
28
29. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
FontDialog
Chức năng hiển thị hộp thoại chọn font chữ được
install trong máy sử dụng khi ứng dụng làm việc với
document, đồ họa…
HBU
Common Dialog 1
LTGD
29
30. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
FontDialog – Properties
Properties
Font: Gets or sets the selected font.
Color: Gets or sets the selected font color
MaxSize, MinSize: Gets or sets the maximum/minimum point
size a user can select
ShowColor: bool, Gets or sets a value indicating whether the
dialog box displays the color choice
ShowApply: bool, Gets or sets a value indicating whether the
dialog box contains an Apply button
ShowEffects: bool, Gets or sets a value indicating whether
the dialog box contains controls that allow the user to specify
strikethrough, underline, and text color options HBU
Common Dialog 1
LTGD
30
31. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
FontDialog – Methods
Methods
DialogResult ShowDialog(): Runs a common dialog box with
a default owner
void Reset(): Resets all dialog box options to their default
values
Events
HBU
Common Dialog 1
LTGD
31
32. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
FontDialog – Demo
HBU
Common Dialog 1
LTGD
32
33. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ColorDialog
Hiển thị chức năng chọn màu
Thuộc tính:
Color: Gets or sets the color selected by the user.
CustomColors: Gets or sets the set of custom colors shown
in the dialog box.
Methods: tương tự như fontDialog
HBU
Common Dialog 1
LTGD
33
34. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ColorDialog – demo
HBU
Common Dialog 1
LTGD
34
35. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
User Control
User control phục vụ mục đích tái sử dụng
Ví dụ:
Tạo ra control Menu sử dụng cho tất cả các form trong
chương trình
Tạo control Login sử dụng cho tất cả các form
HBU
Common Dialog 1
LTGD
35
36. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
User Control – các bước tạo và sử dụng
o Tạo Project window forms control Library
o Tạo user control
o Kéo thả các control vào user control vừa tạo
o Build control, control được tạo lưu dưới file dll
o Add control vừa tạo vào thành toolbox
o Gọi control
HBU
Common Dialog 1
LTGD
36
37. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
User Control – Tạo
HBU
Common Dialog 1
LTGD
Tạo Project loại Window Forms Control Library
37
38. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
User Control – Tạo
HBU
Common Dialog 1
LTGD
R-Click vào project vừa tạo, chọn Add -> user control
38
39. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
User Control – Tạo
HBU
Common Dialog 1
LTGD
Kéo thả control vào, build
Build Project, control vừa tạo có
dạng file .dll
39
40. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
User Control – Tạo
HBU
Common Dialog 1
LTGD
R-click vào toolbox, chọn Add
Tab để thêm tab mới
40
41. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
User Control – Tạo
HBU
Common Dialog 1
LTGD
R-click vào Tab mới tạo, chọn
Choose Items để add dll mới vào
Control mới tạo
Kéo thả control vào
41
42. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Common Dialog
Các common Dialog có thể được tạo, gọi bằng code
mà không cần kéo thả vào form
Cách tạo bằng code là giống nhau với các dialog này
HBU
Common Dialog 1
LTGD
42
43. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Common Dialog – tạo bằng code
HBU
Common Dialog 1
LTGD
43
44. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Menu
Menu cho phép tổ chức các chức năng xử lý
của ứng dụng theo nhóm
Menu được tổ chức phân cấp
Menu
Sub menu
Menu item
Menu có thể được tạo bằng code hoặc
bằng cách kéo thả
HBU
Common Dialog 1
LTGD
44
45. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Menu
HBU
Common Dialog 1
LTGD
45
46. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Menu
HBU
Common Dialog 1
LTGD
46
Menu (2 ways to use)
MainMenu MenuStrip
MenuItem ToolStripMenuItem
Menu
Property
MainMenuStrip
Property
47. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Menu – tạo bằng code
HBU
Common Dialog 1
LTGD
47
private MainMenu mainMenuBar;
private MenuItem menuFile, menuEdit,
menuFileNew, menuFileOpen, menuFileExit,
menuEditCut, menuEditCopy, menuEditPaste;
48. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Menu – tạo bằng code
HBU
Common Dialog 1
LTGD
48
private void createMenu()
{
mainMenuBar = new MainMenu();
this.Menu = mainMenuBar;
menuFile=new MenuItem("File");
menuFileNew = new MenuItem("New");
menuFileOpen = new MenuItem("Open");
menuFileExit = new MenuItem("Exit");
menuFile.MenuItems.Add(menuFileNew);
menuFile.MenuItems.Add(menuFileOpen);
menuFile.MenuItems.Add("-");
menuFile.MenuItems.Add(menuFileExit);
mainMenuBar.MenuItems.Add(menuFile);
49. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Menu – tạo bằng design
Trong ToolBox kéo control MenuStrip thả
vào form
Thanh menuBar xuất hiện trên cùng của form
Trong màn hình design tạo lập các menu
item
HBU
Common Dialog 1
LTGD
49
50. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Menu – tạo bằng design
HBU
Common Dialog 1
LTGD
50
51. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Menu – tạo bằng design
HBU
Common Dialog 1
LTGD
51
52. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Menu – tạo bằng design
HBU
Common Dialog 1
LTGD
52
Click on button
to add MenuItem
53. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Menu – tạo bằng design
HBU
Common Dialog 1
LTGD
53
Click Add button to add new
MenuItem
Text to Display
Click here to add
Sub MenuItem
MenuItem’s
Name
54. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Menu – tạo bằng design
HBU
Common Dialog 1
LTGD
54
55. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Menu – tạo bằng design
• Menu item có thể là TextBox hoặc ComboBox
HBU
Common Dialog 1
LTGD
55
56. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Menu – tạo bằng design – thuộc tính
HBU
Common Dialog 1
LTGD
56
57. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Menu – tạo bằng design – sự kiện
Sự kiện cho Menu Item thường dùng là Click
HBU
Common Dialog 1
LTGD
57
58. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ToolStrip
Dạng ToolBar chứa nhiều các controls khác nhau
Xuất hiện ngay dưới thanh Menu
HBU
Common Dialog 1
LTGD
58
59. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ToolStrip
ToolStrip MenuItem là 1 Menu Item của Menu Strip
Một số thuộc tính cơ bản
• Text
• Image
• Checked
• DropDownItems
sự kiện cơ bản
• Click
HBU
Common Dialog 1
LTGD
59
60. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ToolStripContainer & ToolStrip
HBU
Common Dialog 1
LTGD
Drag & Drop
ToolStrip into
the Form
60
61. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ToolStrip
Mỗi button có thể là
o ToolStripButton
o ToolStripSplitButton
o ToolStripDropDownButton
o ToolStripComboBox
o ToolStripTextBox
o ToolStripSeperator
o ToolStripProgressBar
Thuộc tính cơ bản
o Items
HBU
Common Dialog 1
LTGD
61
62. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ToolStrip
HBU
Common Dialog 1
LTGD
62
63. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ToolStrip- thêm item vào
HBU
Common Dialog 1
LTGD
63
64. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ToolStrip- thêm item vào
HBU
Common Dialog 1
LTGD
Attach Events for
each Item
64
65. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
StatusStrip
• Hiển thị thông tin trạng thái của ứng dụng, nằm bên
dưới cùng của Form.
• Các lớp liên quan
StatusStrip: là container chứa control khác
ToolStripStatusLabel: control có thể add vào StatusStrip
HBU
Common Dialog 1
LTGD
65
66. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
StatusStrip
HBU
Common Dialog 1
LTGD
66
67. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Context Menu
Xuất hiện khi user kích chuột phải
Thông thường menu này xuất hiện tùy thuộc vào đối
tượng trong vùng kích chuột phải
HBU
Common Dialog 1
LTGD
67
68. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Context Menu
HBU
Common Dialog 1
LTGD
Kéo thả control vào form
Click vào đây để hiện vùng edi
68
69. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Context Menu: cách sử dụng
o Mỗi control đều có property là: ContextMenuStrip
Khai báo thuộc tính này với ContextMenuStrip vừa tạo
Khi đó user kích chuột phải lên control thì sẽ hiển thị
context Menu đã cài đặt sẵn
o Khai báo trình xử lý sự kiện Click cho ContextMenu
– Nhấp đôi vào menu item của Context Menu để tạo
– Hoặc trong cửa sổ Properties -> Event kích đúp vào
sự kiện Click
HBU
Common Dialog 1
LTGD
69
70. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Context Menu: cách sử dụng
HBU
Common Dialog 1
LTGD
Thuộc tính của textbox
70
71. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Context Menu: cách sử dụng sự kiện
HBU
Common Dialog 1
LTGD
Thuộc tính của ContextMenu
Sự kiện click
71
72. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Điều khiển danh sách
ListView
TreeView
DataGridView
HBU
Điều khiển danh sách 2
LTGD
72
73. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ListView
Dạng control phổ biến hiện thị một danh sách
item.
Các item có thể có các item con gọi là
subitem
Ví dụ: Windows Explorer
HBU
Điều khiển danh sách 2
LTGD
73
74. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ListView
Có thể hiển thị thông tin theo nhiều dạng
thông qua thuộc tính View
• Xem dạng chi tiết thông tin
• Xem dạng icon nhỏ
• Xem dạng icon lớn
• Xem dạng tóm tắt
HBU
Điều khiển danh sách 2
LTGD
74
75. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ListView - Thuộc tính
Columns: trả về các cột trong listview
Items: trả về 1 item
MultiSelect: chọn nhiều item trong listview
SelectedItems: trả về tập các items được chọn
Count: Đếm số item có trong listview
AllowColumnRecorder: cho phép người dùng kéo thả
để thay đổi thứ tự của cột
LargeImageList: gán 1 ImageList cho Listview
HBU
Điều khiển danh sách 2
LTGD
75
76. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ListView - Phương thức
ArrageIcons: sắp xếp các icon trong control
Clear: xóa các item và column từ listview
GetItemAt: trả về item ở vị trí xác định
Sort: Sắp xếp các item trong listview
HBU
Điều khiển danh sách 2
LTGD
76
77. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ListView - Sự kiện
ColumnClick: Sự kiện xảy ra khi click vào 1 cột
ItemCheck: Xảy ra khi thay đổi trạng thái chọn
của Item
ItemSelectionChanged: Xảy ra khi chọn 1 item
trên Listview
SelectedIndexChanged: xảy ra khi chọn 1 item
trên listview
HBU
Điều khiển danh sách 2
LTGD
77
78. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ListView – Tạo
Kéo thả control vào form
Các items có thể tạo bằng code
Có thể thêm item bằng view Design
HBU
Điều khiển danh sách 2
LTGD
78
79. CHƯƠNG 1: GIỚI THIỆU VS STUDIO
ListView –
HBU
Điều khiển danh sách 2
LTGD
79
80. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ListView – tạo items bằng code
HBU
Điều khiển danh sách 2
LTGD
80
81. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ListView – tạo
HBU
Điều khiển danh sách 2
LTGD
81
82. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ListView – tạo
HBU
Điều khiển danh sách 2
LTGD
Dùng Image List để hiển thị hình ảnh
82
83. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ListView – tạo
HBU
Điều khiển danh sách 2
LTGD
Dùng thuộc tính checked ở property của Listview để hiển thị checkbox
Dùng thuộc tính checked ở property của item để hiển thị item này được check
83
84. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ListView – tạo items bằng code
Các lớp định nghĩa Item
– System.Windows.Forms.ListViewItem
Mỗi item trong ListView có các item phụ gọi là
subitem
o Lớp ListViewItem.ListViewSubItem định nghĩa các
subitem của ListView
o Lớp ListViewSubItem là inner class của ListViewItem
HBU
Điều khiển danh sách 2
LTGD
84
85. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ListView – tạo items bằng code
HBU
Điều khiển danh sách 2
LTGD
ListViewItem item = new ListViewItem("143098987");
ListViewItem.ListViewSubItem sub1 = new ListViewItem.ListViewSubItem();
sub1.Text = "Nguyen Van B";
ListViewItem.ListViewSubItem sub2 = new ListViewItem.ListViewSubItem();
sub2.Text = "CNTT";
item.SubItems.Add(sub1);
item.SubItems.Add(sub2);
item.ImageIndex = 1;
lstViewSV.Items.Add(item);
85
86. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ListView – chọn 1 item trên listView
HBU
Điều khiển danh sách 2
LTGD
private void lstViewSV_SelectedIndexChanged(object sender, EventArgs e)
{
ListView.SelectedListViewItemCollection item;
item = lstViewSV.SelectedItems;
if (item.Count > 0) // neu co item duoc chon
{
string itemText = item[0].Text;
string itemSubText = item[0].SubItems[0].Text;
MessageBox.Show(itemText + "-" + itemSubText);
}
}
86
87. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ListView – Xóa Item
HBU
Điều khiển danh sách 2
LTGD
//cach 1
lstView1.Items.RemoveAt(1); // vi tri thu 1
// cach 2
ListViewItem v = lstView1.Items[5];
lstView1.Items.Remove(v);
87
88. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
TreeView
Điều khiển TreeView dùng để trình bày danh
sách phần tử phân cấp theo từng nút (Node)
hình cây
HBU
Điều khiển danh sách 2
LTGD
88
89. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
TreeView
Hiển thị các dữ liệu một cách có hệ thống hơn,
rõ ràng hơn
Cấu trúc cây:
Root Node: gọi là Node gốc (gốc của cây). Có một
hoặc nhiều Node con và không có Node cha.
Parent Node: là Node nằm dưới Root Node. Nó có
một hoặc nhiều Node con.
Leaf Node : là Node lá, nó không có Node con nào.
HBU
Điều khiển danh sách 2
LTGD
89
90. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
TreeView -Thuộc tính
Nodes: Trả về 1 đối tượng thuộc lớp TreeNode
SelectedNode: Trả về Node đang được click
trong TreeView
ShowPlusMinus: Hiển thị dấu cộng, trừ
ShowRootLines: Hiển thị đường nối giữa các
Node
ImageIndex: Vị trí ảnh trong ImageList
HBU
Điều khiển danh sách 2
LTGD
90
91. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
TreeView –Phương thức
GetNodeCount: đếm số node trong một cây
ExpandAll: mở các node hiện tại
CollapseAll: Co các node lại
GetNodeAt: lấy thông tin của 1 Node tại vị trí nào
đó
HBU
Điều khiển danh sách 2
LTGD
91
92. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
TreeView –Sự kiện
AfterCheck: Xảy ra khi người dùng Check vào CheckBox
AfterCollapse: Xảy ra khi thu gọn tất cả các Node
AfterExpand: Xảy ra khi mở rộng tất cả các Node
AfterSelect: Xảy ra khi Click vào Node
Tương tự đối vối BeforeCheck, BeforeCollapse,
BeforeExpand, BeforeSelect
HBU
Điều khiển danh sách 2
LTGD
92
93. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Node –Thuộc tính của Node trong TreeView
Nodes: trả về tập các node
Text: đọc/gán chuỗi kỹ tự mở node
FirstNode: trả về node đầu
NextNode: chuyển đến node kế
LastNode: Node cuối
PrevNode: Lùi lại Node trước
Parent: Node cha của node hiện tại
Index: Trả về Index của node HBU
Điều khiển danh sách 2
LTGD
93
94. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Node –phương thức của Node trong TreeView
HBU
Điều khiển danh sách 2
LTGD
94
95. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
TreeView – Tạo
Kéo thả control vào form
Tạo node trên cây:
Bằng Design View
Bằng code
HBU
Điều khiển danh sách 2
LTGD
95
96. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
TreeView – Tạo
HBU
Điều khiển danh sách 2
LTGD
96
97. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
TreeView – Tạo
HBU
Điều khiển danh sách 2
LTGD
97
98. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
TreeView – Tạo
HBU
Điều khiển danh sách 2
LTGD
98
99. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
TreeView – Tạo
HBU
Điều khiển danh sách 2
LTGD
Dùng thuộc tính checked ở property của treeview để hiển thị checkbox
Dùng thuộc tính checked ở property của node để hiển thị node này được check
99
100. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
TreeView – Tạo node bằng code
o
HBU
Điều khiển danh sách 2
LTGD
100
101. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
TreeView – Lấy thông tin node
o
HBU
Điều khiển danh sách 2
LTGD
TreeNode Nodes = trViewCompany.Nodes[0].Nodes[0];
string strNodeText = "";
while (Nodes != null) {
strNodeText += Nodes.Text + ";";
Nodes = Nodes.NextNode;
}
MessageBox.Show(strNodeText);
101
102. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
TreeView – Click trên Node
o
HBU
Điều khiển danh sách 2
LTGD
private void trViewCompany_NodeMouseClick_1(object
sender, TreeNodeMouseClickEventArgs e)
{
TreeNode Nodes = e.Node;
if (Nodes != null)
{
MessageBox.Show(Nodes.Text);
}
}
102
103. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
TreeView – Click và xóa trên Node
o
HBU
Điều khiển danh sách 2
LTGD
private void trViewCompany_NodeMouseClick_1(object
sender, TreeNodeMouseClickEventArgs e)
{
TreeNode Nodes = e.Node;
if (Nodes != null)
{
MessageBox.Show(Nodes.Text);
}
}
103
104. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
DataGridView
o DataGridView là một điều khiển chuyên dùng cho
việc hiển thị dữ liệu dưới dạng bảng
o Có thể biểu diễn dữ liệu trên bảng trong Textbox,
CheckBox hoặc DropDownList.
o Sử dụng DataSource để kết nối với bất kì cơ sở dữ
liệu nào, với bất kì bảng nào.
o Tăng tốc độ xử lý bằng việc lưu dữ liệu trong bộ
nhớ cache
HBU
Điều khiển danh sách 2
LTGD
104
105. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
DataGridView - các sự kiện
CellClick: Khi người dùng click vào bất kỳ phần nào của một
ô
CellDoubleClick: Khi người dùng double click vào 1 ô
CellValueChanged: Xảy ra khi nội dung của 1 ô bị thay đổi
UserAddedRow: Xảy ra khi người dùng thêm 1 dòng
UserDeleteRow: Xảy ra khi người dùng xóa một dòng
HBU
Điều khiển danh sách 2
LTGD
105
106. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
DataGridView - Các phương thức
AutoResizeColumns: tự động chỉnh độ rộng của cột phù hợp
với nội dung
AutoResizeRows: Tự động chỉnh độ rộng của dòng
Sort: Sắp xếp nội dung của DataGridView theo thứ tự
SelectAll: chọn tất cả các ô trong DataGridView
ClearSelection: Bỏ chọn các ô đã chọn
HBU
Điều khiển danh sách 2
LTGD
106
107. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
DataGridView - Các thuộc tính
Item: dùng để gán/lấy thông tin của 1 ô
MultiSelect: Xác định người dùng 1 hay nhiều ô/hàng
Sort: Sắp xếp nội dung của DataGridView theo thứ tự
DataMember: Tên của bảng, danh sách … hiển thị dữ liệu
trên DataGridView
DataSource: Là nguồn dữ liệu: dataset, datatable,
dataview…
Columns: Trả về collection chứa các cột trong bảng
CurrentRow: Trả về hàng đang xử lý
HBU
Điều khiển danh sách 2
LTGD
107
108. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
DataGridView - Các thuộc tính
Item: dùng để gán/lấy thông tin của 1 ô
MultiSelect: Xác định người dùng 1 hay nhiều ô/hàng
Sort: Sắp xếp nội dung của DataGridView theo thứ tự
DataMember: Tên của bảng, danh sách … hiển thị dữ liệu
trên DataGridView
DataSource: Là nguồn dữ liệu: dataset, datatable,
dataview…
Columns: Trả về collection chứa các cột trong bảng
CurrentRow: Trả về hàng đang xử lý
HBU
Điều khiển danh sách 2
LTGD
108
109. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
DataGridView – Tạo
HBU
Điều khiển danh sách 2
LTGD
109
110. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Các thuộc tính và phương thức
Tab Order
Sự kiện bàn phím
Sự kiện Mouse
HBU
Các thuộc tính và phương thức 3
LTGD
110
111. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Tab Order
View -> tab order
Click lên số để thay đổi
TabStop property set về false/true
HBU
Các thuộc tính và phương thức 3
LTGD
Click vào số để thay đổi 111
112. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Sự kiện bàn phím
Khi nhấn phím có thể xảy ra trường hợp
– Nhấn 1 phím ký tự
– Nhấn 1 phím điều khiển (Enter, Esc, F1 F12)
– Nhấn Shift hoặc Ctrl hoặc Alt hoặc tổ hợp nào đó của
3 phím này với các ký tự.
Khi một phím được gõ, nhả hay giữ thì các thông điệp
sẽ được gửi đến cửa sổ đang được focus
HBU
Các thuộc tính và phương thức 3
LTGD
112
113. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Sự kiện bàn phím
Toggle keys: Caps Lock, Num Lock, Scroll
Lock
Shift keys: Shift, Ctrl, Alt
Noncharacter keys: các phím chức năng như
các phím di chuyển, Pause, Delete
Character keys: các phím ký tự, phím số,…
HBU
Các thuộc tính và phương thức 3
LTGD
113
114. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Sự kiện bàn phím: Có 3 sự kiện
KeyPress: KeyPress phát sinh kèm theo với
mã ASCII của phím được nhấn
KeyUp
KeyDown
KeyPress không cho biết trạng thái các phím
bổ sung {Shift, Alt, Ctrl…}
Sử dụng KeyUp & KeyDown để xác định
trạng thái các phím bổ sung
HBU
Các thuộc tính và phương thức 3
LTGD
114
115. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Sự kiện bàn phím:
KeyDown Phát sinh khi phím được nhấn
KeyUp Phát sinh khi phím được thả
HBU
Các thuộc tính và phương thức 3
LTGD
tham số kiểu KeyEventArgs
115
116. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Sự kiện bàn phím: Thuộc tính của lớp KeyEventArgs
Alt, Control, Shift Trạng thái các phím bổ sung
Handled Cho biết sự kiện đã xử lý
KeyCode Trả về mã ký tự được định nghĩa trong Key enumeration
KeyData Chứa mã ký tự với thông tin phím bổ sung
KeyValue Trả về số int, đây chính là mã Windows Virtual Key Code
Modifier Trả về giá trị của phím bổ sung
HBU
Các thuộc tính và phương thức 3
LTGD
116
117. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Sự kiện bàn phím: Key enumeration
HBU
Các thuộc tính và phương thức 3
LTGD
…
117
118. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Sự kiện bàn phím: Key enumeration
HBU
Các thuộc tính và phương thức 3
LTGD
118
119. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Sự kiện bàn phím: Key enumeration
HBU
Các thuộc tính và phương thức 3
LTGD
119
120. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Sự kiện bàn phím:
KeyPress Khởi tạo khi phím được nhấn
Thuộc tính của lớp KeyPressEventArgs
KeyChar Chứa ký tự ASCII của phím được nhấn
Handled Cho biết sự kiện KeyPress có được xử lý
chưa
HBU
Các thuộc tính và phương thức 3
LTGD
tham số kiểu KeyPressEventArgs
120
121. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Sự kiện bàn phím: Demo
HBU
Các thuộc tính và phương thức 3
LTGD
121
122. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Sự kiện Mouse:
Có hai loại sự kiện về chuột:
– Client area messages: Các sự kiện chuột xảy ra
khi chuột đang ở vùng client của cửa sổ.
– Non-client area messages: Các sự kiện chuột xảy
ra khi chuột đang ở các vùng như border, menu bar,
title bar, scroll bar, window menu, minimize
button,và maximize button
HBU
Các thuộc tính và phương thức 3
LTGD
122
123. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Sự kiện Mouse:
Các thao tác phát sinh từ mouse
– Di chuyển
– Kích chuột
Lớp MouseEventArgs được sử dụng để chứa
thông tin truyền vào cho trình xử lý sự kiện
mouse.
Mỗi trình xử lý sự kiện sẽ có tham số là đối
tượng object và đối tượng MouseEventArgs
HBU
Các thuộc tính và phương thức 3
LTGD
123
124. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Tham số sự kiện liên quan đến mouse
Số lần kích chuột
Tọa độ (x,y) của con trỏ chuột
Button được nhấn
Các sự kiện
MouseEnter: Xuất hiện khi con trỏ chuột đi vào vùng
biên của control
MouseLeave: Xuất hiện khi con trỏ chuột rời khỏi biên
của control
Click: xuất hiện khi người dùng click chuột
HBU
Các thuộc tính và phương thức 3
LTGD
tham số kiểu EventArgs
124
125. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Các sự kiện
MouseDown/ MouseUp: Xuất hiện khi button được
nhấn/thả và con trỏ chuột đang ở trong vùng biên của
control
MouseMove: Xuất hiện khi chuột di chuyển và con trỏ
chuột ở trong vùng biên của control
MouseWheel: xuất hiện khi người dùng scroll chuột
HBU
Các thuộc tính và phương thức 3
LTGD
tham số kiểu MouseEventArgs
125
126. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Thuộc tính của lớp
Button: Button được nhấn {Left, Right, Middle, none}
có kiểu là MouseButtons
Clicks: Số lần button được nhấn
X: Tọa độ x của con trỏ chuột trong control
Y: Tọa độ y của con trỏ chuột trong control
HBU
Các thuộc tính và phương thức 3
LTGD
MouseEventArgs
126
127. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Các control khác
Tooltip
ProgressBar
TrackBar & Scrollbar
Timer
DateTimePicker & MonthCalendar
CheckedListBox
HBU
Các control khác 4
LTGD
127
128. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Tooltip
Cung cấp chức năng hiển thị một khung text nhỏ khi
user di chuyển chuột vào control
HBU
Các control khác 4
LTGD
Property của textbox
Nhập text vào đây
128
129. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Tooltip
Cung cấp chức năng hiển thị một khung text nhỏ khi
user di chuyển chuột vào control
HBU
Các control khác 4
LTGD
txtToolTip
129
130. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ProgressBar
Hiển thị tiến độ thực hiện của một công việc
Các thuộc tính
– Minimum: giá trị nhỏ nhất
– Maximum: giá trị lớn nhất
– Step: số bước tăng khi gọi hàm PerformStep
– Value: giá trị hiện tại
– Style: kiểu của progress bar
HBU
Các control khác 4
LTGD
130
131. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ProgressBar
Phương thức
– PerformStep(): tăng thêm step
– Increment(int value): tăng vị trí hiện tại của tiến độ với
giá trị xác định
HBU
Các control khác 4
LTGD
131
132. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ProgressBar
HBU
Các control khác 4
LTGD
Max: 100
Min: 0
Step: 10
132
133. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
ProgressBar
HBU
Các control khác 4
LTGD
Mỗi lần click chuột, thanh progress sẽ tăng lên 10%
133
134. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Trackbar
A track bar is a control used to slide a small bar
or pointer, also called a thumb, along a
continuous line.
HBU
Các control khác 4
LTGD
134
135. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Trackbar – Properties
HBU
Các control khác 4
LTGD
135
Properties Description
Maximum Gets or sets the upper limit of the range this TrackBar is
working with.
Minimum Gets or sets the lower limit of the range this TrackBar is
working with
TickFrequency Gets or sets a value that specifies the delta between ticks
drawn on the control.
Value Gets or sets a numeric value that represents the current
position of the scroll box on the track bar.
TickStyle Gets or sets a value indicating how to display the tick
marks on the track bar.
136. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Trackbar – Methods, events
HBU
Các control khác 4
LTGD
136
137. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Scrollbar–
A scrollbar is a control that allows the user to
navigate a document in two directions by clicking
a button that displays an arrow. The control is
equipped with one button at each of its ends
HBU
Các control khác 4
LTGD
137
138. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Scrollbar
Based on their orientation, there are two types of scroll
bars:
Horizontal: scroll bar allows the user to navigate a document left
and right
Vertical: scroll bar allows navigating up and down.
Based on their relationship with the parent control, there
are two types of scroll bars:
associated with their parent or owner
scroll bar controls that are manually added by the programmer
HBU
Các control khác 4
LTGD
138
139. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Scrollbar– Properties
HBU
Các control khác 4
LTGD
139
Properties Description
Maximum Gets or sets the upper limit of values of the scrollable
range
Minimum Gets or sets the lower limit of values of the scrollable
range
TickFrequency Gets or sets a value that specifies the delta between ticks
drawn on the control.
Value Gets or sets a numeric value that represents the current
position of the scroll box on the track bar.
TickStyle Gets or sets a value indicating how to display the tick
marks on the track bar.
140. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Scrollbar– Method, events
HBU
Các control khác 4
LTGD
140
141. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Timer
Bộ định thời gian, thiết lập một khoảng thời gian xác
định (interval) và khi hết khoảng thời gian đó Timer
sẽ phát sinh sự kiện tick.
Thường dùng
Quản lý và thông báo các trạng thái
Autosave
…..
HBU
Các control khác 4
LTGD
141
142. CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Timer – Methods
Events
Properties
HBU
Các control khác 4
LTGD
142
143. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
Timer – Methods
HBU
Các control khác 4
LTGD
Enable Timer
Khoảng thời
gian chờ giữa
2 lần gọi Tick
143
144. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
MonthCalander:
Cho phép user chọn một ngày trong tháng hoặc nhiều
ngày với ngày bắt đầu và ngày kết thúc
Properties:
MaxDate: Gets or sets the maximum allowable date
MinDate: Gets or sets the minimum allowable date
SelectionStart: ngày bắt đầu chọn
SelectionEnd: ngày kết thúc
ShowToday: Gets or sets a value indicating whether the date
represented by the TodayDate property is displayed at the
bottom of the control
HBU
Các control khác 4
LTGD
144
145. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
MonthCalander:
Properties:
TodayDate: Gets or sets the value that is used by
MonthCalendar as today's date.
ShowTodayCircle: Gets or sets a value indicating whether
today's date is identified with a circle or a square.
ShowWeekNumbers: Gets or sets a value indicating whether
the month calendar control displays week numbers (1-52) to
the left of each row of days
HBU
Các control khác 4
LTGD
145
146. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
MonthCalander – events
HBU
Các control khác 4
LTGD
146
Hiển thị ngày được click vào
147. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
DateTimePicker:
Cho phép chọn ngày trong khoảng xác định thông
qua giao diện đồ họa dạng calendar
Là sự kết hợp của ComboBox và MonthCalendar
HBU
Các control khác 4
LTGD
147
148. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
DateTimePicker: Properties
HBU
Các control khác 4
LTGD
148
Name Description
Format Gets or sets the format of the date and time
displayed in the control
CustomFormat Gets or sets the custom date/time format string
Value Gets or sets the date/time value assigned to the
control.
MaxDate Gets or sets the maximum date and time that can
be selected in the control
MinDate Gets or sets the minimum date and time that can
be selected in the control.
149. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
DateTimePicker: Custom Format
HBU
Các control khác 4
LTGD
149
Format string Description
d The one- or two-digit day.
dd
The two-digit day. Single-digit day values are
preceded by a 0.
ddd The three-character day-of-week abbreviation.
dddd The full day-of-week name.
h The one- or two-digit hour in 12-hour format.
hh
The two-digit hour in 12-hour format. Single digit
values are preceded by a 0.
H The one- or two-digit hour in 24-hour format.
150. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
DateTimePicker: Custom Format
HBU
Các control khác 4
LTGD
150
Format string Description
HH
The two-digit hour in 24-hour format. Single digit values are
preceded by a 0.
m The one- or two-digit minute.
mm The two-digit minute. Single digit values are preceded by a 0.
M The one- or two-digit month number.
mm The two-digit minute. Single digit values are preceded by a 0.
M The one- or two-digit month number.
MM
The two-digit month number. Single digit values are preceded by
a 0.
MMM The three-character month abbreviation.
151. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
DateTimePicker: Custom Format
HBU
Các control khác 4
LTGD
151
Format string Description
MMMM The full month name.
s The one- or two-digit seconds.
ss The two-digit seconds. Single digit values are preceded by a 0.
t The one-letter A.M./P.M. abbreviation (A.M. is displayed as "A").
tt The two-letter A.M./P.M. abbreviation (A.M. is displayed as "AM").
y The one-digit year (2001 is displayed as "1").
yy The last two digits of the year (2001 is displayed as "01").
yyyy The full year (2001 is displayed as "2001").
153. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
CheckedListBox
A checked list box is a list box whose items
are each equipped with a check box.
HBU
Các control khác 4
LTGD
153
154. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
CheckedListBox - properties
HBU
Các control khác 4
LTGD
154
Name Description
CheckedItems Collection of checked items in this CheckedListBox.
DataSource Gets or sets the data source for the control.
DisplayMember Gets or sets a string that specifies a property of the
objects contained in the list box whose contents you
want to display
ValueMember Gets or sets a string that specifies the property of the
data source from which to draw the value
Text Gets or searches for the text of the currently selected
item in the ListBox.
155. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
CheckedListBox - properties
HBU
Các control khác 4
LTGD
155
Name Description
CheckOnClick Gets or sets a value indicating whether the check box
should be toggled when an item is selected
Items Gets or sets the data source for the control.
SelectedIndex Gets or sets the zero-based index of the currently
selected item in a
SelectedItem Gets or sets the currently selected item in the ListBox
SelectedValue Gets or sets the value of the member property
specified by the ValueMember property
156. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
CheckedListBox - Methods
ClearSelected(): Unselects all items in the ListBox
GetItemChecked(Int32): Returns a value indicating
whether the specified item is checked.
GetItemText(Object): Returns the text representation
of the specified item
GetSelected(Int32): Returns a value indicating
whether the specified item is selected
HBU
Các control khác 4
LTGD
156
157. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
CheckedListBox – items’ Methods
Add: Thêm mới Item vào checkedlistbox
Insert(index, object): thêm mới item vào vị trí index
Remove(): xóa một item
RemoveAt(index): xóa item ở vị trí index
Clear(): xóa tất cả các item
Count(): đếm có bao nhiêu item trong checkedlistbox
HBU
Các control khác 4
LTGD
157
158. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
CheckedListBox - Events
OnClick(EventArgs): Raises the Click event.
OnItemCheck(ItemCheckEventArgs): Raises the
ItemCheck event.
HBU
Các control khác 4
LTGD
158
159. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
CheckedListBox – Tạo
HBU
Các control khác 4
LTGD
159
Hoặc tạo từ property của checkedlistbox
160. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
CheckedListBox – Tạo bằng code
HBU
Các control khác 4
LTGD
160
string add = txtAdd.Text;
chkListObj.Items.Add(add,true);
Checkbox được check sẵn
161. Enable sự
kiện Tick
CHƯƠNG 2: CÁC ĐIỀU KHIỂN
CheckedListBox – Xóa item
HBU
Các control khác 4
LTGD
161
private void btnXoa_Click(object sender, EventArgs e)
{
CheckedListBox.CheckedItemCollection item;
item = chkListObj.CheckedItems;
if (item.Count > 0)
{
int c = item.Count;
while(item.Count>0)
{
chkListObj.Items.Remove(item[0]);
}
}
}