Bài giảng Lập trình trên môi trường Windows - Windows Control - Phần 1 - Trần Duy Hoàng

pdf 31 trang hoanguyen 3250
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Lập trình trên môi trường Windows - Windows Control - Phần 1 - Trần Duy Hoàng", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfbai_giang_lap_trinh_tren_moi_truong_windows_windows_control.pdf

Nội dung text: Bài giảng Lập trình trên môi trường Windows - Windows Control - Phần 1 - Trần Duy Hoàng

  1. Lậ p trình trên môi tr ườ ng Windows Windows control - Ph n 1 ầ Tr n Duy Hoàng ầ tdhoang@hcmus.edu.vn
  2. Nộ i dung  Form  TextBox  CompoBox  Thuộ c tính, hàm chung  Dialog thông dụ ng
  3. Form  System.Windows.Forms  Hình thành giao diệ n s ử d ụ ng  Sắ p x ế p và thi ế t k ế các control c ơ b ả n
  4. Form  Nhóm thuộ c tính hi ể n th ị ● BackColor ➢ this.BackColor = Color.White; ● ForeColor ➢ this.ForeColor = Color.Black; ● BackgroundImage ➢ this.BackgroundImage = new Bitmap("background.jpg"); ● Text ➢ this.Text = “Quan ly Hoc sinh”; ● FormBorderStyle ➢ this.FormBorderStyle = FormBorderStyle.None;
  5. Form  Nhóm thuộ c tính layout ● Size ➢ this.Size = new Size(100,100); ● ClientSize ➢ this.ClientSize = new Size(100,100); ● StartPosition ➢ this.StartPosition = FormStartPosition.CenterScreen; ● WindowState ➢ this.WindowState = FormWindowState.Maximized;
  6. Form  Nhóm thuộ c tính misc ● AcceptButton ➢ this.AcceptButton = btnDangNhap; ● CancelButton ➢ this.CancelButton = btnBoQua;
  7. Form  Nhóm thuộ c tính window style ● IsMdContainer ➢ this.IsMdContainer = true; ● Opacity ➢ this.Opacity = 0.5; ● ControlBox ● MaximizeBox / MinimizeBox ● Icon ➢ this.Icon = new Icon(“icon.ico”);
  8. Form  Ví dụ : trong hàm form_load private void DemoForm_Load(object sender, EventArgs e) { this.Text = "Demo"; this.Size = new Size(500, 500); this.BackgroundImage = new Bitmap("background.jpg"); this.Opacity = 0.75; }
  9. Form  Thuộ c tính Controls ● Chứ a danh sách các control con c ủ a nó ● Thêm xóa độ ng các control vào form Button btn = new Button; btn.Text = “Hello”; btn.Size = new Size (50, 50); btn.Location = new Point (10,10); this.Controls.Add(btn);
  10. Form  Ví dụ : thêm 1 m ả ng button this.ClientSize = new Size(500, 500); Button[,] arrButton = new Button[10, 10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { arrButton[i,j] = new Button(); arrButton[i,j].Size = new Size(50, 50); arrButton[i,j].Location = new Point(j * 50, i * 50); this.Controls.Add(arrButton[i,j]); } }
  11. Form  Danh sách các hàm ● Show() this.IsMdContainer = true; FrmThemHocSinh frm = new FrmThemHocSinh(); frm.Show(); ● ShowDialog() FrmThemHocSinh frm = new FrmThemHocSinh(); frm.ShowDialog(); if (frm.DialogResult == DiaLogResult.OK) { MessageBox.Show(“Them thanh cong”); }
  12. Form  Các sự ki ệ n đóng m ở form ● Load() ● Closing() DialogResult r = MessageBox.Show(“Ban co muon thoat”, “Thong bao”, MessageBoxButtons.YesNo) if (r == DialogResult.No) e.Cancel = true; ● Closed()
  13. Form  Các sự ki ệ n v ề bàn phím ● Thuộ c tính KeyPreview ➢ this.KeyPreview = true; ● KeyPress() ➢ if (char.IsLower(e.KeyChar)) e.KeyChar = char.ToUpper(e.KeyChar); ➢ if (!char.IsDigit(e.KeyChar)) e.Handled = true;
  14. Form  Các sự ki ệ n v ề bàn phím ● KeyDown(), KeyUp() ➢ if (e.KeyCode == Keys.F5) { ThemNhanVien(); e.Handled = true; } ➢ if (e.KeyCode == Keys.N && e.Control == true) { ThemNhanVien(); e.Handled = true; }
  15. Form  Các sự ki ệ n v ề chu ộ t ● MouseDown() ● MouseUp() ● MouseEnter() ● MouseHover() ● MouseLeave()
  16. TextBox  Các thuộ c tính ● CharacterCasing ➢ txtHoTen.CharacterCasing = CharacterCasing.Upper; ● Multiline ➢ txtDiaChi.Multiline = true; ● PasswordChar ➢ txtMatKhau.Password = '*'; ● MaxLength ➢ txtHoTen.MaxLength = 20;
  17. TextBox  Các thuộ c tính ● Text ● SelectedText ● SelectionStart ● SelectionLength
  18. TextBox  Các sự ki ệ n ● Validating() / Validated() ● TextChange
  19. TextBox  Auto complete ● AutoCompleteMode ➢ Append ➢ Suggest ➢ SuggestAppend ● AutoCompleteSource ➢ FileSystem / FileSystemDirectories ➢ AllUrl / HistoryList ➢ CustomSource ● AutoCompleteCustomSource
  20. ComboBox  Các thuộ c tính ● DropDownStyle ➢ DropDown ➢ DropDownList ● Items ● DataSource ➢ DisplayMember ➢ ValueMember
  21. ComboBox  Các thuộ c tính ● SelectedIndex ● SelectedItem ● Text ● SelectedValue
  22. ComboBox  Các sự ki ệ n ● SelectedIndexChanged ● SelectedValueChanged
  23. Thuộ c tính, hàm chung  TabIndex  TabStop  Enable btnSave.Enable = false; ThemNhanVien(); btnSave.Enable = true;  Focus() ➢ If (txtHoTen.Text == “”) { MessageBox.Show (“Ban chua nhap ho ten”); txtHoTen.Focus(); }
  24. Dialog thông dụ ng  OpenFileDialog OpenFileDialog dialog = new OpenFileDialog(); dialog.InitialDirectory = Application.ExecutablePath; dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; dialog.FilterIndex = 2; dialog.RestoreDirectory = true; if (dialog.ShowDialog() == DialogResult.OK) { MessageBox.Show(dialog.FileName); }
  25. Dialog thông dụ ng  SaveFileDialog SaveFileDialog dialog = new SaveFileDialog(); dialog.InitialDirectory = Application.ExecutablePath; dialog.Filter = "txt files (*.txt)|*.txt"; dialog.RestoreDirectory = true; if (dialog.ShowDialog() == DialogResult.OK) { MessageBox.Show(dialog.FileName); }
  26. Dialog thông dụ ng  FolderBrowserDialog FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.RootFolder = Environment.SpecialFolder.Desktop; dialog.ShowNewFolderButton = true; if (dialog.ShowDialog() == DialogResult.OK) { MessageBox.Show(dialog.SelectedPath); }
  27. Dialog thông dụ ng  FolderBrowserDialog
  28. Dialog thông dụ ng  ColorDialog  FontDialog
  29. Thả o lu ậ n 02/15/11 Tr n Duy Hoàng - tdhoang@fit.hcmus.edu.vn 31/10 ầ