VB.NET

Examples of Dialog Controls in VB.NET

Programmingempire

This article demonstrates some Examples of Dialog Controls in VB.NET. As can be seen, the example given below shows the usage of ColorDialog, FontDialog, and SaveFileDialog.

Changing the BackColor and ForeColor of the text using ColorDialog

In order to use the ColorDialog control, create an object of it and call the ShowDialog() method. For example, Dim cd As New ColorDialog

cd.ShowDialog()

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

Using the FontDialog Control

Similary, we can also use the FontDialog control. At first, create an object of it. For example, Dim ft As New FontDialog. After that, call the ShowDialog() method. For instance, ft.ShowDialog(). Now select the desired font. Finally, set the Font property of the RichTextBox control. For example, RichTextBox1.Font=ft.Font.

Saving a File Using the SaveFileDialog Control

In order to save a file, we can follow the steps given below.

  1. At first, drag a RichTextBox control on the form. Also put a Buuton control on the form.
  2. After that double click on the button to generate the event handler of the Click event.
  3. For the purpose of saving the text that the RichTextBox contains in a text file, create an object of the SaveFileDialog control. For example Dim sv As New SaveFileDialog.
  4. Also create an object of type String for holding the name of file. For Example, Dim fn as String.
  5. Further, set some properties of the SaveFileDialog control. For example sv.Filter=”Text Files (*.txt)|*.txt” and sv.Title=”Save File”.
  6. After that call the ShowDialog() method for opening the save file dialog box. For example, sv.ShowDialog().
  7. Now retrieve the FileName in the string variable. For istance, fn=sv.FileName
  8. In order to save the file, create an object of StreamWriter. For example, Dim objwriter as New System.IO.StreamWriter(fn)
  9. After that call the Write() method. For example, objwriter.Write(RichTextBox1.Text).
  10. Finally, close the objwriter. For example, objwriter.close()

The following code shows the complete example.

Form Design

Dialog Control Example
Dialog Control Example
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim cd As New ColorDialog
        cd.ShowDialog()
        RichTextBox1.BackColor = cd.Color

    End Sub

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

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

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim ft As New FontDialog
        ft.ShowDialog()
        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"
        sv.ShowDialog()

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

Output

The Output of Code Examples of Dialog Controls in VB.NET
The Output of Code Examples of Dialog Controls in VB.NET
programmingempire

You may also like...