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

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 

Recently uploaded (20)

The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 

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