Examples of Dialog Controls in VB.NET

Related Articles

This article illustrates some examples of dialog controls on VB.NET. As you can see, the example below shows the use of ColorDialog, FontDialog and SaveFileDialog.

Change the BackColor and ForeColor of text using ColorDialog

To use the ColorDialog control, create its object and call the ShowDialog () method. for example, Color CD as new as ColorDialog

cd.ShowDialog ()

Additionally, set the BackColor, ForeColor, or SelectionColor properties of the RichTextBox control using the Color property of the ColorDialog control. for example, RichTextBox1.BackColor = cd.Color

Using the FontDialog control

Similarly, we can also use the FontDialog control. First, create an object from it. for example, Dim ft As New FontDialog. Then, call the ShowDialog () method. for example, ft.ShowDialog (). Now select the font you want. Finally, set the Font property of the RichTextBox control. for example, RichTextBox1.Font = ft.Font.

Save a file using SaveFileDialog Control

In order to save a file, we can perform the steps outlined below.

  1. First, drag the RichTextBox control on the form. Also put the Buuton control in the form.
  2. Then double-click the button to create the event handler of the click event.
  3. To save the text that RichTextBox contains in a text file, create an object of the SaveFileDialog control. for example Dim sv As New SaveFileDialog.
  4. Also create a String object to hold the file name. for example, Dim fn as a string.
  5. Additionally, set some properties of the SaveFileDialog control. For examplev.Filter = “Text files (* .txt) | * .txt” and sv.Title = “Save File”.
  6. Then read the ShowDialog () method to open the Save File dialog box. for example, sv.ShowDialog ().
  7. I will now retrieve the file name in the string variable. For example, fn = sv.FileName
  8. To save the file, create a StreamWriter object. for example, Object Writer as New System.IO.StreamWriter (fn)
  9. After that read the Write () method. for example, objwriter.Write (RichTextBox1.Text).
  10. Finally, close the writer. for example, objwriter.close ()

The following code shows the complete example.

Form design

An example of dialogue control
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim cd As New ColorDialog
        RichTextBox1.BackColor = cd.Color

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim cf As New ColorDialog
        RichTextBox1.ForeColor = cf.Color
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim cn As New ColorDialog
        RichTextBox1.SelectionColor = cn.Color
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim ft As New FontDialog
        RichTextBox1.Font = ft.Font
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim sv As New SaveFileDialog
        Dim fn As String
        sv.Filter = "TXT Files (*.txt)|*.txt"
        sv.Title = "Save File"

        fn = sv.FileName
        Dim objwriter As New System.IO.StreamWriter(fn)
    End Sub
End Class


Output code samples of dialog controls on VB.NET
Output code samples of dialog controls on VB.NET



Please enter your comment!
Please enter your name here

Popular Articles