Download presentation
Presentation is loading. Please wait.
Published byDustin Haynes Modified over 9 years ago
1
Common Dialogs
2
What is a Common Dialog/Why use them? Open Dialog Save File As Dialog Color Dialog Font Dialog Print Dialog
3
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
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
Function returns a predefined constant, based on the button pressed: Abort Cancel Ingnore No None OK Retry Yes
6
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
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
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
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
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
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.