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.
Trang 1Ch ng 3
CONTROL
1
i dung
Introduction Windows form
Control Properties and Layout
Labels, TextBoxes and Buttons
GroupBoxes and Panels
CheckBoxes and RadioButtons
PictureBoxes
2
1 Introduction
• Giao di n là ràng bu c, giao c m b o cho các l p hay các c u trúc s th c hi n m t u gì ó
• t giao di n a ra m t s thay th cho các l p tr u
ng t o ra các s ràng bu c gi a nh ng l p và các thành ph n client c a nó
• Khi m t l p th c thi m t giao di n, l p này ph i th c thi
t c các ph ng th c c a giao di n ây là m t b t
bu c mà các l p ph i th c hi n
• Trong ch ng này chúng ta s th o lu n :
• Cách t o, th c thi và s d ng các giao di n
• Cách th c thi nhi u giao di n cùng v i cách k t h p
2 Windows form
• Cho phép ng i phát tri n t o ra các giao di n ng i dùng s d ng các thành ph n khác nhau (components)
• t Form c ch y trên m t máy tính c c b (local machine) và m t form có th truy c p n các tài nguyên khác nhau nh b nh , các th m c, các t p tin, các c s d li u…
• Do ó Windows Form phù h p cho các ng d ng desktop nh các ng d ng qu n lý thông tin, các 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) các thông tin t i ng i dùng.
– t n i n các ngu n CSDL khác nhau trên các máy tính c c b
ho c máy tính khác
Trang 2Windows form tính n ng m i
phép l u tr các thông tin toàn c c c ng nh các thông tin liên quan t i ng i dùng vào trong các ngu n l u tr (file XML, CSDL, các t p tin…)
khi n m i, cho phép t o ra các ng d ng v i giao di n a d ng, phong phú h n.
nh
5
Các tính n ng c a form
• Trong NET thì t o m t Windows Form ng a là
o m t i t ng t l p Form
• p này n m trong Namespace có tên là System.Windows.Forms
• Nói cách khác :
Form trong các ng d ng
ng) c a l p Form
6
o form
• n có th s d ng tính n ng tr c quan thi t
p các u khi n
• n c ng có th s d ng ch c n ng vi t code
t o các u khi n
• n có th k t h p c 2 cách trên khi t o form
7
• Ch n m c Visual Studio NET trong menu Start
• File New Project
8
Trang 33 Control Properties and Layout
10
Chi ti t l p form
• autosize thay i kích th c form
• Text y/thi t l p tiêu c a Windows Form.
• Width r ng c a Form.
• Height chi u cao c a Form.
• StartPosition ch ra v trí kh i ng ban u c a Form.
• Location ch ra v trí theo t a x,y.
• Right a bên ph i.
• FormBorderStyle ch ra ki u vi n c a Form.
• MaximizeBox cho phép hi n th nút phóng to hay ko?
• MinimizeBoz cho phép hi n th nút thu nh ko.
• Tag thu c tính này th ng c s d ng ính kèm
li u vào Form.
• TopMost cho phép Form luôn c hi n th trên t t c các
Form khác ko M c nh là ko (false).
• Controls là t p h p các u khi n trên Form.
• Show Hi n th m t Form
• ShowDialog Hi n th Form d i d ng m t h p
tho i
• Activate Kích ho t Form
Trang 4Các s ki n
• Init y ra sau khi Form c t o ra
• Load y ra khi Form c n p vào b
nh vào sau ó c hi n th S
ki n này là th i m thích h p chúng ta l y d li u và gán cho các u khi n trên Form
• FormClosing X y ra tr c khi Form c óng
• FormClosed y ra sau khi Form c óng
và tr c khi Form c gi i phóng kh i b nh
13
• Basic Controls (các khi n c b n)
• Value Setting Controls (các u khi n thi t l p giá tr )
• Selection List Controls (các u khi n danh sách
ch n)
• Grouping Controls (các u khi n nhóm)
• Graphics Controls (các u khi n h a)
14
• Xem file QuyTacDatTen.pdf
15
4 Labels, TextBoxes and Buttons
• Labels:
– Các nhãn – Gi i thích; minh ho
• TextBoxes:
– Dùng nh p li u
• Buttons:
16
Trang 5Labels
• AutoSize
• Backcolor
• BorderColor
• BorderStyle
• Location(X, Y)
• Name
• Font
• ForeColor
• Size
• TabIndex
• Text
• TextAlign
18
code
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 label //l nh khai báo các trình bày //khai báo label
//khai báo form }
#endregion // nh ngh a label trong l p riêng }
}
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
this.label1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.ForeColor = System.Drawing.SystemColors.MenuHighlight;
this.label1.Location = new System.Drawing.Point(18, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 15);
this.label1.TabIndex = 4;
this.label1.Text = "L p CDTH11QN";
this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
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.label1);
this.Name = "Form1";
this.Text = "Chào b n 1";
this.ResumeLayout(false);
this.PerformLayout();
TextBoxes
• i a d li u vào bi n nh
• i a d li u t bi n nh ra hi n th ( ít s
ng )
Trang 6• nh ngh a
• Khai báo:
– Font – Color – Point – Multiline – Name – Enabled
– Size
– TabIndex
• Kích ho t
• Khai báo bi n riêng
1 L y d li u t textbox a vào bi n nh
2 a d li u t bi n nh lên textbox
21
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
//l nh khai báo các trình bày //khai báo TextBox
//khai báo form, kích ho t textbox
}
#endregion // nh ngh a text trong l p riêng
} }
22
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 li u
• y d li u t textbox 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
Trang 7• Nút xác nh các ki n liên quan
• o Buttons:
– Khai báo
• Thu c tính
• ki n (events)
– Kích ho t – Khai báo button thu c p riêng
25
nh ngh a
this.bttinh= new System.Windows.Forms.Button();
26
Thu c tính
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);
• Focus: Cho phép thi t l p khi kh i t o Form u tiên u khi n TextBox
• Click: y ra khi u khi n Button c nh n
nh n CommandName và CommandArgument
c truy n qua s ki n
Trang 8Xác nh events
private void bttinh_Click(object sender, EventArgs e) {
int so1, so2, so3;
so1 = Convert.ToInt32(txtso1.Text);
so2 = Int32.Parse(txtso2.Text);
so3 = so1 + so2;
txtso3.Text = Convert.ToString(so3);
} “" + so3;
29
Kích ho t trong form
this.Controls.Add(this.lbtong);
30
Khai báo button trong l p riêng
private System.Windows.Forms.Button bttinh;
31
5 GroupBoxes and Panels
• Groupboxes:
– Vùng nhóm các trình bày
• RadioButton
• CheckBox
• Panels:
– Vùng giao di n
32
Trang 9GroupBoxes
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
// groupBox1
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";
// Form1
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);
Trang 10namespace 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;
}
// panel1
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;
39
// Form1
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);
40
Trang 116 CheckBoxes and RadioButtons
• CheckBoxes:
– Có nhi u l a ch n
• Ch n 1 ho c nhi u h n 1
• Ch n t t c
• Không ch n
• RadioButtons
– Có nhi u l a ch n
• d ng CheckBoxes và RadioButtons
41
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;
} }
42
// checkBox1
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;
// 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);
Trang 12// groupBox2
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";
45
RadioButtons
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;
} }
46
// radioButton1
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;
47
// 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.groupBox1);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
48
Trang 13// groupBox1
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";
49
d ng RadioButton và Checkbox
• c ích:
• Câu l nh:
radioButton3 Checked == true / false checkBox1 Checked == true / false
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 và N ";
if ((checkBox1.Checked == false) && (checkBox2.Checked == false))
gioitinh.Text = "";
}