110 likes | 122 Views
Learn how to utilize Open Dialog, Save File As Dialog, Color Dialog, Font Dialog, and Print Dialog in C# for a consistent and easy user interface experience. Practice and understand the common properties and methods available. Enhance your programming skills with these versatile tools!
E N D
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 • 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
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
Open Dialog • Function returns a predefined constant, based on the button pressed: • Abort • Cancel • Ingnore • No • None • OK • Retry • Yes
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 (*.*)|*.*”
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
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
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
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
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