1

我需要作业方面的帮助。我有 2 个表单:form1 有一个字符串(经过一些加密/解密后得到它),Form2(frmSaveFile) 有 saveFileDialog,用户将在其中浏览位置以将生成的字符串保存到文件中。

我的问题是:如何将字符串从 form1 传递到 form2 中的 savefileDialog?并最终将其读回form1进行解密?

这是我的 Form2 代码的样子:

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);

}

这是Form2代码:

{

....code code.....

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

}

感谢您的任何帮助

4

1 回答 1

3

就这样吧,希望对你有帮助。

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);
             }
        }
    }
}

第二种方式:

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);
             }
        }
    }

第三种方式:

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);
             }
        }
    }
于 2013-08-01T07:50:21.453 回答