Bài giảng Lập trình C# 1 - Chương 3: Control

15 5 0
Bài giảng Lập trình C# 1 - Chương 3: Control

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Bài giảng Lập trình C# 1 - Chương 3: Control giới thiệu chung, các tính năng của form, Control Properties and Layout, Labels, TextBoxes and Buttons, xử lý dữ liệu, kích hoạt trong form, GroupBoxes and Panels.

F -X C h a n ge PD F -X C h a n ge N y bu om k lic tr ac c C om k lic C c re k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! 12/05/2011 O W ! PD k e r- s o ft w a i dung Ch ng Introduction CONTROL Labels, TextBoxes and Buttons GroupBoxes and Panels Windows form Control Properties and Layout CheckBoxes and RadioButtons PictureBoxes 1 Introduction Windows form • Giao di n ràng bu c, giao c m b o cho l p hay c u trúc s th c hi n m t u ó • t giao di n a m t s thay th cho l p tr u ng t o s ràng bu c gi a nh ng l p thành ph n client c a • Khi m t l p th c thi m t giao di n, l p ph i th c thi t c ph ng th c c a giao di n ây m t b t bu c mà l p ph i th c hi n • Trong ch ng s th o lu n : • Cách t o, th c thi s d ng giao di n • Cách th c thi nhi u giao di n v i cách k t h p m ng giao di n • Cho phép ng i phát tri n t o giao di n ng i dùng s d ng thành ph n khác (components) • t Form c ch y m t máy tính c c b (local machine) m t form có th truy c p n tài nguyên khác nh b nh , th m c, t p tin, c s d li u… • Do ó Windows Form phù h p cho ng d ng desktop nh ng d ng qu n lý thông tin, ng ng t ng tác tr c ti p v i ng i dùng • Vai trị c a Windows Form: – lý d li u c nh p b i ng i dùng – Hi n th (trình bày) thông tin t i ng i dùng – t n i n ngu n CSDL khác máy tính c c b ho c máy tính khác re F -X C h a n ge PD F -X C h a n ge N y bu Windows form tính n ng m i • • • • • om k lic tr ac c C om k lic C c re k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! 12/05/2011 O W ! PD k e r- s o ft w a Các tính n ng c a form ClickOne Deployment: m t k thu t cho phép t o ng d ng mà có th c cài t v i thao tác c a ng i dùng nh t Application Settings (các thi t l p ng d ng): tính n ng cho phép l u tr thơng tin tồn c c c ng nh thông tin liên quan t i ng i dùng vào ngu n l u tr (file XML, CSDL, t p tin…) New Windows Form Controls: tính n ng cung c p u n m i, cho phép t o ng d ng v i giao di n a d ng, phong phú h n New Data Binding Model (mơ hình liên k t d li u m i): tính ng cung c p m t cách n gi n, thu n ti n, m nh m liên k t u n v i m t ngu n d li u Rich Graphics h a phong phú): tính n ng s m r ng c a GDI lên GDI+ c s d ng cho vi c v thao tác v i hình nh • Trong NET t o m t Windows Form ng a o m t i t ng t l p Form • p n m Namespace có tên System.Windows.Forms • Nói cách khác : p Form l p tr u t ng hóa t t c Windows Form ng d ng – i m t Windows Form m t th hi n c th ( i ng) c a l p Form – o form • n có th s d ng tính n ng tr c quan p u n • Ch n m c Visual Studio NET menu Start • File New Project thi t • n c ng có th s d ng ch c n ng vi t code t o u n • n có th k t h p c cách t o form re F -X C h a n ge PD F -X C h a n ge N y bu om k lic tr ac c C om k lic C c re k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! 12/05/2011 O W ! PD k e r- s o ft w a Control Properties and Layout 10 Chi ti t l p form • • • • • • • • • • • • • • • Name autosize Text Width Height StartPosition Location Left Top Right Bottom FormBorderStyle MaximizeBox MinimizeBoz Tag • TopMost • Controls Các ph tên form thay i kích th c form y/thi t l p tiêu c a Windows Form r ng c a Form chi u cao c a Form ch v trí kh i ng ban u c a Form ch v trí theo t a x,y a trái a a bên ph i a bên d i ch ki u vi n c a Form cho phép hi n th nút phóng to hay ko? cho phép hi n th nút thu nh ko thu c tính th ng c s d ng ính kèm li u vào Form cho phép Form c hi n th t t c Form khác ko M c nh ko (false) t p h p u n Form • Show • Hide • ShowDialog • Activate 11 ng th c Hi n th m t Form n Form Hi n th Form d i d ng m t h p tho i Kích ho t Form 12 re F -X C h a n ge PD F -X C h a n ge N y bu Các s • Init • Load • FormClosing • FormClosed ki n Các y sau Form c t o y Form c n p vào b nh vào sau ó c hi n th S ki n th i m thích h p l y d li u gán cho u n Form X y tr c Form c óng y sau Form c óng tr c Form c gi i phóng kh i b nh ac c tr k e r- s o ft w a u n • Basic Controls (các n c b n) • Value Setting Controls (các u n thi t l p giá tr ) • Selection List Controls (các u n danh sách ch n) • Grouping Controls (các u n nhóm) • Graphics Controls (các u n h a) 14 13 Quy t t om k lic C om k lic C c re k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! 12/05/2011 O W ! PD t tên Labels, TextBoxes and Buttons • Xem file QuyTacDatTen.pdf • Labels: – Các nhãn – Gi i thích; minh ho • TextBoxes: – Dùng nh p li u • Buttons: – Các nút 15 u n 16 re F -X C h a n ge PD F -X C h a n ge N y bu om k lic tr ac c C om k lic C c re k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! 12/05/2011 O W ! PD k e r- s o ft w a Labels • • • • • • AutoSize Backcolor BorderColor BorderStyle Location(X, Y) Name • • • • • • Font ForeColor Size TabIndex Text TextAlign 18 17 namespace WindowsFormsApplication3 { partial class Form1code { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) this.label1.BackColor = System.Drawing.SystemColors.ActiveCaptionText; { this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.label1.BorderStyle =!=System.Windows.Forms.BorderStyle.Fixed3D; if (disposing && (components null)) this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; { this.label1.ForeColor = System.Drawing.SystemColors.MenuHighlight; this.ClientSize = new System.Drawing.Size(384, 262); components.Dispose(); this.label1.Location = new System.Drawing.Point(18, 40); } this.label1.Name = "label1"; this.Controls.Add(this.label1); base.Dispose(disposing); this.label1.Size = new System.Drawing.Size(100, 15); } this.label1.TabIndex = 4; = new System.Windows.Forms.Label(); this.Name = "Form1"; this.label1 #region Windows this.label1.Text Form Designer generated code = b"Ln p1"; CDTH11QN"; this.Text = "Chào this.SuspendLayout(); private void InitializeComponent() this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter; { this.ResumeLayout(false); // nh ngh a label this.PerformLayout(); //l nh khai báo trình bày //khai báo label private //khai báo form System.Windows.Forms.Label label1; } #endregion // nh ngh a label l p riêng } } code 19 TextBoxes • • i a d li u vào bi n nh i a d li u t bi n nh hi n th (ít s ng) 20 re F -X C h a n ge PD F -X C h a n ge N y bu • nh ngh a • Khai báo: Font Color Point Multiline Name Enabled Size TabIndex L y d li u t textbox ac c tr k e r- s o ft w a namespace WindowsFormsApplication3 { partial class Form1code { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code private void InitializeComponent() { // nh ngh a TextBox TextBoxes – – – – – – – – om k lic C om k lic C c re k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! 12/05/2011 O W ! PD a vào bi n nh a d li u t bi n nh lên textbox //l nh khai báo trình bày //khai báo TextBox //khai báo form, kích ho t textbox } #endregion // nh ngh a text l p riêng • Kích ho t • Khai báo bi n riêng } } 22 21 this.txtso1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); this.txtso1.Font = new System.Drawing.Font("Times New Roman", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.txtso1.ForeColor = System.Drawing.Color.Maroon; this.txtso1.Location = new System.Drawing.Point(162, 103); this.txtso1.Name = "txtso1"; this.txtso1.Size = new System.Drawing.Size(70, 35); this.txtso1.TabIndex = 5; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(384, 262); this.Controls.Add(this.txtso1); this.Name = "Form1"; this.Text = "Chao ban!"; this.ResumeLayout(false); this.PerformLayout(); private System.Windows.Forms.TextBox txtso1; 23 lý d • y d li u t textbox li u a vào bi n nh : so2 = Convert.ToInt32(txtso2.Text); ho c so1=Int32.Parse(txtso2.Text) • a d li u t bi n nh lên textbox: Console.WriteLine("Tong cua so "+intso1+" va so "+intso2+" la "+inttong); ho c txtso3.Text = "" + so3; 24 re F -X C h a n ge PD F -X C h a n ge N y bu Buttons • Nút xác nh • o Buttons: om k lic tr ac c C om k lic C c re k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! 12/05/2011 O W ! PD k e r- s o ft w a nh ngh a ki n liên quan this.bttinh = new System.Windows.Forms.Button(); – nh ngh a – Khai báo • Thu c tính • ki n (events) – Kích ho t – Khai báo button thu c p riêng 26 25 Thu c tính Ph this.bttinh.Font = new System.Drawing.Font("Times New Roman", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.bttinh.ForeColor = System.Drawing.Color.Red; this.bttinh.Location = new System.Drawing.Point(283, 103); this.bttinh.Name = "bttinh"; this.bttinh.Size = new System.Drawing.Size(66, 28); this.bttinh.TabIndex = 8; this.bttinh.Text = "Tính"; this.bttinh.UseVisualStyleBackColor = true; this.bttinh.Click += new System.EventHandler(this.bttinh_Click); 27 ng th c s ki n • Focus: Cho phép thi t l p kh i t o Form u tiên u n TextBox • Click: y u n Button c nh n • Command: y u n Button c nh n CommandName CommandArgument c truy n qua s ki n 28 re F -X C h a n ge PD F -X C h a n ge N y bu Xác nh events om k lic tr ac c C om k lic C c re k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! 12/05/2011 O W ! PD k e r- s o ft w a Kích ho t form private void bttinh_Click(object sender, EventArgs e) this.Controls.Add(this.lbtong); { int so1, so2, so3; so1 = Convert.ToInt32(txtso1.Text); so2 = Int32.Parse(txtso2.Text); so3 = so1 + so2; txtso3.Text = Convert.ToString(so3); } “" + so3; 30 29 Khai báo button l p riêng GroupBoxes and Panels private System.Windows.Forms.Button bttinh; • Groupboxes: – Vùng nhóm trình bày – Thơng th ng GroupBoxes s ch a: • RadioButton • CheckBox • Panels: – Vùng giao di n – Ch a nhi u i t 31 ng 32 re F -X C h a n ge PD F -X C h a n ge N y bu om k lic tr ac c C om k lic C c re k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! 12/05/2011 O W ! PD k e r- s o ft w a GroupBoxes namespace WindowsFormsApplication5 { partial class Form1 { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.SuspendLayout(); // groupBox1 // Form1 } #endregion private System.Windows.Forms.GroupBox groupBox1; } } 34 33 // groupBox1 // Form1 this.groupBox1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); this.groupBox1.Location = new System.Drawing.Point(97, 42); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(241, 119); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Nhóm cơng vi c"; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(584, 364); this.Controls.Add(this.groupBox1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); 35 36 re F -X C h a n ge PD F -X C h a n ge N y bu om k lic tr ac c C om k lic C c re k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! 12/05/2011 O W ! PD k e r- s o ft w a Panels namespace WindowsFormsApplication5 { partial class Form1 { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code private void InitializeComponent() { this.panel1 = new System.Windows.Forms.Panel(); this.SuspendLayout(); // panel1 // Form1 } #endregion private System.Windows.Forms.Panel panel1; } } 37 38 // panel1 // Form1 this.panel1.AutoScroll = true; this.panel1.BackColor = System.Drawing.SystemColors.ActiveCaption; this.panel1.Location = new System.Drawing.Point(34, 152); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(448, 189); this.panel1.TabIndex = 1; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(584, 364); this.Controls.Add(this.panel1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); 39 40 10 re F -X C h a n ge PD F -X C h a n ge N y bu CheckBoxes and RadioButtons ac c tr k e r- s o ft w a CheckBoxes namespace WindowsFormsApplication5 { partial class Form1 { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code private void InitializeComponent() { this.checkBox1 = new System.Windows.Forms.CheckBox(); this.SuspendLayout(); // checkBox1 // Form1 } #endregion private System.Windows.Forms.CheckBox checkBox1; } } • CheckBoxes: – Có nhi u l a ch n – n c phép: • Ch n ho c nhi u h n • Ch n t t c • Khơng ch n • RadioButtons – Có nhi u l a ch n – n ch c ch n mà thơi • om k lic C om k lic C c re k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! 12/05/2011 O W ! PD d ng CheckBoxes RadioButtons 41 // checkBox1 42 // Form1 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(519, 364); this.Controls.Add(this.groupBox2); this.Controls.Add(this.panel1); this.Controls.Add(this.groupBox1); this.Name = "Form1"; this.Text = "Form1"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.groupBox2.ResumeLayout(false); this.groupBox2.PerformLayout(); this.ResumeLayout(false); this.checkBox1.AutoSize = true; this.checkBox1.Location = new System.Drawing.Point(33, 18); this.checkBox1.Name = "checkBox1"; this.checkBox1.Size = new System.Drawing.Size(48, 17); this.checkBox1.TabIndex = 0; this.checkBox1.Text = "Nam"; this.checkBox1.UseVisualStyleBackColor = true; 43 44 11 re F -X C h a n ge PD F -X C h a n ge N y bu // groupBox2 om k lic tr ac c C om k lic C c re k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! 12/05/2011 O W ! PD k e r- s o ft w a RadioButtons this.groupBox2.Controls.Add(this.checkBox2); this.groupBox2.Controls.Add(this.checkBox1); this.groupBox2.Location = new System.Drawing.Point(233, 28); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(108, 81); this.groupBox2.TabIndex = 2; this.groupBox2.TabStop = false; this.groupBox2.Text = "Gi i tính"; namespace WindowsFormsApplication5 { partial class Form1 { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code private void InitializeComponent() { this.radioButton1 = new System.Windows.Forms.RadioButton(); this.SuspendLayout(); // radioButton1 // Form1 } #endregion private System.Windows.Forms.RadioButton radioButton1; } } 45 46 // radioButton1 // Form1 this.radioButton1.AutoSize = true; this.radioButton1.Location = new System.Drawing.Point(37, 25); this.radioButton1.Name = "radioButton1"; this.radioButton1.Size = new System.Drawing.Size(77, 17); this.radioButton1.TabIndex = 0; this.radioButton1.TabStop = true; this.radioButton1.Text = "Nh p m"; this.radioButton1.UseVisualStyleBackColor = true; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(519, 364); this.Controls.Add(this.groupBox1); this.Name = "Form1"; this.Text = "Form1"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.ResumeLayout(false); 47 48 12 re F -X C h a n ge PD F -X C h a n ge N y bu // groupBox1 om k lic tr ac c C om k lic C c re k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! 12/05/2011 O W ! PD k e r- s o ft w a d ng RadioButton Checkbox this.groupBox1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); this.groupBox1.Controls.Add(this.radioButton4); this.groupBox1.Controls.Add(this.radioButton3); this.groupBox1.Location = new System.Drawing.Point(34, 27); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(160, 83); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Nhóm cơng vi c"; • c ích: – Ki m sốt ng i dùng click vào nút – Xác nh hành ng ti p theo • Câu l nh: radioButton3.Checked == true / false checkBox1.Checked == true / false 49 50 private void button1_Click(object sender, EventArgs e) { if (radioButton3.Checked==true) congviec.Text="Nh p m"; if (radioButton4.Checked == true) congviec.Text = "Tính m"; if (checkBox1.Checked == true) gioitinh.Text = "Ng i Nam"; if (checkBox2.Checked == true) gioitinh.Text = "Ng i N "; if ((checkBox1.Checked==true) && (checkBox2.Checked==true)) gioitinh.Text="Nam N "; if ((checkBox1.Checked == false) && (checkBox2.Checked == false)) } 51 gioitinh.Text = ""; 52 13 re F -X C h a n ge PD F -X C h a n ge N y bu PictureBoxes om k lic tr ac c C om k lic C c re k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! 12/05/2011 O W ! PD k e r- s o ft w a PictureBoxes • • • • • • AnchorStyles Name BackColor Image Size SizeMode 53 PictureBoxes 54 n event click vào nh this.anh1 = new System.Windows.Forms.PictureBox(); ((System.ComponentModel.ISupportInitialize)(this.anh1)).BeginInit(); this.SuspendLayout(); // anh1 this.anh1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.To p | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.anh1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.anh1.Image = global::Control.Properties.Resources.IMG_3264; this.anh1.Location = new System.Drawing.Point(44, 50); this.anh1.Name = "anh1"; this.anh1.Size = new System.Drawing.Size(155, 143); this.anh1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; this.anh1.TabIndex = 0; this.anh1.TabStop = false; 55 private void pictureBox1_Click(object sender, EventArgs e) { //Câu l nh MessageBox.Show("B n ã ch n hoa t tr ng"); } 56 14 re F -X C h a n ge PD F -X C h a n ge N y bu om k lic tr ac c C om k lic C c re k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! 12/05/2011 O W ! PD k e r- s o ft w a Hi n th t ng nh picturebox private void button1_Click(object sender, EventArgs e) { imageNum = imageNum + 1; if (imageNum > 3) imageNum = 1; pictureBox1.Image = Image.FromFile("D:/GIANGDAY/C#/Csharp_Basic/MINHHO A/picture/anh" + imageNum + ".jpg"); } 57 Thanks 58 15 re ... – Các nút 15 u n 16 re F -X C h a n ge PD F -X C h a n ge N y bu om k lic tr ac c C om k lic C c re k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! 12 /05/2 011 O W ! PD k e r- s o ft w... this.label1.Location = new System.Drawing.Point (18 , 40); } this.label1.Name = "label1"; this.Controls.Add(this.label1); base.Dispose(disposing); this.label1.Size = new System.Drawing.Size (10 0, 15 );... System.Drawing.Size( 519 , 364); this.Controls.Add(this.groupBox2); this.Controls.Add(this.panel1); this.Controls.Add(this.groupBox1); this.Name = "Form1"; this.Text = "Form1"; this.groupBox1.ResumeLayout(false);

Ngày đăng: 08/05/2021, 11:47

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

Tài liệu liên quan