-2

I made two windows form and try to transfer data from one to another.

For form 1, I used button with below code to send value to second form

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

For form 2, I used button with below code to recive value from first form

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

However, I sucess to send and recive value of Messages_Class.FIrst_Client = ms.ToArray(); but I wonder how sending another valuse such as "stegoMessage" and scheme. I wish any one can help

Long Luong
  • 764
  • 2
  • 14
  • 28

1 Answers1

0

Messages_Class.FIrst_Client = ms.ToArray();

Did you pass the value via a static class? I think it's not a good choice.

You can get the data from form1 by using Application.OpenForms to get the instance of form1 first.

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

Test result,

enter image description here

**Update Singleton mode**

Content added in 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());
        }
    }
}

Then call From2Singleton in Form1.cs

// open form2 from form1
private void button1_Click(object sender, EventArgs e)
{
    stegoMessage = "test";
    Form2 form2 = Form2.Form2Signleton();
    form2.Show();
}
大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
  • Thanks for replay, I'm sorry to not clear my qustion, I need when press send button it will be sending data directly to textbox in second form and vice vers. However I tried many ways such as these solution"https://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form" its work but the problem is when press send buttom it open new form again not making transfer between open forms – Mohammed Farttoos Jul 24 '20 at 05:39
  • What is the relation between Form1 and Form2? Is Form2 opened from Form1? – 大陸北方網友 Jul 24 '20 at 05:43
  • @MohammedFarttoos If you want the form open only once, you can try `Singleton mode`. I have updated my answer. You can test it in your project. – 大陸北方網友 Jul 24 '20 at 06:10
  • it like chat between two but under encryption. To explane when sender 1 send message it will be hided within stego message and send so I put all process inside the send button and just try to send stego message from sender 1 to second one and vice vers when user 2 write message and press send button it will be sent to sender one . but as I told you not open new form I need it between these two open form – Mohammed Farttoos Jul 24 '20 at 06:13
  • @MohammedFarttoos If so, you still can achieve it via `Application.OpenForms` and some custom properties. I updated the answer and add a gif demo. – 大陸北方網友 Jul 24 '20 at 06:32
  • Thanks for demo I tried to applied it but there is error message "System.NullReferenceException: 'Object reference not set to an instance of an object.' form2 was null." – Mohammed Farttoos Jul 24 '20 at 07:07
  • @MohammedFarttoos The two windows form are in two projects? If in the same one, you need to modify `Application.OpenForms["here is the name of form you want to get"]`. – 大陸北方網友 Jul 24 '20 at 07:18
  • I know I take your time and I appriciate all what you did, I wish sending me the demo as code to understand becouse I still have error – Mohammed Farttoos Jul 24 '20 at 07:40
  • @MohammedFarttoos If the answer helps you, you can click `√` to [mark it as answer or upvote it](https://stackoverflow.com/help/someone-answers). – 大陸北方網友 Jul 24 '20 at 07:57
  • OK I did, Thank you for support and great help – Mohammed Farttoos Jul 24 '20 at 09:38
  • Can you help me for my same problem but I need this solution in WPF, the link is: https://stackoverflow.com/questions/63314798/how-to-use-get-set-in-windows-presentation-foundation-wpf – Mohammed Farttoos Aug 09 '20 at 00:07