SlideShare a Scribd company logo
1 of 5
Putting Dialog Boxes to Work

We will work with OpenFile, SaveFile, Font and Color Dialog's in this section. From the toolbox
drag a MainMenu component, RichTextBox control, Button Control, OpenFileDialog,
SaveFileDialog, FontDialog and ColorDialog onto the form. The sample code demonstrated
below allows you to select a file to be opened and displays it in the RichTextBox with
OpenFileDialog, allows you to save the text you enter in the RichTextBox to a location using the
SaveFileDialog, allows you to select a font and applies the selected font to text in the RTB
using FontDialog and allows you to select a color and applies the color to text in the RTB using
the ColorDialog. Select the MainMenu component and in the "Type Here" part of the MainMenu
type File and using the down arrow keys on the keyboard start typing Open, Save, SelectFont
and SelectColor under the File menu. It should look like this: File-> Open, Save, SelectFont,
SelectColor. We will assign OpenFileDialog to Open, SaveFileDialog to Save, FontDialog to
SelectFont and ColorDialog to SelectColor under File Menu. The form in design view should
look similar to the image below.




Before proceeding further you need to set properties for these dialogs in their properties window.
They are listed below.
For OpenFileDialog1, set the DefaultExt property to txt so that it opens text files, InitialDirectory
property to C:, RestoreDirectory propery to True and the Text property to Open File From.

For SaveFileDialog1, set the DefaultExt property to txt so that it saves files in text format,
InitialDirectory property to C: so that when you save a file, it first provides C: drive as the choice
of location, OverwritePrompt property to False, RestoreDirectory propery to True and the Text
property to Save File In.

For FontDialog1, set the AllowSimulations, AllowVectorFonts, AllowverticalFonts properties to
false, MaxSize to 50, MinSize to 5 and ShowApply and ShowColor properties to True.

For ColorDialog1, set AnyColor and SolidColorOnly properties to True.

Code

Imports System.IO
Public Class Form1 Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
RichTextBox1.Text = " "
'clears the text in richtextbox
End Sub

Private FileName As String
'declaring filename that will be selected
Dim sr As StreamReader
'streamreader is used to read text

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e
As_
System.EventArgs) Handles MenuItem2.Click
Try
With OpenFileDialog1
'With statement is used to execute statements using a particular object, here,_
'OpenFileDialog1
.Filter = "Text files (*.txt)|*.txt|" & "All files|*.*"
'setting filters so that Text files and All Files choice appears in the Files of
Type box
'in the dialog
If .ShowDialog() = DialogResult.OK Then
'showDialog method makes the dialog box visible at run time
FileName = .FileName
sr = New StreamReader(.OpenFile)
'using streamreader to read the opened text file
RichTextBox1.Text = sr.ReadToEnd()
'displaying text from streamreader in richtextbox
End If
End With
Catch es As Exception
MessageBox.Show(es.Message)
Finally
If Not (sr Is Nothing) Then
sr.Close()
End If
End Try
End Sub

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e
As_
System.EventArgs) Handles MenuItem3.Click
Dim sw As StreamWriter
'streamwriter is used to write text
Try
With SaveFileDialog1
.FileName = FileName
.Filter = "Text files (*.txt)|*.txt|" & "All files|*.*"
If .ShowDialog() = DialogResult.OK Then
FileName = .FileName
sw = New StreamWriter(FileName)
'using streamwriter to write text from richtextbox and saving it
sw.Write(RichTextBox1.Text)
End If
End With
Catch es As Exception
MessageBox.Show(es.Message)
Finally
If Not (sw Is Nothing) Then
sw.Close()
End If
End Try
End Sub

Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e
As_
System.EventArgs) Handles MenuItem4.Click
Try
With FontDialog1
.Font = RichTextBox1.Font
'initializing the dialog box to match the font used in the richtextbox
.Color = RichTextBox1.ForeColor
'default color is Black
If .ShowDialog = DialogResult.OK Then
setFont()
'calling a method setFont() to set the selected font and color
End If
End With
Catch es As Exception
MessageBox.Show(es.Message)
End Try
End Sub

