473,473 Members | 2,303 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

7 New Member
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\HarrisonPc\Documents\VisualStudio2010\Pr ojects\NewAlgo\NewAlgo\bin\Debug\DO16090001_ABL_MC K058"

Please help.
Thanks
Expand|Select|Wrap|Line Numbers
  1.   Private Sub SplitFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SplitFolder.Click
  2.         SplitFolder.Font = New Font(SplitFolder.Font, FontStyle.Italic)
  3.         Dim openFileDialog1 As New OpenFileDialog()
  4.         'Setup the open dialog.
  5.         openFileDialog1.FileName = ""
  6.         openFileDialog1.Title = "Choose a file to encrypt"
  7.         openFileDialog1.InitialDirectory = "C:\"
  8.         openFileDialog1.Filter = "All Files (*.*) | *.*"
  9.  
  10.         'Find out if the user chose a file.
  11.         If openFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
  12.             MainModule.strFileToEncrypt = openFileDialog1.FileName
  13.             txtDestinationEncrypt.Text = MainModule.strFileToEncrypt
  14.  
  15.             Dim iPosition As Integer = 0
  16.             Dim i As Integer = 0
  17.  
  18.             'Get the position of the last "\" in the OpenFileDialog.FileName path.
  19.             '-1 is when the character your searching for is not there.
  20.             'IndexOf searches from left to right.
  21.             While MainModule.strFileToEncrypt.IndexOf("\"c, i) <> -1
  22.                 iPosition = MainModule.strFileToEncrypt.IndexOf("\"c, i)
  23.                 i = iPosition + 1
  24.             End While
  25.  
  26.             Dim sr As StreamReader = New StreamReader(MainModule.strFileToEncrypt)
  27.             Dim strLine As String = String.Empty
  28.  
  29.             Do While sr.Peek() >= 0
  30.                 strLine = String.Empty
  31.                 strLine = sr.ReadLine
  32.                 Dim reader As StringReader = New StringReader(strLine.ToString())
  33.                 Dim currentRow(1000) As String
  34.                 Dim j As Integer
  35.  
  36.                 While True
  37.                     'For index As Integer = 0 To strLine.Length - 1
  38.  
  39.                     currentRow(j) = reader.ReadLine()
  40.  
  41.                     MessageBox.Show(currentRow(0))
  42.                     ' MessageBox.Show(currentRow(1))
  43.                     Dim currentField As String = currentRow(0)
  44.                     Dim delivery_order_number As String = currentField.Split(",")(0)
  45.                     delivery_order_number = delivery_order_number.Replace("""", "").Trim()
  46.                     delivery_order_number = delivery_order_number.Replace("/", "").Trim()
  47.  
  48.                     MsgBox(delivery_order_number)
  49.                     Dim buyer_code As String = currentField.Split(",")(1)
  50.                     buyer_code = buyer_code.Replace("""", "").Trim()
  51.                     MsgBox(buyer_code)
  52.                     Dim warehouse_code As String = currentField.Split(",")(2)
  53.                     warehouse_code = warehouse_code.Replace("""", "").Trim()
  54.                     MsgBox(warehouse_code)
  55.  
  56.                     txtFileDestination.Text = delivery_order_number + "_" + buyer_code + "_" + warehouse_code
  57.                     ' Set a variable to the My Documents path.
  58.                     Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
  59.                     Dim outputFile As String = path & "\" & txtFileDestination.Text
  60.                     MsgBox(outputFile)
  61.                     If System.IO.File.Exists(outputFile) = False Then
  62.                         System.IO.File.Create(outputFile).Dispose()
  63.                     End If
  64.                     Dim objWriter As New System.IO.StreamWriter(outputFile, True)
  65.                     objWriter.WriteLine(currentRow(j))
  66.                     objWriter.Close()
  67.                     Exit While
  68.  
  69.  
  70.                 End While
  71.  
  72.                 Exit Do
  73.                 j += 1
  74.             Loop
  75.  
  76.         End If
  77.     End Sub
  78.  Private Sub EncryptString_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EncryptString.Click
  79.         EncryptString.Font = New Font(EncryptString.Font, FontStyle.Italic)
  80.         SplitFolder_Click(sender, e)
  81.         'Dim openFileDialog1 As New OpenFileDialog()
  82.         'Setup the open dialog.
  83.         ' openFileDialog1.FileName = ""
  84.         ' openFileDialog1.Title = "Choose a file to encrypt"
  85.         ' openFileDialog1.InitialDirectory = "C:\"
  86.         ' openFileDialog1.Filter = "All Files (*.*) | *.*"
  87.  
  88.         'Find out if the user chose a file.
  89.         '     If openFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
  90.         '   MainModule.strFileToEncrypt = openFileDialog1.FileName
  91.         MainModule.strFileToEncrypt = txtFileDestination.Text
  92.         txtDestinationEncrypt.Text = MainModule.strFileToEncrypt
  93.  
  94.         Dim iPosition As Integer = 0
  95.         Dim i As Integer = 0
  96.  
  97.         'Get the position of the last "\" in the OpenFileDialog.FileName path.
  98.         '-1 is when the character your searching for is not there.
  99.         'IndexOf searches from left to right.
  100.         While MainModule.strFileToEncrypt.IndexOf("\"c, i) <> -1
  101.             iPosition = MainModule.strFileToEncrypt.IndexOf("\"c, i)
  102.             i = iPosition + 1
  103.         End While
  104.         strOutputEncrypt = MainModule.strFileToEncrypt.Substring(iPosition + 1)
  105.         strOutputEncrypt2 = MainModule.strFileToEncrypt.Substring(iPosition + 1)
  106.  
  107.         'Assign S the entire path, ending at the last "\".
  108.         Dim S As String = MainModule.strFileToEncrypt.Substring(0, iPosition + 1)
  109.  
  110.         'Replace the "." in the file extension with "_".
  111.         strOutputEncrypt = Me.strOutputEncrypt.Replace("."c, "_"c)
  112.         strOutputEncrypt2 = Me.strOutputEncrypt2.Replace("."c, "_"c)
  113.         'The final file name.  XXXXX.encrypt
  114.         txtDestinationEncrypt.Text = S + Me.strOutputEncrypt + ".encrypt"
  115.         txtDestinationEncrypt2.Text = S + Me.strOutputEncrypt2 + "_encrypted" + ".txt"
  116.         'End If
  117.         Dim txtPassEncrypt As String = "this_is_the_key"
  118.         txtFileToEncrypt.Text = txtPassEncrypt
  119.  
  120.  
  121.         'Declare variables for the key and iv.
  122.         'The key needs to hold 256 bits and the iv 128 bits.
  123.         Dim bytKey As Byte()
  124.         Dim bytIV As Byte()
  125.         'Send the password to the CreateKey function.
  126.         bytKey = CreateKey(txtPassEncrypt)
  127.         'Send the password to the CreateIV function.
  128.         bytIV = CreateIV(txtPassEncrypt)
  129.  
  130.         'Start the encryption.
  131.         EncryptOrDecryptFile(MainModule.strFileToEncrypt, txtDestinationEncrypt.Text, _
  132.                              bytKey, bytIV, CryptoAction.ActionEncrypt)
  133.         EncryptOrDecryptFile(MainModule.strFileToEncrypt, txtDestinationEncrypt2.Text, _
  134.                                  bytKey, bytIV, CryptoAction.ActionEncrypt)
  135.  
  136.  
  137.     End Sub
Apr 25 '16 #1
6 1326
Luk3r
300 Contributor
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:
Expand|Select|Wrap|Line Numbers
  1. SplitFolder_Click(sender, e)
Apr 25 '16 #2
kamauharrison87
7 New Member
Thanks for your response.
I already had called the splitFolder_click method on the second line of EncryptString button
Apr 25 '16 #3
Luk3r
300 Contributor
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.
Apr 25 '16 #4
kamauharrison87
7 New Member
I have just saved it within that folder but still gets the same error.
Apr 26 '16 #5
kamauharrison87
7 New Member
Might the problem be in the path definition? The visual studio is within the MyDocuments folder but have several projects within it.
Expand|Select|Wrap|Line Numbers
  1. txtFileDestination.Text = delivery_order_number + "_" + buyer_code + "_" + warehouse_code
  2.                     ' Set a variable to the My Documents path.
  3.                     Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
  4.                     Dim outputFile As String = path & "\" & txtFileDestination.Text
  5.                     MsgBox(outputFile)
  6.                     If System.IO.File.Exists(outputFile) = False Then
  7.                         System.IO.File.Create(outputFile).Dispose()
  8.                     End If
  9.                     Dim objWriter As New System.IO.StreamWriter(outputFile, True)
  10.                     objWriter.WriteLine(currentRow(j))
  11.                     objWriter.Close()
Apr 26 '16 #6
kamauharrison87
7 New Member
I have changed the path
from
Dim path As String = System.Environment.GetFolderPath(Environment.Speci alFolder.MyDocuments)

to
Dim path As String = Directory.GetCurrentDirectory
And it has worked!
Thanks
Apr 26 '16 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
3
by: MC D | last post by:
Is it possible to call an objects constructor method from within the class somehow? The class I have populates itself with data from a sqlServer database when it is constructed, and I want to...
1
by: Mike | last post by:
Hi Scenario: I am calling a web method from a WinForms method. The proxy of the web service then checks one of it's settings. Depending on the setting I want the proxy to call a method in the...
3
by: shawn | last post by:
Hi All Was trying to get this bit of code working (simplified of course) for (i=0;i<uuid_num;i++) { window.setInterval(InitMap(uuidValues), timePeriod); } Where uuid_num, uuidValues,...
5
by: Earl | last post by:
I need to call a method on an owned child form, and am wondering if the best way of doing this is to capture the Closing event of the form that passes control back to the form where I have the...
1
by: OneShed | last post by:
Hi, I am trying to solve one problem. I have one object and I create another different object from it (aggregation). How can I now call methods of first object within the second object? Here is...
4
by: Steve Hershoff | last post by:
Hi everyone, We have a javascript function we'd like to call from within a C# method in our code-behind file. The way it has worked historically is we'd call the method from a hyperlink, like...
1
by: Andrus | last post by:
How to call extension method using reflection ? I tried this code but methodInfo is null. Andrus. using System.Reflection; using System.Windows.Forms;
7
by: =?ISO-8859-1?Q?Fernando_G=F3mez?= | last post by:
Hello all. I have this class with a virtual method and a constructor that calls this virtual method. A derived class overrides this virtual method, so I expected that when the base's constructor is...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.