Calling a button_click method within another method using it's args

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kamauharrison87
    New Member
    • Apr 2016
    • 7

    Calling a button_click method within another method using it's args

    I want to read a .csv file with hundreds of lines of data. I have created two buttons, one for splitting the file into distinct lines based on their distinct features and the other one for encrypting each of the file and saving two output files.
    I have managed to create the methods successfully but merging them is a problem. i.e how do i call the button_split method within the encryption button?

    Using my current code, i get this error "Could not find file 'C:\Users\Harri sonPc\Documents \VisualStudio20 10\Projects\New Algo\NewAlgo\bi n\Debug\DO16090 001_ABL_MCK058"

    Please help.
    Thanks
    Code:
      Private Sub SplitFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SplitFolder.Click
            SplitFolder.Font = New Font(SplitFolder.Font, FontStyle.Italic)
            Dim openFileDialog1 As New OpenFileDialog()
            'Setup the open dialog.
            openFileDialog1.FileName = ""
            openFileDialog1.Title = "Choose a file to encrypt"
            openFileDialog1.InitialDirectory = "C:\"
            openFileDialog1.Filter = "All Files (*.*) | *.*"
    
            'Find out if the user chose a file.
            If openFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
                MainModule.strFileToEncrypt = openFileDialog1.FileName
                txtDestinationEncrypt.Text = MainModule.strFileToEncrypt
    
                Dim iPosition As Integer = 0
                Dim i As Integer = 0
    
                'Get the position of the last "\" in the OpenFileDialog.FileName path.
                '-1 is when the character your searching for is not there.
                'IndexOf searches from left to right.
                While MainModule.strFileToEncrypt.IndexOf("\"c, i) <> -1
                    iPosition = MainModule.strFileToEncrypt.IndexOf("\"c, i)
                    i = iPosition + 1
                End While
    
                Dim sr As StreamReader = New StreamReader(MainModule.strFileToEncrypt)
                Dim strLine As String = String.Empty
    
                Do While sr.Peek() >= 0
                    strLine = String.Empty
                    strLine = sr.ReadLine
                    Dim reader As StringReader = New StringReader(strLine.ToString())
                    Dim currentRow(1000) As String
                    Dim j As Integer
    
                    While True
                        'For index As Integer = 0 To strLine.Length - 1
    
                        currentRow(j) = reader.ReadLine()
    
                        MessageBox.Show(currentRow(0))
                        ' MessageBox.Show(currentRow(1))
                        Dim currentField As String = currentRow(0)
                        Dim delivery_order_number As String = currentField.Split(",")(0)
                        delivery_order_number = delivery_order_number.Replace("""", "").Trim()
                        delivery_order_number = delivery_order_number.Replace("/", "").Trim()
    
                        MsgBox(delivery_order_number)
                        Dim buyer_code As String = currentField.Split(",")(1)
                        buyer_code = buyer_code.Replace("""", "").Trim()
                        MsgBox(buyer_code)
                        Dim warehouse_code As String = currentField.Split(",")(2)
                        warehouse_code = warehouse_code.Replace("""", "").Trim()
                        MsgBox(warehouse_code)
    
                        txtFileDestination.Text = delivery_order_number + "_" + buyer_code + "_" + warehouse_code
                        ' Set a variable to the My Documents path.
                        Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                        Dim outputFile As String = path & "\" & txtFileDestination.Text
                        MsgBox(outputFile)
                        If System.IO.File.Exists(outputFile) = False Then
                            System.IO.File.Create(outputFile).Dispose()
                        End If
                        Dim objWriter As New System.IO.StreamWriter(outputFile, True)
                        objWriter.WriteLine(currentRow(j))
                        objWriter.Close()
                        Exit While
    
    
                    End While
    
                    Exit Do
                    j += 1
                Loop
    
            End If
        End Sub
     Private Sub EncryptString_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EncryptString.Click
            EncryptString.Font = New Font(EncryptString.Font, FontStyle.Italic)
            SplitFolder_Click(sender, e)
            'Dim openFileDialog1 As New OpenFileDialog()
            'Setup the open dialog.
            ' openFileDialog1.FileName = ""
            ' openFileDialog1.Title = "Choose a file to encrypt"
            ' openFileDialog1.InitialDirectory = "C:\"
            ' openFileDialog1.Filter = "All Files (*.*) | *.*"
    
            'Find out if the user chose a file.
            '     If openFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            '   MainModule.strFileToEncrypt = openFileDialog1.FileName
            MainModule.strFileToEncrypt = txtFileDestination.Text
            txtDestinationEncrypt.Text = MainModule.strFileToEncrypt
    
            Dim iPosition As Integer = 0
            Dim i As Integer = 0
    
            'Get the position of the last "\" in the OpenFileDialog.FileName path.
            '-1 is when the character your searching for is not there.
            'IndexOf searches from left to right.
            While MainModule.strFileToEncrypt.IndexOf("\"c, i) <> -1
                iPosition = MainModule.strFileToEncrypt.IndexOf("\"c, i)
                i = iPosition + 1
            End While
            strOutputEncrypt = MainModule.strFileToEncrypt.Substring(iPosition + 1)
            strOutputEncrypt2 = MainModule.strFileToEncrypt.Substring(iPosition + 1)
    
            'Assign S the entire path, ending at the last "\".
            Dim S As String = MainModule.strFileToEncrypt.Substring(0, iPosition + 1)
    
            'Replace the "." in the file extension with "_".
            strOutputEncrypt = Me.strOutputEncrypt.Replace("."c, "_"c)
            strOutputEncrypt2 = Me.strOutputEncrypt2.Replace("."c, "_"c)
            'The final file name.  XXXXX.encrypt
            txtDestinationEncrypt.Text = S + Me.strOutputEncrypt + ".encrypt"
            txtDestinationEncrypt2.Text = S + Me.strOutputEncrypt2 + "_encrypted" + ".txt"
            'End If
            Dim txtPassEncrypt As String = "this_is_the_key"
            txtFileToEncrypt.Text = txtPassEncrypt
    
    
            'Declare variables for the key and iv.
            'The key needs to hold 256 bits and the iv 128 bits.
            Dim bytKey As Byte()
            Dim bytIV As Byte()
            'Send the password to the CreateKey function.
            bytKey = CreateKey(txtPassEncrypt)
            'Send the password to the CreateIV function.
            bytIV = CreateIV(txtPassEncrypt)
    
            'Start the encryption.
            EncryptOrDecryptFile(MainModule.strFileToEncrypt, txtDestinationEncrypt.Text, _
                                 bytKey, bytIV, CryptoAction.ActionEncrypt)
            EncryptOrDecryptFile(MainModule.strFileToEncrypt, txtDestinationEncrypt2.Text, _
                                     bytKey, bytIV, CryptoAction.ActionEncrypt)
    
    
        End Sub
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    Either create your code within functions and call the functions, or you can call a button adding the following line into your encryption button click event:
    Code:
    SplitFolder_Click(sender, e)

    Comment

    • kamauharrison87
      New Member
      • Apr 2016
      • 7

      #3
      Thanks for your response.
      I already had called the splitFolder_cli ck method on the second line of EncryptString button

      Comment

      • Luk3r
        Contributor
        • Jan 2014
        • 300

        #4
        Is your .csv file located within the project directory? It seems that's what it's implying. My apologies for missing that in your code.

        Comment

        • kamauharrison87
          New Member
          • Apr 2016
          • 7

          #5
          I have just saved it within that folder but still gets the same error.

          Comment

          • kamauharrison87
            New Member
            • Apr 2016
            • 7

            #6
            Might the problem be in the path definition? The visual studio is within the MyDocuments folder but have several projects within it.
            Code:
            txtFileDestination.Text = delivery_order_number + "_" + buyer_code + "_" + warehouse_code
                                ' Set a variable to the My Documents path.
                                Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                                Dim outputFile As String = path & "\" & txtFileDestination.Text
                                MsgBox(outputFile)
                                If System.IO.File.Exists(outputFile) = False Then
                                    System.IO.File.Create(outputFile).Dispose()
                                End If
                                Dim objWriter As New System.IO.StreamWriter(outputFile, True)
                                objWriter.WriteLine(currentRow(j))
                                objWriter.Close()

            Comment

            • kamauharrison87
              New Member
              • Apr 2016
              • 7

              #7
              I have changed the path
              from
              Dim path As String = System.Environm ent.GetFolderPa th(Environment. SpecialFolder.M yDocuments)

              to
              Dim path As String = Directory.GetCu rrentDirectory
              And it has worked!
              Thanks

              Comment

              Working...