Private Sub setFont()
Try
With FontDialog1
RichTextBox1.Font = .Font
If .ShowColor Then
RichTextBox1.ForeColor = .Color
'setting the color
End If
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As
_
System.EventArgs) Handles MenuItem5.Click
Static CustomColors() As Integer = {RGB(255, 0, 0), RGB(0, 255, 0),
RGB(0, 0, 255)}
'initializing CustomColors with an array of integers and putting Red, Green,
'and Blue in the custom colors section
Try
With ColorDialog1
.Color = RichTextBox1.ForeColor
'initializing the selected color to match the color currently used
'by the richtextbox's foreground color
.CustomColors = CustomColors
'filling custom colors on the dialog box with the array declared above
If .ShowDialog() = DialogResult.OK Then
RichTextBox1.ForeColor = .Color
CustomColors = .CustomColors
'Storing the custom colors to use again
End If
ColorDialog1.Reset()
'resetting all colors in the dialog box
End With
Catch es As Exception
MessageBox.Show(es.Message)
End Try
End Sub

End Class

More Related Content

Viewers also liked

A to z of stress management 1
A to z of stress management 1A to z of stress management 1
A to z of stress management 1pgujar
 
A day in the life of a future teacher
A day in the life of a future teacherA day in the life of a future teacher
A day in the life of a future teacherdanienem
 
Happy Birthday !!!
Happy Birthday !!!Happy Birthday !!!
Happy Birthday !!!Alin Armanca
 
ไขมัน
ไขมันไขมัน
ไขมันjunnanta
 
orkunn
orkunnorkunn
orkunnorkenn
 
Meta análise– veda1
Meta análise– veda1Meta análise– veda1
Meta análise– veda1souzadea1
 
Scrum gathering 2012 shanghai_ 敏捷测试与质量管理分会场演讲话题:快速可持续的高质量发布(路宁)
Scrum gathering 2012 shanghai_ 敏捷测试与质量管理分会场演讲话题:快速可持续的高质量发布(路宁)Scrum gathering 2012 shanghai_ 敏捷测试与质量管理分会场演讲话题:快速可持续的高质量发布(路宁)
Scrum gathering 2012 shanghai_ 敏捷测试与质量管理分会场演讲话题:快速可持续的高质量发布(路宁)jalamar
 
How to drink water for better heath
How to drink water for better heath How to drink water for better heath
How to drink water for better heath vikas_deshpande
 
White paper blackmoney2012
White paper blackmoney2012White paper blackmoney2012
White paper blackmoney2012vikas_deshpande
 

Viewers also liked (15)

A to z of stress management 1
A to z of stress management 1A to z of stress management 1
A to z of stress management 1
 
A day in the life of a future teacher
A day in the life of a future teacherA day in the life of a future teacher
A day in the life of a future teacher
 
Happy Birthday !!!
Happy Birthday !!!Happy Birthday !!!
Happy Birthday !!!
 
Arrive alive
Arrive aliveArrive alive
Arrive alive
 
ไขมัน
ไขมันไขมัน
ไขมัน
 
_
  _  _
_
 
Four squares
Four squaresFour squares
Four squares
 
Sanskrit bharati course
Sanskrit bharati courseSanskrit bharati course
Sanskrit bharati course
 
orkunn
orkunnorkunn
orkunn
 
Meta análise– veda1
Meta análise– veda1Meta análise– veda1
Meta análise– veda1
 
Scrum gathering 2012 shanghai_ 敏捷测试与质量管理分会场演讲话题:快速可持续的高质量发布(路宁)
Scrum gathering 2012 shanghai_ 敏捷测试与质量管理分会场演讲话题:快速可持续的高质量发布(路宁)Scrum gathering 2012 shanghai_ 敏捷测试与质量管理分会场演讲话题:快速可持续的高质量发布(路宁)
Scrum gathering 2012 shanghai_ 敏捷测试与质量管理分会场演讲话题:快速可持续的高质量发布(路宁)
 
How to drink water for better heath
How to drink water for better heath How to drink water for better heath
How to drink water for better heath
 
White paper blackmoney2012
White paper blackmoney2012White paper blackmoney2012
White paper blackmoney2012
 
AT89 S52
AT89 S52AT89 S52
AT89 S52
 
Factores inseguridad
Factores inseguridadFactores inseguridad
Factores inseguridad
 

Similar to Putting dialog boxes to work

Código Editor Net
Código Editor NetCódigo Editor Net
Código Editor Netcymbron
 
Delphi L05 Files and Dialogs
Delphi L05 Files and DialogsDelphi L05 Files and Dialogs
Delphi L05 Files and DialogsMohammad Shaker
 
Apply Bold, Italic and Underline to Selected Text in a RichtextBox using Visu...
Apply Bold, Italic and Underline to Selected Text in a RichtextBox using Visu...Apply Bold, Italic and Underline to Selected Text in a RichtextBox using Visu...
Apply Bold, Italic and Underline to Selected Text in a RichtextBox using Visu...Daniel DotNet
 
