Bài tập thực hành lập trình winform bài 2

8 496 1
Bài tập thực hành lập trình winform   bài 2

Đang tải... (xem toàn văn)

Thông tin tài liệu

Mục tiêu Luyện tập với control thể danh sách như: ListView, TreeView Nội dung Sửa Bài tập tuần 01 - Bước 1: Xây dựng giao diện form sau txtNumC txtNumR btnCreate - Bước 2: Tạo form tên Form2 - Bước 3: Code Form1.cs sau public partial class Form1 : Form { public Form1() { InitializeComponent(); btnCreate.Click += btnCreate_Click; } void btnCreate_Click(object sender, EventArgs e) { int nr, nc; if (!int.TryParse(txtNumR.Text, out nr)) nr = 3; if (!int.TryParse(txtNumC.Text, out nc)) nc = 3; Form2 frm = new Form2(nr, nc); frm.StartPosition = FormStartPosition.CenterScreen; frm.ShowDialog(); } } Ở thực gắn kết event code (không thông qua design) Việc để hiểu rõ chất gắn kết event nhằm dễ dàng thực control tự tạo code không qua design (Ở Form2) - Bước 4: Code Form2.cs sau public partial class Form2 : Form { int margin = 5, padding = 2, s = 30; public Form2() { InitializeComponent(); } public Form2(int nr, int nc) { InitializeComponent(); Button btn = null; for(int i=0; i= 0) { //vì ViewMode ListView đặt giá trị theo enum //nên dễ dàng ánh xạ với index lv.View = (View)cbLvView.SelectedIndex; } }  Viết code cho event AfterSelect TreeView void tv_AfterSelect(object sender, TreeViewEventArgs e) { //lấy đối tượng giữ Tag var th = e.Node.Tag as TruongHoc; //xét TruongHoc if (th == null) { FillListView(e.Node.Tag as LopHoc); } else { FillListView(th); } } Với hàm thêm phần tử cho ListView sau (ở có trường hợp lựa chọn TruongHoc hay LopHoc TreeView nên có phiên hàm) void FillListView(TruongHoc th) { //xóa ListView lv.Clear(); //thêm cột ColumnHeader ch = new ColumnHeader(); ch.Text = "Tên lớp"; ch.Width = 200; lv.Columns.Add(ch); ch = new ColumnHeader(); ch.Text = "Trường"; ch.Width = 200; lv.Columns.Add(ch); //thêm phần tử //luôn ghi nhớ item ListView ListViewItem ListViewItem lvi = null; foreach (LopHoc lh in th.ListLopHoc) { lvi = new ListViewItem(lh.TenLop); lvi.SubItems.Add(th.TenTruong); //với ListViewItem có Tag để sử dụng lvi.Tag = lh; lv.Items.Add(lvi); } } void FillListView(LopHoc lh) { //xóa ListView lv.Clear(); //thêm cột ColumnHeader ch = new ColumnHeader(); ch.Text = "Họ tên"; ch.Width = 200; lv.Columns.Add(ch); ch = new ColumnHeader("Lớp"); ch.Text = "Lớp"; ch.Width = 200; lv.Columns.Add(ch); //thêm phần tử ListViewItem lvi = null; foreach (HocSinh hs in lh.ListHocSinh) { lvi = new ListViewItem(hs.HoTen); lvi.SubItems.Add(lh.TenLop); lvi.Tag = hs; lv.Items.Add(lvi); } }  Cuối viết code cho event DoubleClick ListView void lv_DoubleClick(object sender, EventArgs e) { //kiểm tra item listView có chọn if (lv.SelectedItems.Count > 0) { ListViewItem lvi = lv.SelectedItems[0]; var lh = lvi.Tag as LopHoc; if (lh != null) { FillListView(lh); } } } - Bước 4: Chạy chương trình Bài tập  Hoàn thành ListView TreeView  Viết chương trình ví dụ loại UserControl (Extended Composite) ... LopHoc; if (lh != null) { FillListView(lh); } } } - Bước 4: Chạy chương trình Bài tập  Hoàn thành ListView TreeView  Viết chương trình ví dụ loại UserControl (Extended Composite) ...public partial class Form2 : Form { int margin = 5, padding = 2, s = 30; public Form2() { InitializeComponent(); } public Form2(int nr, int nc) { InitializeComponent(); Button... có đối số Form2 (tự xây dựng) Đối với thông số như: kích thước, mức canh lề,… nên đặt giá trị biến để dễ dàng thay đổi (không code cứng kiểu: btn.Width = 20 ;) - Bước 5: Chạy chương trình Sử dụng

Ngày đăng: 22/01/2016, 18:33

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan