Chapter 4Menus, Functions And Common Dialog Programming In C Sharp © 2009 4- 2 METHODS – PHƯƠNG THỨC • Phương thức là một tập hợp trình tự các câu lệnh thực hiện được đặt tên.. • Mỗi
Trang 1Chapter 4
Menus,
Functions And
Common Dialog
Programming In
C Sharp
© 2009
4- 2
METHODS – PHƯƠNG THỨC
• Phương thức là một tập hợp trình tự các câu lệnh thực
hiện được đặt tên
• Mỗi phương thức sẽ có một tên và thân phương thức
• Thân của phương thức sẽ chứa một tập hợp các câu lệnh
thực sự chạy khi phương thức được gọi.
• Tên của phương thực là một định danh có nghĩa Khi
đặt tên phương thức chú ý đặt tên sao cho ngắn gọn gợi
nhớ nội dung bên trong.
• Mỗi phương thức điều có dữ liệu vào, dữ liệu được xử lí
và dữ liệu ra.
Khai Báo Methods _Phương Thức
• Dạng 1 có giá trị trả về
<Kiểu trả về> <Tên phương thức> (Danh sách tham số nếu có)
{
// Các câu lệnh thực hiện trong thân phương thức
< return Giá trị trả về > // bắt buộc có return
}
{
long lngS;
lngS= a+b;
return lngS ;
}
Trang 2© 2009
4- 4
Khai Báo Methods _Phương Thức
Dạng 2 không có giá trị trả về
<void> <Tên phương thức> (Danh sách tham số nếu có)
{
// Các câu lệnh thực hiện trong thân phương thức
// chú ý không có câu lệnh return
}
{
int tam;
tam=a;
a=b;
b=tam;
}
© 2009
4- 5
Lời Gọi Phương Thức
• <Class Name>.<Phương thức>(tham số) nếu trong cùng class thì
có thể bỏ class Name
Ví dụ:
private void btnNhan_Click(object sender, EventArgs e)
{
int intA;
double dblB;
intA = Convert.ToInt32(txtSoA.Text);
dblB= Convert.ToDouble(txtSoB.Text);
lblKQ.Text = HamTich(intA, dblB).ToString();
}
double HamTich(int A, Double dblB)
{
// lenh cua ham
return (A * dblB);
}
© 2009
4- 6
Passing Arguments To Methods
• Value Types : Kiểu trị
+ Bao gồm dữ liệu của các kiểu cơ bản và
dữ liệu của người lập trình tạo ra.
• Reference Type : Kiểu tham chiếu
+Chứa dựng một địa chỉ để chỉ đến bộ nhớ nơi
mà dữ liệu được cất trữ Hoặc do người lập
trình tạo ra kiểu tham chiếu.
Trang 3© 2009
4- 7
Truyền đối số là tham trị trong Methods
Truyền tham trị: Khi truyền đối số cho một Method nếu đối số của
chúng ta là một tham trị thì giá trị của đối số turyền vào sẽ được
giữ nguyên sau khi ra khỏi Method mặc cho trong thân Method co
sự thay đổi giá trị tham số
Ví dụ:
double HamTich(int A, Double dblB)
{
// lenh cua ham
return (A * dblB);
}
Tham trị
© 2009
4- 8
Truyền đối số là tham chiếu trong Methods
Truyền tham chiếu: Khi truyền đối số cho một Method nếu đối số
của chúng ta là một tham chiếu thì giá trị của đối số turyền vào
sẽ được giữ thay đổi khi ra khỏi Method nếu trong thân Method
có sự thay đổi giá trị tham số.
void Tinh(int intA, ref int intB, out double dblC)
{
// out phải gán giá trị trước khi thao tác
intA += 10;
intB += 10;
dblC = 0;
dblC += 10;
dblC = Math.Sqrt(dblC);
}
Tham biến
Ví dụ Viết Qua Methods
private void btnTru_Click(object sender, EventArgs e)
{
//int intHieu;
//intHieu = MamHieu();
//lblKQ.Text = intHieu.ToString();
lblKQ.Text = MamHieu().ToString();
}
int MamHieu()
{
int intA, intB;
intA = Convert.ToInt32(txtSoA.Text);
intB = Convert.ToInt32(txtSoB.Text);
return intA - intB;
}
Trang 4© 2009
4- 10
Ví dụ Viết Qua Methods
private void btnTinh_Click(object sender, EventArgs e)
{
int intA, intB; double dblC=0;
intA = Convert.ToInt16( txtSoA.Text);
intB = Convert.ToInt32( txtSoB.Text);
lblATruoc.Text = intA.ToString();
lblBTruoc.Text = intB.ToString();
lblSoCTruoc.Text = dblC.ToString();
Tinh(intA, ref intB, out dblC);
lblASau.Text = intA.ToString();
lblBSau.Text = intB.ToString();
lblSoCSau.Text = dblC.ToString();
}
void Tinh(int intA, ref int intB, out double dblC)
{
// out phải gán giá trị trước khi thao tác
intA += 10; intB += 10; dblC = 0; dblC += 10;
dblC = Math.Sqrt(dblC);
}
© 2009
4- 11
Menu Designer Initially
MainMenu Control appears in Component Tray Type first Menu here
© 2009
4- 12
Using the Menu Designer
words "Type Here" appear at the top of the
form
Keyboard Access Keys
for a MenuItem object
Trang 5© 2009
4- 13
Submenus
indicates to the user the existence of a
submenu
a menu item and typing the next item's text
© 2009
4- 14
Separator Bars
their purpose
menu
– Typing a single hyphen for the text
– Right-click on Menu Designer where you want
separator bar and choose Insert Separator
Trang 6© 2009
4- 16
Menu Properties
• Text
– Examples: mnuFileExit, mnuHelpAbout,
mnuFormatColorRed
© 2009
4- 17
Menu Design Standards
for names, order/location, access keys,
shortcut keys
File Edit View Format Help
© 2009
4- 18
• Close
• Save As
• Exit
• Replace (Ctrl H)
Trang 7© 2009
4- 19
• Font
• Paragraph
• Alignment
• Color
• System Information
View Menu
• Toolbar
• Status Bar
© 2009
4- 20
Modifying Menu Items Using
Menu Designer
– Insert New menu
– Delete menu
– Insert Separator
– Edit Name, displays menu item Name property
rather than Text property on Form
Ví dụ Qua Menu
Submenu
Menu Items
Trang 8© 2009
4- 22
Code Sử Dụng Menu
private void btnNhan_Click(object sender, EventArgs e)
{
int intA;
double dblB;
intA = Convert.ToInt32(txtSoA.Text);
dblB= Convert.ToDouble(txtSoB.Text);
lblKQ.Text = HamTich(intA, dblB).ToString();
}
double HamTich(int A, Double dblB)
{
// lenh cua ham
return (A * dblB);
}
{
btnNhan_Click( sender, e);// chỉ cần gọi lại
//trong event btnNhan_Click đã có lệnh rồi
}
© 2009
4- 23
Windows Common
Dialog Boxes (dlg prefix)
– File Open and Saving
– Printing and Previewing
– Color selection
– Font selection
– Appears in the Component Tray, pane at
bottom of Form Designer where nondisplay
controls are shown
© 2009
4- 24
Color & Font Dialogs
Trang 9© 2009
4- 25
Common Dialog Controls
© 2009
4- 26
Displaying Common Dialog
common dialog at run time
doesn’t do anything else
dlgColor.ShowDialog( )
dlgFont.ShowDialog( )
dlgPrint.ShowDialog( )
Using the Common Dialog
Information
choices made by the user in the Common
dialog
– Color Dialog displayed
– User selects color and clicks OK
– Code must be written to apply the selected
(dlgColor.Color) color to an object(s)
Trang 10© 2009
4- 28
Set Initial Values for Color or
Font Common Dialogs
Common Dialog is displayed it normally
displays the current values of the object
being updated
you should therefore assign the Object's
existing values
© 2009
4- 29
– Changing the background color of a form
• Assign the current color to be selected when the
Color Dialog displays (otherwise black is selected)
– Changing the font of a label
• Assign the current font name, size, and style to be
selected when the Font Dialog displays
© 2009
4- 30
Kéo Thả Hộp Thoại
Trang 11© 2009
4- 31
Color Dialog Example
private void mnuTuyChinhKieuMau_Click(object sender,
EventArgs e)
{
// Initialize Color Dialog
dlgColor.Color = lblKQ.ForeColor;
// Display Color Dialog
dlgColor.ShowDialog();
//'Apply user choice to object
lblKQ.ForeColor = dlgColor.Color;
}
© 2009
4- 32
Font Dialog Example
private void mnuTuyChinhKieuFont_Click(object sender,
EventArgs e)
{
// Initialize font Dialog
dlgFont.Font = lblKQ.Font ;
// Display font Dialog
dlgFont.ShowDialog();
//'Apply user choice to object
lblKQ.Font = dlgFont.Font;
}
Context Menus
form or on a control
is pointing when right-clicking
bar
Trang 12© 2009
4- 34
Creating Context Menus
– Appears in the Component Tray, pane at
bottom of Form Designer where nondisplay
controls are shown
–Words "Context Menu" appear at the top of the
form
words "Type Here" appear underneath
© 2009
4- 35
Connecting Context Menu to
Object
• Use Context Menu's property window to give
it a standard name using the mnu prefix
• Modify the ContextMenu property of the
associated object so that this Context Menu
displays when user right-clicks the object
• If there is only one Context Menu connect it to
the form and it will display for the form and all
of the controls on the form
© 2009
4- 36
Context Menu
Contex menu