Chapter 4 Menus, Functions And Common Dialog

12 284 0
Chapter 4 Menus, Functions And Common Dialog

Đ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

1 Chapter 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. ©2009 4-3 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 } Vídụ: long Tong(int a, int b) { long lngS; lngS= a+b; return lngS ; } 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 } Vídụ: static void sapXep(int a, int b) { 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. 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 ©2009 4-9 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; } 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 • To create the menus simply type where the words "Type Here" appear at the top of the form • Include & symbol as you type to indicate Keyboard Access Keys • You are actually entering the Text property for aMenuItemobject • ChangeMenuItemobject names in the Properties Window to include mnu prefix 5 ©2009 4-13 Submenus • Popup to the right of the menu • Filled triangle to the right of the menu item indicates to the user the existence of a submenu • Avoid more than one level deep • Create submenus by moving to the right of a menu item and typing the next item's text ©2009 4-14 SubMenus (cont.) ©2009 4-15 Separator Bars • Used for grouping menu items according to their purpose • Visually represented as a bar across the menu • Create using one of two methods – Typing a single hyphen for the text – Right-click on Menu Designer where you want separator bar and choose Insert Separator 6 ©2009 4-16 Menu Properties • Text • Name, prefix =mnu – Examples:mnuFileExit,mnuHelpAbout, mnuFormatColorRed • Checked, True/False (see coding tip p 204) • Enabled, True/False • Visible, True/False ©2009 4-17 Menu Design Standards • Follow the industry standards for Windows for names, order/location, access keys, shortcut keys • Basic Main Menus File Edit View Format Help ©2009 4-18 File Menu Edit Menu • New (Ctrl N) • Open (Ctrl O) • Close • Save As • Save (Ctrl S) • Print (Ctrl P) • Exit • Undo (Ctrl Z) • Cut (Ctrl X) • Copy (Ctrl C) • Paste (Ctrl V) • Find (Ctrl F) • Replace (Ctrl H) 7 ©2009 4-19 Format Menu Help Menu • Font • Paragraph • Alignment • Color • About (F1) • System Information View Menu • Toolbar • Status Bar ©2009 4-20 Modifying Menu Items Using Menu Designer • Right-Click the Menu Bar on the Form to – Insert New menu – Delete menu – Insert Separator – Edit Name, displays menu item Name property rather than Text property on Form • Drag and Drop menu items to new locations ©2009 4-21 VídụQua Menu Submenu Menu Items 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); } private void mnuPhepTinhNhan_Click(object sender, EventArgs e) { 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 (dlgprefix) • Predefined standard dialog boxes for: – File Open and Saving – Printing and Previewing – Color selection – Font selection • Add the Common Dialog control to form – Appears in the Component Tray, pane at bottom of Form Designer wherenondisplay controls are shown ©2009 4-24 Color & Font Dialogs 9 ©2009 4-25 Common Dialog Controls • OpenFileDialog • SaveFileDialog • FontDialog • ColorDialog • PrintDialog • PrintPreviewDialog ©2009 4-26 Displaying Common Dialog • UseShowDialogMethod to display common dialog at run time • ShowDialogonly displays the dialog, it doesn’t do anything else dlgColor.ShowDialog( ) dlgFont.ShowDialog( ) dlgPrint.ShowDialog( ) ©2009 4-27 Using the Common Dialog Information • Code must be written to retrieve and use the choices made by the user in the Common dialog • Example – Color Dialog displayed – User selects color and clicks OK – Code must be written to apply the selected (dlgColor.Color) color to an object(s) 10 ©2009 4-28 Set Initial Values for Color or Font Common Dialogs • In Windows, when a Color or Font Common Dialog is displayed it normally displays the current values of the object being updated • Before executing theShowDialogmethod, you should therefore assign the Object's existing values ©2009 4-29 Set Initial Values (cont.) • Examples – 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 [...]...Color Dialog Example • Change background color of a Label 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; } 4- 31 © 2009 Font Dialog Example • Change font of a Label private... 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 4- 35 © 2009 Context Menu Contex menu 4- 36 © 2009 12 ... top level menus like the menu bar 4- 33 © 2009 11 Creating Context Menus • Add ContextMenu control – 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 • Click on the words "Context Menu" and the words "Type Here" appear underneath • Proceed as you did for Menu Bar 4- 34 © 2009 Connecting Context Menu... Font Dialog Example • Change font of a Label 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; } 4- 32 © 2009 Context Menus • Popup in response to right mouse click on form or on a control • Are specific to the component to which . Font Dialogs 9 ©2009 4-25 Common Dialog Controls • OpenFileDialog • SaveFileDialog • FontDialog • ColorDialog • PrintDialog • PrintPreviewDialog ©2009 4-26 Displaying Common Dialog • UseShowDialogMethod. common dialog at run time • ShowDialogonly displays the dialog, it doesn’t do anything else dlgColor.ShowDialog( ) dlgFont.ShowDialog( ) dlgPrint.ShowDialog( ) ©2009 4-27 Using the Common Dialog. right-clicking • Also called Popup menus or Shortcut menus • Do not have top level menus like the menu bar 12 ©2009 4-34 Creating Context Menus • AddContextMenucontrol – Appears in the Component

Ngày đăng: 13/05/2014, 11:31

Từ khóa liên quan

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

Tài liệu liên quan