Tải bản đầy đủ (.pdf) (26 trang)

Giáo trình C winform phần 2.1

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (153.16 KB, 26 trang )


L p trình trên môi tr ng windowsậ ườ
Windows control – Ph n 2ầ

N i dungộ

MaskEditBox

CheckBox

RadioButton

Panel

ListView

TreeView

Timer

SendKey

Thi t k layoutế ế

Dock, Anchor

MaskEditBox

Namespace: System.Windows.Forms

Đ nh d ng th hi n và nh p c a TextBox nh đ nh ị ạ ể ệ ậ ủ ư ị


d ng s đi n tho i,….ạ ố ệ ạ

Thu c tính Mask : thi t l p m t nộ ế ậ ặ ạ

Mask đ c đ nh nghĩa s nượ ị ẵ

T đ nh nghĩa Maskự ị

MaskEditBox

Mask đ c đ nh nghĩa s nượ ị ẵ

MaskEditBox

T đ nh nghĩa Maskự ị
Thành ph n maskầ Ý nghĩa
0 S . Yêu c u b t bu c ph i nh p s t 0-9ố ầ ắ ộ ả ậ ố ừ
9 S ho c kho ng tr ng (Optional)ố ặ ả ắ
# S ho c kho ng tr ng (Optional). Có th nh p ố ặ ả ắ ể ậ
d u + ho c - ấ ặ
L Kí t [a z] ho c [A Z] (B t bu c)ự ặ ắ ộ
? Kí t [a z] ho c [A Z] (Không b t bu c)ự ặ ắ ộ
, Đ n v ph n ngàn (1,234)ơ ị ầ
. Đ n v ph n l (0.32)ơ ị ầ ẻ

MaskEditBox

T đ nh nghĩa Maskự ị

Ví d :ụ

mebSoDienThoai. Mask = “000-0000-000”;
// hàm ki m tra d li u nh pể ữ ệ ậ
if (medSoDienThoai.MaskCompleted == false)
MessageBox.Show(“So dien thoai khong hop le”)
else
soDienThoai = medSoDienThoai.Text

MaskEditBox

T đ nh nghĩa Maskự ị

Ví dụ

Nh p MSSV : 0812345ậ

Nh p mã s n ph m : A-090401-0001ậ ả ẩ

Nhâp ngày sinh : 01/04/2009

CheckBox

Thu c tính Checkedộ

S ki n CheckedChangedự ệ
if (chbDongY.Checked == true)
MessageBox.Show("Dong y");

RadioButton

Thu c tính Checkedộ


Thu c tính CheckedChangedộ
if (rabGioiTinhNam.Checked == true)
MessageBox.Show("Gioi tinh la nam");

Panel

Thu c tínhộ

Size / ClientSize

Controls

ListView

Th hi n d ng l i ho c icon c a các items con.ể ệ ạ ướ ặ ủ

Thu c tínhộ

Columns

Items

View (LargeIcon, SmallIcon, Detail, List)

FullRowSelect

SmallImageList / LargeImageList

S kiên ItemActivateự


ListView

M i item trong ListView là 1 ListViewItemỗ

Thu c tính ListViewItem ộ

Text

SubItems (Ch dùng khi View c a ListView là Detail)ỉ ủ

ImageIndex

ListView
// them cot vao listview
lvwDir.Columns.Add("Name", 200, HorizontalAlignment.Left);
lvwDir.Columns.Add("Size", 80, HorizontalAlignment.Right);
lvwDir.Columns.Add("Type", 80, HorizontalAlignment.Left);
lvwDir.Columns.Add("Date Modified", 160,
HorizontalAlignment.Left);
// hien thi theo dang chi tiet
lvwDir.View = View.Details;

ListView
// them danh sach hinh cho icon cua listview
lvwDir.SmallImageList = new ImageList();
lvwDir.SmallImageList.Images.Add(new Icon("icons/folder.ico"));
lvwDir.SmallImageList.Images.Add(new
Icon("icons/document.ico"));


ListView
foreach (DirectoryInfo subDir in curDir.GetDirectories())
{
ListViewItem lvi = lvwDir.Items.Add(subDir.Name);
lvi.Tag = subDir;
lvi.ImageIndex = 0;
lvi.SubItems.Add("");
lvi.SubItems.Add("Folder");
lvi.SubItems.Add(subDir.LastWriteTime.ToString());
}

TreeView

Th hi n d ng Câyể ệ ạ

T ng node bên trong nó là 1 TreeNodeừ

Trong 1 TreeNode có th có 1 ho c nhi u TreeNode ể ặ ề
con

TreeView

Thu c tính TreeViewộ

Nodes

SelectedNode

ImageList


S ki n TreeViewự ệ

AfterSelect

BeforeSelect

Ph ng th c TreeViewươ ứ

CollapseAll

ExpandAll

TreeView
// them danh sach cac icon
tvwDir.ImageList = new ImageList();
tvwDir.ImageList.Images.Add(new Icon("icons/mycomputer.ico"));
tvwDir.ImageList.Images.Add(new Icon("icons/drive.ico"));
tvwDir.ImageList.Images.Add(new Icon("icons/folder.ico"));
tvwDir.ImageList.Images.Add(new Icon("icons/document.ico"));

TreeView
// them nut My computer va cac o dia
TreeNode myComputerNode = new TreeNode("My computer");
myComputerNode.Tag = "My computer";
myComputerNode.ImageIndex = 0;
tvwDir.Nodes.Add(myComputerNode);

TreeView
// them cac node o dia vao mycomputer node
foreach (DriveInfo drive in DriveInfo.GetDrives())

{
TreeNode driveNode = new TreeNode(drive.Name);
driveNode.Tag = drive.RootDirectory;
driveNode.ImageIndex = 1;
myComputerNode.Nodes.Add(driveNode);
}

Timer

Thu c tínhộ

Interval : chu kỳ c a timer (mili giây)ủ

S ki nự ệ

Tick

Ph ng th c :ươ ứ

Start : b t đ u timerắ ầ

Stop : d ng timerừ

SendKeys

G i 1 thao tác phím đ n ng d ngở ế ứ ụ

Ví d :ụ
SendKeys.Send(“A”);
SendKeys.Send(“abc”);

SendKeys.Send(“{Enter}”);
SendKeys.Send(“A{Enter}”);
SendKeys.Send(“+(abc)”); // shift
SendKeys.Send(“^(abc)”); // ctrl
SendKeys.Send(“%(abc)”); // alt

Thi t k layoutế ế

S d ng Toolbar ử ụ
LayOut

Thi t k v trí các ế ế ị
control trong giao di n ệ
nh : ư

Th c hi n s p x pự ệ ắ ế

Canh ch nh kích th c ỉ ướ
gi a các controlữ

Canh t a đ cho nhi u ọ ộ ề
control
Ch n ToolBar LayOut ọ

Thi t k layoutế ế
Ch n ọ 2 control tr lênở
Toolbar đ c enableượ

Thi t k layoutế ế


Cách thao tác:

B c 1: Ch n t i thi u 2 controls (Nh n phím Ctrl + ướ ọ ố ể ấ
click chu t trái lên các control đ c ch n)ộ ượ ọ

B c 2: Ch n các bi u t ng trong thanh Layout ướ ọ ể ươ
ToolBar

×