Danh mục

Using Common Dialog Controls

Số trang: 7      Loại file: pdf      Dung lượng: 28.48 KB      Lượt xem: 1      Lượt tải: 0    
10.10.2023

Phí lưu trữ: miễn phí Tải xuống file đầy đủ (7 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Sử dụng thường Dialog Controls The Bell Ringers ứng dụng bây giờ cho phép bạn lưu thông tin, nhưng nó luôn luôn lưu dữ liệu vào cùng một tập tin, ghi đè lên bất cứ điều gì đã có. Ngoài ra, các chức năng in ấn vẫn còn mất tích. Bây giờ là thời gian để giải quyết những vấn đề này.
Nội dung trích xuất từ tài liệu:
Using Common Dialog Controls Using Common Dialog ControlsThe Bell Ringers application now allows you to save information, but it always savesdata to the same file, overwriting anything that is already there. Also, the printfunctionality is still missing. Now is the time to address these issues.There are a number of everyday tasks that require the user to specify some sort ofinformation. For example, if the user wants to print a file, the user is usually asked whichprinter to use, and the user can set additional properties such as the number of copies.You might have noticed that the same Print dialog box is used by many differentapplications. This is not due to lack of imagination by applications developers; it is justthat the requirement is so common that Microsoft has standardized it and made itavailable as a “common dialog”—a component supplied with the Microsoft Windowsoperating system that you can use in your own applications.There are a number of other common dialog boxes available as well, including dialogboxes for opening and saving files, selecting colors and fonts, specifying page formats,and performing print previews. You can use any of these common dialog boxes in VisualStudio 2005 through the common dialog controls.Using the SaveFileDialog ControlIn the following exercise, you will use the SaveFileDialog control. In the BellRingersapplication, when the user saves details to a file you will prompt the user for the nameand location of the file by using a SaveFileDialog control.Use a SaveFileDialog control 1. Display MemberForm in the Design View window. 2. In the Toolbox, expand the Dialogs category. 3. Drag a SaveFileDialog control onto the form. The control appears under the form and is given the name saveFileDialog1. 4. Click the saveFileDialog1 control. In the Properties window, set its properties by using the values specified in the following table. Property Value Description (Name) saveFileDialog The name of the control. Setting this property to True allows the dialog AddExtension True box to add the file extension indicated by the Property Value Description DefaultExt property to the name of the file specified by the user if the user omits the file extension. The default file extension to use if the user does DefaultExt txt not specify the extension when providing the filename. The name of the currently selected file. Delete FileName Leave blank the value if you dont want a file to be selected by default. The default directory to be used by the dialog InitialDirectory C:\ box. If this property is True, the user is warned when an attempt is made to overwrite an existing file OverwritePrompt True with the same name. For this to work, the ValidateNames property must also be set to True. A string that is displayed on the title bar of the Title Bell Ringers dialog box. This property indicates whether filenames are validated. It is used by some other properties, such as OverwritePrompt. If this property is set ValidateNames True to True, the dialog box also checks to verify that any filename typed in by the user contains only valid characters.5. In the Code And Text Editor window displaying MemberForm.cs, locate the saveMemberClick method at the end of the file.6. Type the following statements at the start of this method, surrounding the code that creates and uses the StreamWriter object:7. DialogResult buttonClicked = saveFileDialog.ShowDialog();8. if (buttonClicked.Equals(DialogResult.OK))9. {10. StreamWriter writer = new StreamWriter(Members.txt); // existing code11. ...12. MessageBox.Show(Member details saved, Saved); // existing code }13. The first statement displays the Save File dialog box by using the ShowDialog method. The Save File dialog box is modal, which means that the user cannot continue using any other forms in the application until she has closed this dialog box by clicking one of its buttons. Modal dialog boxes also have a DialogResult property that indicates which button the user clicked (the Save dialog has a Save button and a Cancel button). The ShowDialog method returns the value of this DialogResult property; if the user clicks Save, the DialogResult property will be OK (not Save because there is no such DialogResult value). IMPORTANT The SaveFileDialog control prompts the user for the name of a file to save to, but does not actually do any saving—you still have to supply that code yourself. 14. Modify the statement that creates the StreamWriter object: StreamWriter writer = new StreamWriter(saveFileDialog.FileName); The method will now write to the file specified by the user rather than Members.txt. 15. Build and run the application. Create a new member. On the File menu, click Save Member. The Bell Ringers dialog b ...

Tài liệu được xem nhiều: