Dialog Components in Visual Basic 2008

Today, I will teach you how to open a file, save a file, select a color and font of the corresponding text in the RichTextBox in Visual Basic 2008. With this, you can save the file, open the file that you have created and you can change the color and the font of the text in the RichTextBox. Let’s Begin: 1.Open Visual Studio. 2.Create a new Project. 3.Set the Form just like this. First Form Dialog Go to the code view and do this following code for the “Open File” Button. This method serves to open the .txt file and the content will appear in the RichTextBox.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Try
  3.             With OpenFileDialog1
  4.  
  5.                 'CHECK THE SELECTED FILE IF IT EXIST OTHERWISE THE DIALOG BOX WILL DISPLAY A WARNING.
  6.                 .CheckFileExists = True
  7.  
  8.              
  9.                 'CHECK THE SELECTED PATH IF IT EXIST OTHERWISE THE DIALOG BOX WILL DISPLAY A WARNING.
  10.                 .CheckPathExists = True
  11.  
  12.  
  13.                 'GET AND SET THE DEFAULT EXTENSION
  14.                 .DefaultExt = "txt"
  15.  
  16.            
  17.                 'RETURN THE FILE LINKED TO THE LNK FILE
  18.                 .DereferenceLinks = True
  19.  
  20.                 'SET THE FILE NAME TO EMPTY
  21.                 .FileName = ""
  22.  
  23.                 'FILTERING THE FILES
  24.                 .Filter = "Text files (*.txt)|*.txt|All files|*.*"
  25.  
  26.                 'SET THIS FOR ONE FILE SELECTION ONLY.
  27.                 .Multiselect = False
  28.  
  29.                
  30.  
  31.                 'SET THIS TO PUT THE CURRENT FOLDER BACK TO WHERE IT HAS STARTED.
  32.                 .RestoreDirectory = True
  33.  
  34.                 'SET THE TITLE OF THE DIALOG BOX.
  35.                 .Title = "Select a file to open"
  36.  
  37.                 'ACCEPT ONLY THE VALID WIN32 FILE NAMES.
  38.                 .ValidateNames = True
  39.  
  40.                 If .ShowDialog = Windows.Forms.DialogResult.OK Then
  41.                     Try
  42.                         RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(.FileName)
  43.                     Catch fileException As Exception
  44.                         Throw fileException
  45.                     End Try
  46.                 End If
  47.  
  48.             End With
  49.         Catch ex As Exception
  50.             MsgBox(ex.Message, MsgBoxStyle.Exclamation, Me.Text)
  51.         End Try
  52.     End Sub
This code is for the “Save File” Button. This method serves to save the text that you have created in the RichTextBox.
  1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2.         Try
  3.             With SaveFileDialog1
  4.                  'IF THE USER NEGLECTS THE FILE ETENSION THEN, ADD THE DEFUALT EXTENSION.
  5.                 .AddExtension = True
  6.  
  7.                
  8.                 'CHECK IF THE OUTPUT PATH ACTUALLY EXISTS
  9.                 'BEFORE CREATING A NEW FILE AND BEFORE OVERWRITING.
  10.                 'THE FOLLOWING VALUES ARE IN ITS DEFUALT FORM.
  11.                 .CheckPathExists = True
  12.                 .CreatePrompt = False
  13.                 .OverwritePrompt = True
  14.                 .ValidateNames = True
  15.              
  16.                 'GET AND SET THE DEFAULT EXTENSION
  17.                 .DefaultExt = "txt"
  18.  
  19.                 'FILLTERING THE FILES THAT YOU HAVE SAVED.
  20.                 .Filter = "Text files (*.txt)|*.txt|" & "All files|*.*"
  21.                 .FilterIndex = 1
  22.  
  23.                 If .ShowDialog() = Windows.Forms.DialogResult.OK Then
  24.                     My.Computer.FileSystem.WriteAllText(.FileName, RichTextBox1.Text, False)
  25.                 End If
  26.  
  27.             End With
  28.         Catch ex As Exception
  29.             MsgBox(ex.Message, MsgBoxStyle.Exclamation, Me.Text)
  30.         End Try
  31.     End Sub
