Thursday, April 25, 2013

Example: Common Dialog Controls

Open, Save, Color, Font and Print Dialog Controls
image082

Imports System.IO
Public Class Dialogss

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.Title = "Select File to open"
        OpenFileDialog1.Filter = "Text File|*.txt|All Files|*.*"
        OpenFileDialog1.FilterIndex = 1
        OpenFileDialog1.CheckFileExists = True
        OpenFileDialog1.CheckPathExists = True
        OpenFileDialog1.InitialDirectory=My.Computer.FileSystem.SpecialDirectories.MyDocuments
        OpenFileDialog1.Multiselect = False
        OpenFileDialog1.ShowHelp = True
        Dim rfile As StreamReader
        If OpenFileDialog1.ShowDialog = MsgBoxResult.OK Then
            rfile = New StreamReader(OpenFileDialog1.FileName)
            Me.TextBox1.Text = rfile.ReadToEnd
        End If

    End Sub

    Private Sub Button2_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        SaveFileDialog1.Title = "Save File"
        SaveFileDialog1.Filter = "Text Files|*.txt|All Files|*.*"
        SaveFileDialog1.AddExtension = True
        SaveFileDialog1.DefaultExt = "txt"
        Dim wfile As StreamWriter
        If SaveFileDialog1.ShowDialog = MsgBoxResult.Ok Then
            wfile = New StreamWriter(SaveFileDialog1.FileName)
            wfile.Write (Me.TextBox1.Text)
            wfile.Close ()
        End If
    End Sub

    Private Sub Button3_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ColorDialog1.AllowFullOpen = True
        ColorDialog1.FullOpen = True
        If ColorDialog1.ShowDialog = MsgBoxResult.Ok Then
            Me.TextBox1.ForeColor = ColorDialog1.Color
        End If
    End Sub

    Private Sub Button4_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        FontDialog1.ShowEffects = True
        FontDialog1.ShowApply = True
        If FontDialog1.ShowDialog = MsgBoxResult.Ok Then
            Me.TextBox1.Font = FontDialog1.Font
        End If
    End Sub

    Private Sub Button5_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        PrintDialog1.AllowPrintToFile = True
        PrintDialog1.AllowSelection = True
        Dim pd As New Printing.PrintDocument

        PrintDialog1.Document = New Printing.PrintDocument
        If PrintDialog1.ShowDialog = MsgBoxResult.Ok Then
            pd.Print()
        End If
    End Sub
End Class

No comments:

Post a Comment

Contact us

Name

Email *

Message *