How to take the Text of a Textbox in a Second Form and assign it to a String in the main form?

Asked

Viewed 346 times

0

Well the goal is simple: assign the value of textBoxKey of password class in String keySimetric of class Acecrypt. But I don’t know how to receive and cast :(

Class of password entry form.

public partial class Senha : Form
{

    string key;

    public Senha()
    {
        InitializeComponent();
    }

    private void btn_enviarKey_Click(object sender, EventArgs e)
    {
        key = textBoxKey.Text;
        this.Close();
    }
}

Class of the Main form.

public partial class ACEcrypt : Form
{

    string chaveSimetrica;

    public ACEcrypt()
    {
        InitializeComponent();
    }

    private void btn_criptografar_Click(object sender, EventArgs e)
    {
        if (openFileCrypt.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            exibirCaminho.Text = openFileCrypt.FileName;

            //Input do Arquivo ou Pasta
            string streamPath = exibirCaminho.Text;

            FileStream inStream = File.OpenRead(openFileCrypt.FileName);
            //FileStream outStream = File.OpenWrite(openFileCrypt.FileName + ".ace");
           // int b;

            //while ((b = inStream.ReadByte()) > -1)
            //    outStream.WriteByte((byte)b);

            //outStream.Flush();
            //outStream.Close();
            inStream.Close();

            Senha ISCrypt = new Senha();
            ISCrypt.ShowDialog();
            ISCrypt.FormClosing += new FormClosingEventHandler(ISCrypt_FormClosing);

            //Para teste
            //MessageBox.Show(chaveSimetrica);

        }
    }

    void ISCrypt_FormClosing(object sender, FormClosingEventArgs e)
    {
        if ((sender as Senha).textData != null)
            chaveSimetrica = (string)(sender as Senha).textData;

    }

    private void btn_descriptografar_Click(object sender, EventArgs e)
    {
        if (openFileDecrypt.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            exibirCaminho.Text = openFileDecrypt.FileName;

        }
    }
}

1 answer

2


In the form Senha, you can create a publish property that returns the string key.

public string Key
{ 
    get{ return key;}
}

Then after the ISCrypt.ShowDialog(); you would be able to access ISCrypt.Key;

Senha ISCrypt = new Senha();
ISCrypt.ShowDialog();
chaveSimetrica = ISCrypt.Key;
  • Thank you very much! It worked perfectly!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.