Passing information between C #

1

Hello,

I have a problem with passing information between forms in windows forms with C #, the problem is the following. I have a MAIN FORM, from this, I call the one SUB FORM, which happens to be the child's principal. The point is, I want the SUB FORM to pass me, or return some information back to the MAIN. It can be a Boolean value, list, anyway, I want to pass any value.

Example:

NO MAIN FORM:

private void buttonDiretorio_Click(object sender, EventArgs e)
    {
        Form Frm = new FormOpenDirectorySystem();
        Frm.ShowDialog();
        bypass = Frm.bypass;

        if (bypass)
        {
            MessageBox.Show("O acesso foi concedido.", "Acesso liberado.", MessageBoxButtons.OK, MessageBoxIcon.Information);

            if (Directory.Exists("C:\Diretorio\"))
            {
                //abro o diretório
            }
            else MessageBox.Show("O diretório raiz do sistema não foi encontrado.", "Problema ao abrir o diretório raiz", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

ON THE SUB FORM THAT IS CALLED BY THE MAIN FORM:

public partial class FormOpenDirectorySystem : Form
{
    public FormOpenDirectorySystem()
    {
        InitializeComponent();
    }

    public bool bypass = false;

    private void buttonCancelar_Click(object sender, EventArgs e)
    {
        bypass = false;
        this.Close();
    }

    private void buttonContinuar_Click(object sender, EventArgs e)
    {
        String password = txtBypass.Text;
        if (password == "1q2w3e4r5t")
        {
            bypass = true;
            this.Close();
        }
        else
        {
            bypass = false;
            this.Close();
        }
    }
}

In my example, I am in the main form, and I call a new screen, where I will read a password / password / bypass, and I want this bypass to return TRUE if the "authentication" was successful , or FALSE if it was not successful.

Sincerely;

    
asked by anonymous 19.11.2016 / 22:20

2 answers

1

You can do this with a public property on the secondary form

public partial class FormOpenDirectorySystem : Form
{
    public bool Sucesso { get; private set; } = false;

    public FormOpenDirectorySystem()
    {
        InitializeComponent();
    }

    private void buttonCancelar_Click(object sender, EventArgs e)
    {
        Sucesso = false;
        this.Close();
    }

    private void buttonContinuar_Click(object sender, EventArgs e)
    {
        if(loginSucedido)
            Sucesso = true;

        this.Close();
    }
}

In use

Form frm = new FormOpenDirectorySystem();
frm.ShowDialog();
bool sucesso = frm.Sucesso;
    
19.11.2016 / 22:48
0

Hello, To do it is very simple, however, imagine that the form you indicated is a class (and is really a class) and that this class also has methods (buttonContinuar_Click) and properties. So, in this line you should only declare a public property in this class. For example:

public partial class FormOpenDirectorySystem : Form {
//Esse propriedade porderá ser acessar a partir da rotina chamadora
public bool ByPass { get;private set {value = bypass};}

public FormOpenDirectorySystem()
{
    InitializeComponent();
}

public bool bypass = false;
.
.

// The call would be ....

using(frm = new FormOpenDirectorySystem()){
   frm.ShowDialog();
var valorRetornado = frm.ByPass;
}
    
19.11.2016 / 22:42