• We just launched and are currently in beta. Join us as we build and grow the community.

Dialog Components in Visual Basic 2008

XxDat-WayxX

Clown Prince
X Rep
0
0
0
Rep
0
X Vouches
0
0
0
Vouches
0
Posts
155
Likes
35
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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.

dailogfirstform.png


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. 'CHECK THE SELECTED FILE IF IT EXIST OTHERWISE THE DIALOG BOX WILL DISPLAY A WARNING.
  5. .

    CheckFileExists

    =

    True


  6. 'CHECK THE SELECTED PATH IF IT EXIST OTHERWISE THE DIALOG BOX WILL DISPLAY A WARNING.
  7. .

    CheckPathExists

    =

    True


  8. 'GET AND SET THE DEFAULT EXTENSION
  9. .

    DefaultExt

    =

    "txt"


  10. 'RETURN THE FILE LINKED TO THE LNK FILE
  11. .

    DereferenceLinks

    =

    True

  12. 'SET THE FILE NAME TO EMPTY
  13. .

    FileName

    =

    ""

  14. 'FILTERING THE FILES
  15. .

    Filter

    =

    "Text files (*.txt)|*.txt|All files|*.*"

  16. 'SET THIS FOR ONE FILE SELECTION ONLY.
  17. .

    Multiselect

    =

    False



  18. 'SET THIS TO PUT THE CURRENT FOLDER BACK TO WHERE IT HAS STARTED.
  19. .

    RestoreDirectory

    =

    True

  20. 'SET THE TITLE OF THE DIALOG BOX.
  21. .

    Title

    =

    "Select a file to open"

  22. 'ACCEPT ONLY THE VALID WIN32 FILE NAMES.
  23. .

    ValidateNames

    =

    True

  24. If

    .

    ShowDialog

    =

    Windows.

    Forms

    .

    DialogResult

    .

    OK

    Then
  25. Try
  26. RichTextBox1.

    Text

    =

    My.

    Computer

    .

    FileSystem

    .

    ReadAllText

    (

    .

    FileName

    )
  27. Catch

    fileException As

    Exception
  28. Throw

    fileException
  29. End

    Try
  30. End

    If

  31. End

    With
  32. Catch

    ex As

    Exception
  33. MsgBox

    (

    ex.

    Message

    , MsgBoxStyle.

    Exclamation

    , Me

    .

    Text

    )
  34. End

    Try
  35. 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. 'CHECK IF THE OUTPUT PATH ACTUALLY EXISTS
  7. 'BEFORE CREATING A NEW FILE AND BEFORE OVERWRITING.
  8. 'THE FOLLOWING VALUES ARE IN ITS DEFUALT FORM.
  9. .

    CheckPathExists

    =

    True
  10. .

    CreatePrompt

    =

    False
  11. .

    OverwritePrompt

    =

    True
  12. .

    ValidateNames

    =

    True

  13. 'GET AND SET THE DEFAULT EXTENSION
  14. .

    DefaultExt

    =

    "txt"

  15. 'FILLTERING THE FILES THAT YOU HAVE SAVED.
  16. .

    Filter

    =

    "Text files (*.txt)|*.txt|"

    &

    "All files|*.*"
  17. .

    FilterIndex

    =

    1

  18. If

    .

    ShowDialog

    (

    )

    =

    Windows.

    Forms

    .

    DialogResult

    .

    OK

    Then
  19. My.

    Computer

    .

    FileSystem

    .

    WriteAllText

    (

    .

    FileName

    , RichTextBox1.

    Text

    , False

    )
  20. End

    If

  21. End

    With
  22. Catch

    ex As

    Exception
  23. MsgBox

    (

    ex.

    Message

    , MsgBoxStyle.

    Exclamation

    , Me

    .

    Text

    )
  24. End

    Try
  25. 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. Try
  5. With

    ColorDialog1

  6. 'SET THE COLORS THAT YOU HAVE SELECTED TO MATCH
  7. 'THE CURRENTLY COLORS THAT WAS USED IN THE RICHTEXTBOX.
  8. .

    Color

    =

    RichTextBox1.

    ForeColor

  9. 'SET THE ARRAY VARIABLE THAT YOU HAVE SUPPLIED
  10. 'AND FILL IT TO THE CUSTOM COLORS ON THE DIALOG BOX.
  11. .

    CustomColors

    =

    Custom_Colors


  12. 'IT ALLOWS YOU TO CREATE CUSTOM COLORS.
  13. .

    AllowFullOpen

    =

    True


  14. 'THE BASIC COLORS WILL DISPLAY.
  15. .

    AnyColor

    =

    True


  16. 'THE DIALOG BOX WILL DISPLAY WITH THE CUSTOM COLOR SETTINGS, SIDE OPEN, AS WELL.
  17. .

    FullOpen

    =

    False


  18. 'SOLID COLORS ARE ALLOWED.
  19. .

    SolidColorOnly

    =

    True

  20. If

    .

    ShowDialog

    (

    )

    =

    Windows.

    Forms

    .

    DialogResult

    .

    OK

    Then
  21. RichTextBox1.

    ForeColor

    =

    .

    Color
  22. 'STORE THE CUSTOM COLORS FOR FUTURE USE.
  23. Custom_Colors =

    .

    CustomColors
  24. End

    If


  25. 'RESET ALL THE COLORS IN THE DIALOG BOX.
  26. ColorDialog1.

    Reset

    (

    )

  27. End

    With


  28. Catch

    ex As

    Exception
  29. MsgBox

    (

    ex.

    Message

    , MsgBoxStyle.

    Exclamation

    , Me

    .

    Text

    )
  30. End

    Try
  31. 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. 'SET THE DIALOG BOX TO MATCH THE FONT THAT YOU USED IN THE RICHTEXTBOX.
  5. .

    Font

    =

    RichTextBox1.

    Font
  6. 'SET THE DEFAULT COLOR.
  7. .

    Color

    =

    RichTextBox1.

    ForeColor


  8. 'THE CHOICES OF THE COLORS WILL APPEAR.
  9. .

    ShowColor

    =

    True


  10. 'THE APPLY BUTTON APPEARS ON THE DIALOG BOX
  11. .

    ShowApply

    =

    True


  12. 'SET THE EFFECTS TO BE SELECTED.
  13. .

    ShowEffects

    =

    True


  14. 'DON'T ALLOW SIMULATION, VECTOR FONTS OR VERTICAL FONTS.
  15. ' THEN, LET THE USER CHANGE THE CHARACTER SET.
  16. .

    AllowScriptChange

    =

    True
  17. .

    AllowSimulations

    =

    False
  18. .

    AllowVectorFonts

    =

    False
  19. .

    AllowVerticalFonts

    =

    False


  20. 'SET THE FIXED AND PROPORTIONAL FONTS.
  21. .

    FixedPitchOnly

    =

    False
  22. 'ALLOW ONLY FONTS THAT EXIST.
  23. .

    FontMustExist

    =

    True
  24. 'SET THE MAXIMUM SIZE SELECTABLE FONT SIZE
  25. .

    MaxSize

    =

    48
  26. 'SET THE MINIMUM SIZE SELECTABLE FONT SIZE
  27. .

    MinSize

    =

    8

  28. 'SET UP THE REQUESTED FONTS.
  29. If

    .

    ShowDialog

    =

    Windows.

    Forms

    .

    DialogResult

    .

    OK

    Then
  30. RichTextBox1.

    Font

    =

    .

    Font
  31. End

    If

  32. End

    With
  33. Catch

    ex As

    Exception
  34. MsgBox

    (

    ex.

    Message

    , MsgBoxStyle.

    Exclamation

    , Me

    .

    Text

    )
  35. End

    Try
  36. End

    Sub


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top