Putting dialog boxes to work

425 views

Published on

bcv

Published in: Technology, Art & Photos
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
425
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Putting dialog boxes to work

  1. 1. Putting Dialog Boxes to WorkWe will work with OpenFile, SaveFile, Font and Color Dialogs in this section. From the toolboxdrag a MainMenu component, RichTextBox control, Button Control, OpenFileDialog,SaveFileDialog, FontDialog and ColorDialog onto the form. The sample code demonstratedbelow allows you to select a file to be opened and displays it in the RichTextBox withOpenFileDialog, allows you to save the text you enter in the RichTextBox to a location using theSaveFileDialog, allows you to select a font and applies the selected font to text in the RTBusing FontDialog and allows you to select a color and applies the color to text in the RTB usingthe ColorDialog. Select the MainMenu component and in the "Type Here" part of the MainMenutype File and using the down arrow keys on the keyboard start typing Open, Save, SelectFontand 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 toSelectFont and ColorDialog to SelectColor under File Menu. The form in design view shouldlook 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. 2. For OpenFileDialog1, set the DefaultExt property to txt so that it opens text files, InitialDirectoryproperty 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 choiceof location, OverwritePrompt property to False, RestoreDirectory propery to True and the Textproperty to Save File In.For FontDialog1, set the AllowSimulations, AllowVectorFonts, AllowverticalFonts properties tofalse, MaxSize to 50, MinSize to 5 and ShowApply and ShowColor properties to True.For ColorDialog1, set AnyColor and SolidColorOnly properties to True.CodeImports System.IOPublic Class Form1 Inherits System.Windows.Forms.Form#Region " Windows Form Designer generated code "#End RegionPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As_System.EventArgs) Handles Button1.ClickRichTextBox1.Text = " "clears the text in richtextboxEnd SubPrivate FileName As Stringdeclaring filename that will be selectedDim sr As StreamReaderstreamreader is used to read textPrivate Sub MenuItem2_Click(ByVal sender As System.Object, ByVal eAs_System.EventArgs) Handles MenuItem2.ClickTryWith OpenFileDialog1With 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 ofType boxin the dialogIf .ShowDialog() = DialogResult.OK ThenshowDialog method makes the dialog box visible at run time
  3. 3. FileName = .FileNamesr = New StreamReader(.OpenFile)using streamreader to read the opened text fileRichTextBox1.Text = sr.ReadToEnd()displaying text from streamreader in richtextboxEnd IfEnd WithCatch es As ExceptionMessageBox.Show(es.Message)FinallyIf Not (sr Is Nothing) Thensr.Close()End IfEnd TryEnd SubPrivate Sub MenuItem3_Click(ByVal sender As System.Object, ByVal eAs_System.EventArgs) Handles MenuItem3.ClickDim sw As StreamWriterstreamwriter is used to write textTryWith SaveFileDialog1.FileName = FileName.Filter = "Text files (*.txt)|*.txt|" & "All files|*.*"If .ShowDialog() = DialogResult.OK ThenFileName = .FileNamesw = New StreamWriter(FileName)using streamwriter to write text from richtextbox and saving itsw.Write(RichTextBox1.Text)End IfEnd WithCatch es As ExceptionMessageBox.Show(es.Message)FinallyIf Not (sw Is Nothing) Thensw.Close()End IfEnd TryEnd SubPrivate Sub MenuItem4_Click(ByVal sender As System.Object, ByVal eAs_System.EventArgs) Handles MenuItem4.ClickTryWith FontDialog1
  4. 4. .Font = RichTextBox1.Fontinitializing the dialog box to match the font used in the richtextbox.Color = RichTextBox1.ForeColordefault color is BlackIf .ShowDialog = DialogResult.OK ThensetFont()calling a method setFont() to set the selected font and colorEnd IfEnd WithCatch es As ExceptionMessageBox.Show(es.Message)End TryEnd SubPrivate Sub setFont()TryWith FontDialog1RichTextBox1.Font = .FontIf .ShowColor ThenRichTextBox1.ForeColor = .Colorsetting the colorEnd IfEnd WithCatch ex As ExceptionMessageBox.Show(ex.Message)End TryEnd SubPrivate Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As_System.EventArgs) Handles MenuItem5.ClickStatic 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 sectionTryWith ColorDialog1.Color = RichTextBox1.ForeColorinitializing the selected color to match the color currently usedby the richtextboxs foreground color.CustomColors = CustomColorsfilling custom colors on the dialog box with the array declared aboveIf .ShowDialog() = DialogResult.OK ThenRichTextBox1.ForeColor = .ColorCustomColors = .CustomColorsStoring the custom colors to use again
  5. 5. End IfColorDialog1.Reset()resetting all colors in the dialog boxEnd WithCatch es As ExceptionMessageBox.Show(es.Message)End TryEnd SubEnd Class

×