1

I need help with an assignment please. I have 2 forms: form1 has a string( I get it after some encryption/decryption), Form2(frmSaveFile) has the saveFileDialog where the user will browse a location to save the generated string into a file.

my question is: how do I pass the string from form1 to savefileDialog in form2? and eventually read it back to form1 for decryption?

Here is how my Form2 code looks like:

private Form1 myForm1;
    private void btnBrowse_Click_1(object sender, EventArgs e)
    {

       myForm1 = new Form1();
      string val =  myForm1.Encrypted_TextVal;  // I try to get this val from form1 but it's null cause I call it before form1 does anything with it!

        SaveFileDialog save = new SaveFileDialog();
        if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK) {

            StreamWriter write = new StreamWriter(File.Create(save.FileName));
            write.Write(val);

}

Here is Form2 code:

{

....code code.....

 string hashDecryptedText = BitConverter.ToString(sh1.ComputeHash(textToBitArray.GetBytes(Decrypted))); // string to save in a file

}

Thank you for any help

samy
  • 31
  • 4
  • 3
    Can you shorten down your code to the relevant parts, please? I think the specifics of your save method are not relevant when your question is about accessing something across forms. Also, please format your code correctly for the question and add the *homework* tag if this is a homework assignment. Note that very similar questions were already discussed [here](http://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form-in-c-sharp-winforms-application), ... – O. R. Mapper Aug 01 '13 at 07:23
  • ... [here](http://stackoverflow.com/questions/14663710/c-sharp-winform-accessing-public-properties-from-other-forms-difference-betwe), [here](http://stackoverflow.com/questions/217389/how-to-access-form-methods-and-controls-from-a-class-in-c) and [here](http://stackoverflow.com/questions/8566/best-way-to-access-a-control-on-another-form-in-winforms). – O. R. Mapper Aug 01 '13 at 07:23
  • Thanks for your comments Mapper. I shortened my code and I will try to look at the posts you have recommended. – samy Aug 01 '13 at 07:40
  • @Mapper, most of the existing answers access values from CONTROLS in other forms( textbox or label). my problem is that the string I want to pass is not coming from any control. It's just a string I created and want to save in a file from another form! Thanks for sharing this info though – samy Aug 01 '13 at 07:44
  • The solutions involving public properties from [this](http://stackoverflow.com/questions/217389/how-to-access-form-methods-and-controls-from-a-class-in-c) and [this](http://stackoverflow.com/questions/8566/best-way-to-access-a-control-on-another-form-in-winforms) question are still applicable - you can expose your string by offering a public property just the same way. – O. R. Mapper Aug 01 '13 at 07:49

1 Answers1

3

There you go, I hope it helps.

using System;
using System.Windows.Forms;
using System.IO;

namespace Stackoverflow
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        static string Hash = "Your encrytped/hash/w.e";

        Form2 form2 = new Form2(Hash);
    }

    public partial class Form2 : Form
    {
        public Form2(string Hash)
        {
             SaveFileDialog save = new SaveFileDialog();
             save.DefaultExt = ".txt";
             if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             {
                 using (StreamWriter write = new StreamWriter(File.Create(save.FileName)))                
                    write.Write(Hash);
             }
        }
    }
}

A second way:

public partial class Form1 : Form
    {
        public static Form1 Global;
        public Form1()
        {
            InitializeComponent();
            Global = this;
        }

        public string Hash = "Your encrytped/hash/w.e";

        Form2 form2 = new Form2();
    }

    public partial class Form2 : Form
    {
        public Form2()
        {
             SaveFileDialog save = new SaveFileDialog();
             save.DefaultExt = ".txt";
             if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             {
                 using (StreamWriter write = new     StreamWriter(File.Create(save.FileName)))                
                    write.Write(Form1.Global.Hash);
             }
        }
    }

A third way:

public static class DataHolder
    {
        private static string _hash;
        public static string Hash { get { return _hash; } set { _hash = value; } }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SetHash("HASH");
        }

        public void SetHash(string hash)
        {
            DataHolder.Hash = hash;           
        }

        Form2 form2 = new Form2();

    }

    public partial class Form2 : Form
    {
        public Form2()
        {
             SaveFileDialog save = new SaveFileDialog();
             save.DefaultExt = ".txt";
             if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             {
                 using (StreamWriter write = new StreamWriter(File.Create(save.FileName)))                
                    write.Write(DataHolder.Hash);
             }
        }
    }