Windows Forms For Beginners Part 5
Windows Forms For Beginners Part 5Windows Forms For Beginners Part 5
Windows Forms For Beginners Part 5Bhushan Mulmule
 
CS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docx
CS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docxCS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docx
CS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docxannettsparrow
 
Module 3 open office writer
Module 3 open office writerModule 3 open office writer
Module 3 open office writerjaimemgarcia
 
Paste Only Plain Text in RichTextBox Control using Visual Basic.Net
Paste Only Plain Text in RichTextBox Control using Visual Basic.NetPaste Only Plain Text in RichTextBox Control using Visual Basic.Net
Paste Only Plain Text in RichTextBox Control using Visual Basic.NetDaniel DotNet
 
C#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docx
C#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docxC#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docx
C#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docxRAHUL126667
 
3.1 file input and output
3.1   file input and output3.1   file input and output
3.1 file input and outputallenbailey
 
Membuat aplikasi penjualan buku sederhana
Membuat aplikasi penjualan buku sederhanaMembuat aplikasi penjualan buku sederhana
Membuat aplikasi penjualan buku sederhanaYusman Kurniadi
 
Vb lecture notes
Vb lecture notesVb lecture notes
Vb lecture notesalldesign
 
TM 2nd qtr-3ndmeeting(java script-functions)
TM 2nd qtr-3ndmeeting(java script-functions)TM 2nd qtr-3ndmeeting(java script-functions)
TM 2nd qtr-3ndmeeting(java script-functions)Esmeraldo Jr Guimbarda
 

Similar to Putting dialog boxes to work (20)

Unit5
Unit5Unit5
Unit5
 
Filehandling
FilehandlingFilehandling
Filehandling
 
VB Dot net
VB Dot net VB Dot net
VB Dot net
 
Código Editor Net
Código Editor NetCódigo Editor Net
Código Editor Net
 
Delphi L05 Files and Dialogs
Delphi L05 Files and DialogsDelphi L05 Files and Dialogs
Delphi L05 Files and Dialogs
 
Apply Bold, Italic and Underline to Selected Text in a RichtextBox using Visu...
Apply Bold, Italic and Underline to Selected Text in a RichtextBox using Visu...Apply Bold, Italic and Underline to Selected Text in a RichtextBox using Visu...
Apply Bold, Italic and Underline to Selected Text in a RichtextBox using Visu...
 
Chapter10
Chapter10Chapter10
Chapter10
 
Windows Forms For Beginners Part 5
Windows Forms For Beginners Part 5Windows Forms For Beginners Part 5
Windows Forms For Beginners Part 5
 
CS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docx
CS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docxCS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docx
CS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docx
 
Module 3 open office writer
Module 3 open office writerModule 3 open office writer
Module 3 open office writer
 
Paste Only Plain Text in RichTextBox Control using Visual Basic.Net
Paste Only Plain Text in RichTextBox Control using Visual Basic.NetPaste Only Plain Text in RichTextBox Control using Visual Basic.Net
Paste Only Plain Text in RichTextBox Control using Visual Basic.Net
 
C#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docx
C#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docxC#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docx
C#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docx
 
3.1 file input and output
3.1   file input and output3.1   file input and output
3.1 file input and output
 
Database connectivity with data reader by varun tiwari
Database connectivity with data reader by varun tiwariDatabase connectivity with data reader by varun tiwari
Database connectivity with data reader by varun tiwari
 
Membuat aplikasi penjualan buku sederhana
Membuat aplikasi penjualan buku sederhanaMembuat aplikasi penjualan buku sederhana
Membuat aplikasi penjualan buku sederhana
 
Chapter 11
Chapter 11Chapter 11
Chapter 11
 
WPF Application
WPF ApplicationWPF Application
WPF Application
 
Vb lecture notes
Vb lecture notesVb lecture notes
Vb lecture notes
 
TM 2nd qtr-3ndmeeting(java script-functions)
TM 2nd qtr-3ndmeeting(java script-functions)TM 2nd qtr-3ndmeeting(java script-functions)
TM 2nd qtr-3ndmeeting(java script-functions)
 
Python IO
Python IOPython IO
Python IO
 

Recently uploaded

The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 

Recently uploaded (20)

The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 

