OpenFileDialog Codes

Example: How to browse files from the User’s desired Folder.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        OpenFileDialog1.InitialDirectory = "E:\New Folder\New Folder1"
        OpenFileDialog1.RestoreDirectory = True
        
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                PictureBox1.ImageLocation = OpenFileDialog1.FileName 
        End If
End Sub

NB : If User's folder path is incorrect/not available, then System Default Folder works automatically works.
Example : How to browse files from the System Default Folder.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then  'File's Browsing Code
         PictureBox1.ImageLocation = OpenFileDialog1.FileName  'File's Attaching Code in Picture Box
     End If
End Sub
Example : How to browse files from the System Default Folder with the File’s path detail.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
           TextBox2.Text = OpenFileDialog1.FileName      'Show full Path of browsing image.
           PictureBox1.ImageLocation = TextBox2.Text
     End If
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
 If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
  TextBox2.Text = Path.GetFileName(OpenFileDialog1.FileName)'Show only image file name,not full path.
  PictureBox1.ImageLocation = TextBox2.Text
 End If
End Sub
Example : How to browse files from the User’s desired Folder with the File’s path detail.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     OpenFileDialog1.InitialDirectory = "E:\New Folder\New Folder1"
     OpenFileDialog1.RestoreDirectory = True

     If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
           TextBox2.Text = OpenFileDialog1.FileName      'File's Path Detail code.
           PictureBox1.ImageLocation = TextBox2.Text
     End If
End Sub

---------------  OR  ---------------

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        OpenFileDialog1.Title = "Please Select Photo"
        OpenFileDialog1.InitialDirectory = "F:"
        OpenFileDialog1.ShowDialog()
        Picture1.ImageLocation = OpenFileDialog1.FileName.ToString
        Picture1.Visible = True
End Sub

PrintFileDialog Codes

Example: How to Print the file contents in VB .Net.
NB:
First of all create a form(say Form2) - Now drag the PageSetupDialog(say PageSetupDialog1), PrintDialog(say PrintDialog1), PrintDocument(say PrintDocument1), PrintPreviewDialog(say PrintPreviewDialog1) controls from 'Printing' options of Control Tool box over the Form - Now create a Rich Text box to write and print the contents (say RichTextBox1 ) - Now create three buttons on the form Print(say BtnPrint), PrintPreview(say BtnPrintPreview) and Page Setup(say BtnPageSetup)

------------------------------------------------------------------

Public Class Form2

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Text = "Print Form"

        BtnPrint.Text = "Print"
        BtnPrintPreview.Text = "Print PreView"

        BtnPrint.BackColor = Color.Red
        BtnPrintPreview.BackColor = Color.Green

        PrintPreviewDialog1.Document = PrintDocument1
    End Sub

--------------------------------------------------------------------

    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim font As New Font("Times New Roman", 24, FontStyle.Regular)     'set the font to display  
        e.Graphics.DrawString(RichTextBox1.Text, font, Brushes.Red, 100, 100) 'The DrawString() function is used to print letters.  
    End Sub

---------------------------------------------------------------------

    Private Sub BtnPrint_Click(sender As Object, e As EventArgs) Handles BtnPrint.Click
        If PrintDialog1.ShowDialog = DialogResult.OK Then        'Open the print dialog box  
            PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
            PrintDocument1.Print()       'print a document  
        End If
    End Sub

---------------------------------------------------------------------

    Private Sub BtnPageSetup_Click(sender As Object, e As EventArgs) Handles BtnPageSetup.Click
        PageSetupDialog1.Document = PrintDocument1
        PageSetupDialog1.Document.DefaultPageSettings.Color = False
        PageSetupDialog1.ShowDialog()
    End Sub

---------------------------------------------------------------------

    Private Sub BtnPrintPreview_Click(sender As Object, e As EventArgs) Handles BtnPrintPreview.Click
        If RichTextBox1.Text = " " Then
            MsgBox("Please write some text in box to Print...")
            Exit Sub
        Else
            PrintPreviewDialog1.ShowDialog()
        End If
    End Sub

End Class

Loading

Categories: VB .Net Codes

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.