DotNetWebTutor.com                                                    Link to Us

common dialogs
most windows programs have the same dialogs
because there are many common dialogs built into windows
showdialog() opens the dialog at runtime

common dialog classes

  • colordialog
  • openfiledialog
  • savefiledialog
  • folderbrowserdialog
  • fontdialog
  • pagesetupdialog
  • printdialog
  • printpreviewdialog

Color Dialog
Dim colorChoices as new colordialog()
if colorChoices.showdialog()=dialogresult.ok then
Me.backColor= colorChoices.color
end if

You can setallowfullopen for false to disallow custom colors

Showhelp to true to allow them to invoke help with f1, if you do this you have to write code to handle the help request

Openfiledialog
Savefiledialog
Both have a filter string
This allows you to only allow certain file extensions

common dialog example
common dialog example

  • start a new project
  • add a label with text in it
  • add 3 buttons
  • file
  • font
  • color
  • Change their text property to identify them
  • drop openfiledialog, fontdialog and colordialog controls onto the form

colordialog fontdialog openfiledialog
They are non-visible controls so they appear below the form
openfiledialog

  • add this code to the file button

if openfiledialog1.showdialog()=windows.forms.dialogresult.ok then
MessageBox.Show("you selected" + OpenFileDialog1.FileName)
end if

  • add this code to the font button

if fontdialog1.showdialog()=windows.forms.dialogresult.ok then
MessageBox.Show("You selected " & FontDialog1.Font.ToString)
end if

  • add to the color button

if colordialog1.showdialog()=windows.forms.dialogresult.ok then
me.backcolor=colordialog1.color
end if

  • run it

visual basic example
Example of getting more than one file:
Dim myDialog as new fileopendialog()
Mydialog.filter=Image Files(*.bmp;*.jpg;*.gif)| _
*.bmp;*.jpg;*.gif” & “|All files (*.*)|*.*”
Mydialog.checkfileexists=true
Mydialog.multiselect=true
If mydialog.showDialog()=dialogresult.ok then
Dim selectedfiles as string=””
For each file as string in mydialog.filenames
Selectedfiles &=file & “”
Next
lblDisplay.text=”You chose:” & SelectedFiles
end if