Putting dialog boxes to work

  • 1. Putting Dialog Boxes to Work We will work with OpenFile, SaveFile, Font and Color Dialog's in this section. From the toolbox drag a MainMenu component, RichTextBox control, Button Control, OpenFileDialog, SaveFileDialog, FontDialog and ColorDialog onto the form. The sample code demonstrated below allows you to select a file to be opened and displays it in the RichTextBox with OpenFileDialog, allows you to save the text you enter in the RichTextBox to a location using the SaveFileDialog, allows you to select a font and applies the selected font to text in the RTB using FontDialog and allows you to select a color and applies the color to text in the RTB using the ColorDialog. Select the MainMenu component and in the "Type Here" part of the MainMenu type File and using the down arrow keys on the keyboard start typing Open, Save, SelectFont and SelectColor under the File menu. It should look like this: File-> Open, Save, SelectFont, SelectColor. We will assign OpenFileDialog to Open, SaveFileDialog to Save, FontDialog to SelectFont and ColorDialog to SelectColor under File Menu. The form in design view should look similar to the image below. Before proceeding further you need to set properties for these dialogs in their properties window. They are listed below.
  • 2. For OpenFileDialog1, set the DefaultExt property to txt so that it opens text files, InitialDirectory property to C:, RestoreDirectory propery to True and the Text property to Open File From. For SaveFileDialog1, set the DefaultExt property to txt so that it saves files in text format, InitialDirectory property to C: so that when you save a file, it first provides C: drive as the choice of location, OverwritePrompt property to False, RestoreDirectory propery to True and the Text property to Save File In. For FontDialog1, set the AllowSimulations, AllowVectorFonts, AllowverticalFonts properties to false, MaxSize to 50, MinSize to 5 and ShowApply and ShowColor properties to True. For ColorDialog1, set AnyColor and SolidColorOnly properties to True. Code Imports System.IO Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " #End Region Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_ System.EventArgs) Handles Button1.Click RichTextBox1.Text = " " 'clears the text in richtextbox End Sub Private FileName As String 'declaring filename that will be selected Dim sr As StreamReader 'streamreader is used to read text Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As_ System.EventArgs) Handles MenuItem2.Click Try With OpenFileDialog1 'With statement is used to execute statements using a particular object, here,_ 'OpenFileDialog1 .Filter = "Text files (*.txt)|*.txt|" & "All files|*.*" 'setting filters so that Text files and All Files choice appears in the Files of Type box 'in the dialog If .ShowDialog() = DialogResult.OK Then 'showDialog method makes the dialog box visible at run time
  • 3. FileName = .FileName sr = New StreamReader(.OpenFile) 'using streamreader to read the opened text file RichTextBox1.Text = sr.ReadToEnd() 'displaying text from streamreader in richtextbox End If End With Catch es As Exception MessageBox.Show(es.Message) Finally If Not (sr Is Nothing) Then sr.Close() End If End Try End Sub Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As_ System.EventArgs) Handles MenuItem3.Click Dim sw As StreamWriter 'streamwriter is used to write text Try With SaveFileDialog1 .FileName = FileName .Filter = "Text files (*.txt)|*.txt|" & "All files|*.*" If .ShowDialog() = DialogResult.OK Then FileName = .FileName sw = New StreamWriter(FileName) 'using streamwriter to write text from richtextbox and saving it sw.Write(RichTextBox1.Text) End If End With Catch es As Exception MessageBox.Show(es.Message) Finally If Not (sw Is Nothing) Then sw.Close() End If End Try End Sub Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As_ System.EventArgs) Handles MenuItem4.Click Try With FontDialog1
  • 4. .Font = RichTextBox1.Font 'initializing the dialog box to match the font used in the richtextbox .Color = RichTextBox1.ForeColor 'default color is Black If .ShowDialog = DialogResult.OK Then setFont() 'calling a method setFont() to set the selected font and color End If End With Catch es As Exception MessageBox.Show(es.Message) End Try End Sub Private Sub setFont() Try With FontDialog1 RichTextBox1.Font = .Font If .ShowColor Then RichTextBox1.ForeColor = .Color 'setting the color End If End With Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles MenuItem5.Click Static CustomColors() As Integer = {RGB(255, 0, 0), RGB(0, 255, 0), RGB(0, 0, 255)} 'initializing CustomColors with an array of integers and putting Red, Green, 'and Blue in the custom colors section Try With ColorDialog1 .Color = RichTextBox1.ForeColor 'initializing the selected color to match the color currently used 'by the richtextbox's foreground color .CustomColors = CustomColors 'filling custom colors on the dialog box with the array declared above If .ShowDialog() = DialogResult.OK Then RichTextBox1.ForeColor = .Color CustomColors = .CustomColors 'Storing the custom colors to use again
  • 5. End If ColorDialog1.Reset() 'resetting all colors in the dialog box End With Catch es As Exception MessageBox.Show(es.Message) End Try End Sub End Class