Click here to Skip to main content
15,905,971 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
Hi,

I use this code below for converting files into Base64. This piece of code
textBoxDescribeIncident.Text = FileToBase64String(openDlg.FileName);
convert files into Base64. How can I use this outcome in an other private void button_Click ???

Thanks in advance!!

private void buttonBrowse_Click(object sender, EventArgs e) {
    OpenFileDialog openDlg = new OpenFileDialog();
    openDlg.Filter = "All Supported Files (All Files (*.*)|*.*";
    if (openDlg.ShowDialog(this) == DialogResult.OK) {
        Cursor = Cursors.WaitCursor;
        textBoxAttachFile.Text = openDlg.FileName;
        try {
            textBoxDescribeIncident.Text = FileToBase64String(openDlg.FileName);
        } catch (Exception) {
            MessageBox.Show(this, string.Format("An error occurred whilst converting to base-64."),
                Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        Cursor = Cursors.Default;
    }
}


private void buttonCreate_Click(object sender, EventArgs e) {
     
     1. First I select a files, this file will directly converted to Base64
     2. I fillin all my other fields
     3. When I click the button Create, I need the Base64 hashcode in this private methode

}
Posted
Updated 11-Apr-11 21:56pm
v2
Comments
OriginalGriff 12-Apr-11 3:52am    
What is the problem? It looks as if you are using it in the click method... :confused:
Member 7762422 12-Apr-11 3:56am    
1. First I select a files, this file will directly converted to Base64
2. I fillin all my other fields
3. When I click the button Create, I need the Base64 hashcode in this private methode
OriginalGriff 12-Apr-11 4:01am    
You are selecting a file:

OpenFileDialog openDlg = new OpenFileDialog();
openDlg.Filter = "All Supported Files (All Files (*.*)|*.*";
if (openDlg.ShowDialog(this) == DialogResult.OK) {
You presumably have filled in the fields in your form before you pressed the button.
You get the Base64 string:
textBoxDescribeIncident.Text = FileToBase64String(openDlg.FileName);

So where is the problem? This is probably simple, but I don't understand which bit of this is causing difficulty - you have done it once already!
Member 7762422 12-Apr-11 4:07am    
I think my problem is, First I select a file with the browse button, second step I click on the create button but both buttons are in a private method so how can I use the values of textBoxDescribeIncident.Text = FileToBase64String(openDlg.FileName); in my second step?
Pong D. Panda 12-Apr-11 4:14am    
You can! If they are on the same class, just call the textBoxDescribeIncident.Text on your second button click event.

When you press your "Browse" button, you create the Base64 string, and save it to what is (from the name) a text box:
private void buttonBrowse_Click(object sender, EventArgs e) {
    OpenFileDialog openDlg = new OpenFileDialog();
    openDlg.Filter = "All Supported Files (All Files (*.*)|*.*";
    if (openDlg.ShowDialog(this) == DialogResult.OK) {
        Cursor = Cursors.WaitCursor;
        textBoxAttachFile.Text = openDlg.FileName;
        try {
            textBoxDescribeIncident.Text = FileToBase64String(openDlg.FileName); //<<<---HERE!
        } catch (Exception) {
            MessageBox.Show(this, string.Format("An error occurred whilst converting to base-64."),
                Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        Cursor = Cursors.Default;
    }
}
You can access that from your other buttons, provided they are on the same form:
private void buttonCreate_Click(object sender, EventArgs e) 
   {
   if (!string.IsNullOrEmpty(textBoxDescribeIncident.Text))
      {
      MessageBox.Show("Here it is: " + textBoxDescribeIncident.Text);
      }
   }
If you don't want to display the info in a text box, then declare a private string and put it there instead.
If you need it on a different form, then that's another matter - easy to solve, but different!
 
Share this answer
 
Comments
Member 7762422 12-Apr-11 4:20am    
Thanks OrginalGriff, this is working in my situation!
OriginalGriff 12-Apr-11 4:23am    
Welcome!
Tarun.K.S 12-Apr-11 4:31am    
Lol! This was OP's problem!
You have your Base64 conversion on your textBoxDescribeIncident.Text already, so why not use that value on your buttonCreate_Click.

If the two buttons were on different class, lets say buttonBrowse_Click on Class1 and buttonCreate_Click on Class2. You can add a public property on your Class2 that will be set by buttonBrowse_Click on Class1.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900