1 / 11

CS 102

CS 102. Common Dialogs. Overview. What is a Common Dialog/Why use them? Open Dialog Save File As Dialog Color Dialog Font Dialog Print Dialog. Common Dialogs. A “Common” dialog is one that you will use often, and are familiar with Save programmers work

erasto
Télécharger la présentation

CS 102

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 102 Common Dialogs

  2. Overview • What is a Common Dialog/Why use them? • Open Dialog • Save File As Dialog • Color Dialog • Font Dialog • Print Dialog

  3. Common Dialogs • A “Common” dialog is one that you will use often, and are familiar with • Save programmers work • Help users with consistent interface • Common dialogs are provided by Windows • Look/feel the same way for every application • Common properties • Easy to modify • Many available methods • Can override default behavior

  4. Open Dialog • Used when you need to open a file • Drag/drop Open File control from the toolbox • Does not show on form, but in control tray • Display by calling on ShowDialog method: If ofdOpenFile.ShowDialog() = Windows.Forms.DialogResult.OK Then MessageBox(“The file is: “ & ofdOpenFile.FileName) Else MessageBox(“No file was selected.”) End If

  5. Open Dialog • Function returns a predefined constant, based on the button pressed: • Abort • Cancel • Ingnore • No • None • OK • Retry • Yes

  6. Filter Property • Other properties on the control: • Filter property. You can set the initial filter. • Filter is a string with two parts • The text to show for the filter type • The file type • You can have multiple filters • Separate the filters with a pipe (“|”) • Example: ofdOpenFile.Filter = “Text files (*.txt)|*.txt|All files (*.*)|*.*”

  7. Other Key Properties • You can set the initial directory that is displayed with the InitialDirectory property ofd.OpenFile.InitialDirectory = “c:\users\marty\stonehill” • You can set the title of the Open Dialog with the Title property

  8. Save File (As) Dialog • Used to save a file to a file name • Drag a Save File Dialog to the form • The InitialDirectory, Title, Filter properties are EXACTLY the same as the are for the Open Dialog • When you have the name of the file to save, you can open the file with normal file operations

  9. ColorDialog • You can choose colors for your program at run time with the ColorDialog control • It is dragged to the form, modified, and opened exactly the same as the other common dialogs • You can set the initial color by setting the .Color property • If the user presses OK, the dialog returns the color selected in the .Color property cdColor.Color = Color.Blue If cdColor.ShowDialog() = Windows.Forms.DialogResult.OK Then MsgBox(“Color selected is: “ & cdColor.Color End If

  10. FontControl • Same process as all other common controls • The results are returned in the .Font property • You can set the initial value with the .Font property • Then use that selected font by setting it for another control If fdFont.ShowDialog() = Windows.Forms.DialogResult.OK Then txtSomeTextBox.Font = fdFont.Font

  11. PrintDocument • You can drag the Print Document Control to your form • Print by calling on the .Print method for the common control • This common dialog is a bit more complicated • When you tell the program to print, it calls on a PrintPage event to print the page • You must write the code in the print page event so that the page gets printed • See section 9.3 for more details

More Related