After that, do this following code for changing the color of the text in the RichTextBox.
  1. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  2.         'SET THE RED, GREEN, AND BLUE IN THE CUSTOM COLORS SELECTION
  3.         Static Custom_Colors() As Integer = {RGB(255, 0, 0), RGB(0, 255, 0), RGB(0, 0, 255)}
  4.  
  5.         Try
  6.             With ColorDialog1
  7.                
  8.                 'SET THE COLORS THAT YOU HAVE SELECTED TO MATCH
  9.                 'THE CURRENTLY COLORS THAT WAS USED IN THE RICHTEXTBOX.
  10.                 .Color = RichTextBox1.ForeColor
  11.  
  12.                 'SET THE ARRAY VARIABLE THAT YOU HAVE SUPPLIED
  13.                 'AND FILL IT TO THE CUSTOM COLORS ON THE DIALOG BOX.
  14.                 .CustomColors = Custom_Colors
  15.  
  16.  
  17.                 'IT ALLOWS YOU TO CREATE CUSTOM COLORS.
  18.                 .AllowFullOpen = True
  19.  
  20.            
  21.                 'THE BASIC COLORS WILL DISPLAY.
  22.                 .AnyColor = True
  23.  
  24.    
  25.                 'THE DIALOG BOX WILL DISPLAY WITH THE CUSTOM COLOR SETTINGS, SIDE OPEN, AS WELL.
  26.                 .FullOpen = False
  27.  
  28.                  
  29.                 'SOLID COLORS ARE ALLOWED.
  30.                 .SolidColorOnly = True
  31.  
  32.                 If .ShowDialog() = Windows.Forms.DialogResult.OK Then
  33.                     RichTextBox1.ForeColor = .Color  
  34.                     'STORE THE CUSTOM COLORS FOR FUTURE USE.
  35.                     Custom_Colors = .CustomColors
  36.                 End If
  37.  
  38.                
  39.                 'RESET ALL THE COLORS IN THE DIALOG BOX.
  40.                 ColorDialog1.Reset()
  41.  
  42.             End With
  43.  
  44.  
  45.         Catch ex As Exception
  46.             MsgBox(ex.Message, MsgBoxStyle.Exclamation, Me.Text)
  47.         End Try
  48.     End Sub
Lastly, do this following code for changing the font of the text in the RichTetBox.
  1. Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  2.         Try
  3.             With FontDialog1
  4.      
  5.                 'SET THE DIALOG BOX TO MATCH THE FONT THAT YOU USED IN THE RICHTEXTBOX.
  6.                 .Font = RichTextBox1.Font
  7.                 'SET THE DEFAULT COLOR.
  8.                 .Color = RichTextBox1.ForeColor
  9.  
  10.          
  11.                 'THE CHOICES OF THE COLORS WILL APPEAR.
  12.                 .ShowColor = True
  13.  
  14.              
  15.                 'THE APPLY BUTTON APPEARS ON THE DIALOG BOX
  16.                 .ShowApply = True
  17.  
  18.              
  19.                 'SET THE EFFECTS TO BE SELECTED.
  20.                 .ShowEffects = True
  21.  
  22.                
  23.                 'DON'T ALLOW SIMULATION, VECTOR FONTS OR VERTICAL FONTS.
  24.                 ' THEN, LET THE USER CHANGE THE CHARACTER SET.
  25.                 .AllowScriptChange = True
  26.                 .AllowSimulations = False
  27.                 .AllowVectorFonts = False
  28.                 .AllowVerticalFonts = False
  29.  
  30.              
  31.                 'SET THE FIXED AND PROPORTIONAL FONTS.
  32.                 .FixedPitchOnly = False
  33.                 'ALLOW ONLY FONTS THAT EXIST.
  34.                 .FontMustExist = True
  35.                 'SET THE MAXIMUM SIZE SELECTABLE FONT SIZE
  36.                 .MaxSize = 48
  37.                 'SET THE MINIMUM SIZE SELECTABLE FONT SIZE
  38.                 .MinSize = 8
  39.  
  40.                 'SET UP THE REQUESTED FONTS.
  41.                 If .ShowDialog = Windows.Forms.DialogResult.OK Then
  42.                     RichTextBox1.Font = .Font
  43.                 End If
  44.  
  45.             End With
  46.         Catch ex As Exception
  47.             MsgBox(ex.Message, MsgBoxStyle.Exclamation, Me.Text)
  48.         End Try
  49.     End Sub

Add new comment