common dialogs. what is a common dialog/why use them? open dialog save file as dialog color...

11
Common Dialogs

Upload: dustin-haynes

Post on 13-Dec-2015

282 views

Category:

Documents


11 download

TRANSCRIPT

Page 1: Common Dialogs.  What is a Common Dialog/Why use them?  Open Dialog  Save File As Dialog  Color Dialog  Font Dialog  Print Dialog

Common Dialogs

Page 2: Common Dialogs.  What is a Common Dialog/Why use them?  Open Dialog  Save File As Dialog  Color Dialog  Font Dialog  Print Dialog

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

Page 3: 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

Page 4: Common Dialogs.  What is a Common Dialog/Why use them?  Open Dialog  Save File As Dialog  Color Dialog  Font Dialog  Print 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

Page 5: Common Dialogs.  What is a Common Dialog/Why use them?  Open Dialog  Save File As Dialog  Color Dialog  Font Dialog  Print Dialog

Function returns a predefined constant, based on the button pressed:

Abort Cancel Ingnore No None OK Retry Yes

Page 6: Common Dialogs.  What is a Common Dialog/Why use them?  Open Dialog  Save File As Dialog  Color Dialog  Font Dialog  Print Dialog

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 (*.*)|*.*”

Page 7: Common Dialogs.  What is a Common Dialog/Why use them?  Open Dialog  Save File As Dialog  Color Dialog  Font Dialog  Print Dialog

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

Page 8: Common Dialogs.  What is a Common Dialog/Why use them?  Open Dialog  Save File As Dialog  Color Dialog  Font Dialog  Print 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

Page 9: Common Dialogs.  What is a Common Dialog/Why use them?  Open Dialog  Save File As Dialog  Color Dialog  Font Dialog  Print Dialog

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

Page 10: Common Dialogs.  What is a Common Dialog/Why use them?  Open Dialog  Save File As Dialog  Color Dialog  Font Dialog  Print Dialog

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

Page 11: Common Dialogs.  What is a Common Dialog/Why use them?  Open Dialog  Save File As Dialog  Color Dialog  Font Dialog  Print Dialog

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