Toolstrip Events Code Behind – Readymade code for New, Save, Print, Copy, Paste, Help

Please find the code below for your application’s Toolstrip control


' Code for New Record 
Private Sub NewToolStripButton1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripButton1.Click

' Check whether all textboxes empty and then ask user...
For Each ctrl As Control In Me.Controls
' If they are not empty, then ask user to clear contents
   If TypeOf ctrl Is TextBox And Not ctrl.Text = "" Then
      Dim userAction As MsgBoxResult = MsgBox("Are you sure you want to start new search and clear existing contents?", MsgBoxStyle.YesNo, "Clear Data")
      If userAction = MsgBoxResult.Yes Then
          ' Clearing all textboxes at once... Please see at the bottom to get this function code  
          clearAllTxt(Me)
          txtSerailNo.Select()
      Else
          Exit Sub
      End If
   End If
Next
End Sub

' Code for Save
Private Sub SaveToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton1.Click

    ' Saving snap shot of existing form to desired destination on your local drive
    Dim myStream As Stream
    SaveFileDialog1.Filter = "Image files (*.jpg)|*.jpg|All files (*.*)|*.*"
    SaveFileDialog1.FilterIndex = 1
    SaveFileDialog1.RestoreDirectory = True

    If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
        myStream = SaveFileDialog1.OpenFile()
            If (myStream IsNotNothing) Then
                Dim bm AsNew Bitmap(Me.Width, Me.Height)
                Me.DrawToBitmap(bm, New Rectangle(1, 1, Me.Width, Me.Height))
                bm.Save(myStream, System.Drawing.Imaging.ImageFormat.Jpeg)
                myStream.Close()
            End If
    End If
End Sub

' Code for Print
Private Sub PrintToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripButton1.Click

' Check whether all textboxes empty and then ask user...
   For Each ctrl As Control In Me.Controls
      ' Determine that data found on form, and accordingly ask user
      If TypeOf ctrl Is TextBox And Not ctrl.Text = "" Then
          Dim userAction As MsgBoxResult = MsgBox("Do you want to print existing form contents?", MsgBoxStyle.YesNoCancel, "Confirm Print")
             If userAction = MsgBoxResult.Yes Then
                If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                        PrintDocument1.Print()
                End If
             Else
                Exit Sub
             End If
      ElseIf TypeOf ctrl Is TextBox And ctrl.Text = "" Then
            Dim userAction As MsgBoxResult = MsgBox("No data found on the form ! Do you still want to print ?", MsgBoxStyle.YesNoCancel, "Confirm Print")
                 If userAction = MsgBoxResult.Yes Then
                        If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                            PrintDocument1.Print()
                        End If
                 Else
                        Exit Sub
                 End If
       End If
Next
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
   Dim bm As New Bitmap(Me.Width, Me.Height)
   Me.DrawToBitmap(bm, New Rectangle(5, 5, Me.Width, Me.Height))
   e.Graphics.DrawImage(bm, 0, 0)
End Sub


' Code for copy
Private Sub CopyToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripButton1.Click
   If TypeOf Me.ActiveControl Is TextBox Then
        Dim cpy As String = Me.ActiveControl.Text.ToString()
             If Not cpy = "" Then
                Clipboard.SetText(cpy)
             End If
   End If
End Sub

' Copy for paste
Private Sub PasteToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripButton1.Click
   Dim txt AsString = Clipboard.GetText().ToString()
       If Not txt = "" Then
             If TypeOf Me.ActiveControl Is TextBox Then
                 Me.ActiveControl.Text = txt
             End If
       End If
End Sub

' Code for Help
Private Sub HelpToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpToolStripButton1.Click
   Try
       If File.Exists(HELP_FILE) Then
           System.Diagnostics.Process.Start(HELP_FILE)
       Else
           MsgBox("Help file is missing from your system", MsgBoxStyle.Exclamation, "Missing File")
       EndIf
   Catch ex As Exception
       MsgBox("Unable to open file ! Please contact your application administrator.", MsgBoxStyle.Critical, "Start Process Error")
   End Try
End Sub

' Clearing all textboxes at once
Public Sub clearAllTxt(ByVal root As Control)
   For Each ctrl As Control In root.Controls
        clearAllTxt(ctrl)
           If TypeOf ctrl Is TextBox Then
              CType(ctrl, TextBox).Text = String.Empty
           End If
   Next
End Sub