【问题标题】:How can send data from one form to another using MemoryStream如何使用 MemoryStream 将数据从一种形式发送到另一种形式
【发布时间】:2020-07-24 14:28:53
【问题描述】:

我制作了两个窗体并尝试将数据从一个窗体传输到另一个窗体。

对于表单 1,我使用带有以下代码的按钮将值发送到第二个表单

using(MemoryStream ms = new MemoryStream()) {
    using(CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write)) {
        byte[] plainTextMessage = Encoding.UTF8.GetBytes(messageToSend);
        cs.Write(plainTextMessage, 0, plainTextMessage.Length);
        cs.Close();
        Messages_Class.FIrst_Client = ms.ToArray();

        txt_sender1_encryption.Text = Convert.ToBase64String(Messages_Class.FIrst_Client);
        StegScheme scheme;
        string stegoMessage;
        byte[] secretMessage = Encoding.UTF8.GetBytes(txt_sender1_encryption.Text);
        string coverMessage = txtCover.Text;

        scheme = StegScheme.MSCUKAT;
        stegoMessage = ArabicSteg.Encode(secretMessage, true, coverMessage, scheme, out used);
        txt_sender1_Stego.Text = stegoMessage;
    }
}

对于表单 2,我使用带有以下代码的按钮从第一个表单中获取值

using(MemoryStream ms = new MemoryStream()) {
    using(CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write)) {
        cs.Write(Messages_Class.Second_Client, 0, Messages_Class.Second_Client.Length);
        cs.Close();
        txt_incoming_normal.Text = Encoding.UTF8.GetString(ms.ToArray());
        txt_incoming_encrypted.Text = Convert.ToBase64String(Messages_Class.Second_Client);
    }
}

但是,我成功发送和接收Messages_Class.FIrst_Client = ms.ToArray(); 的值,但我想知道如何发送另一个值,例如“stegoMessage”和方案。我希望任何人都可以提供帮助

【问题讨论】:

    标签: c# visual-studio


    【解决方案1】:

    Messages_Class.FIrst_Client = ms.ToArray();

    您是否通过静态类传递值?我认为这不是一个好的选择。

    可以通过Application.OpenForms先获取form1的实例,从form1获取数据。

    Form1.cs

    public partial class Form1 : Form
    {
        public TextBox tb1
        {
            get { return textBoxinForm1; }
            set { textBoxinForm1 = value; }
        }
    
        private void buttonSendtoForm2_Click(object sender, EventArgs e)
        {
            Form2 form2 = (Form2)Application.OpenForms["Form2"];
            form2.tb2.Text = textBoxinForm1.Text;
        }
    }
    

    Form2.cs

    public partial class Form2 : Form
    {
        public TextBox tb2
        {
            get { return textBoxinForm2; }
            set { textBoxinForm2 = value; }
        }
    
        private void buttonSendtoForm1_Click(object sender, EventArgs e)
        {
            Form1 form1 = (Form1)Application.OpenForms["Form1"];
            form1.tb1.Text = textBoxinForm2.Text;
        }
    }
    

    测试结果,

    **更新单例模式**

    Form2.cs中添加的内容

    public partial class Form2 : Form
    {
        public static Form2 f2 = null;
        public static Form2 Form2Signleton()
        {
            if (f2 == null)
            {
                f2 = new Form2();
            }
            return f2;
        }
    
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                this.Dispose(true);
                f2 = null;
            }
            catch (Exception m)
            {
                MessageBox.Show(m.ToString());
            }
        }
    }
    

    然后在Form1.cs中调用From2Singleton

    // open form2 from form1
    private void button1_Click(object sender, EventArgs e)
    {
        stegoMessage = "test";
        Form2 form2 = Form2.Form2Signleton();
        form2.Show();
    }
    

    【讨论】:

    • 感谢重播,很抱歉没有清除我的问题,我需要在按下发送按钮时将数据以第二种形式直接发送到文本框,反之亦然。但是我尝试了很多方法,例如这些解决方案“stackoverflow.com/questions/1559770/…”它的工作,但问题是当按下发送按钮时它再次打开新表单而不是在打开的表单之间进行传输
    • Form1和Form2是什么关系? Form2 是从 Form1 打开的吗?
    • @MohammedFarttoos 如果您希望表单只打开一次,您可以尝试Singleton mode。我已经更新了我的答案。你可以在你的项目中测试它。
    • 它就像两个人之间的聊天,但处于加密状态。为了解释当发件人 1 发送消息时,它将隐藏在隐秘消息中并发送,所以我将所有进程放在发送按钮中,并尝试将隐秘消息从发件人 1 发送到第二个,反之亦然,当用户 2 写消息并按下发送按钮时它将被发送给发件人一。但正如我告诉你的,不要打开新表单,我需要在这两个打开的表单之间使用它
    • @MohammedFarttoos 如果是这样,您仍然可以通过Application.OpenForms 和一些自定义属性来实现它。我更新了答案并添加了一个 gif 演示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 2021-08-06
    • 2023-04-01
    相关资源
    最近更新 更多