common dialogs. what is a common dialog/why use them? open dialog save file as dialog color...
TRANSCRIPT
Common Dialogs
What is a Common Dialog/Why use them? Open Dialog Save File As Dialog Color Dialog Font Dialog Print Dialog
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
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
Function returns a predefined constant, based on the button pressed:
Abort Cancel Ingnore No None OK Retry Yes
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 (*.*)|*.*”
You can set the initial directory that is displayed with the InitialDirectory propertyofd.OpenFile.InitialDirectory = “c:\users\marty\stonehill”
You can set the title of the Open Dialog with the Title property
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
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 propertycdColor.Color = Color.Blue
If cdColor.ShowDialog() = Windows.Forms.DialogResult.OK Then
MsgBox(“Color selected is: “ & cdColor.Color
End If
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 controlIf fdFont.ShowDialog() = Windows.Forms.DialogResult.OK Then
txtSomeTextBox.Font = fdFont.Font
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