Pass information between C#RMS

Asked

Viewed 582 times

1

Hello,

I have a problem to pass information between windows forms Forms with C#, the problem is as follows. I have a MAIN FORM, from this I call the a SUB FORM, which happens to be the son of the principal. The point is, I want SUB FORM to give me, or return information back to MAIN. It can be a boolean value, list, anyway, I want to pass any value.

Example:

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

    }

IN THE SUBFORM 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 this 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 "authentication" was successful, or FALSE if it was unsuccessful.

Sincerely yours;

  • You probably want some of this one: http://answall.com/search?q=%5Bc%23%5D+entre+Forms

  • @bigown I was really looking for a dup for this, but it seems like everyone wants to do the opposite.

  • John, I wrote an answer and now I realize what you do exactly that in the question code! What is the problem with the current code?

  • the problem is exactly that it does not work, kk What would be the error in the code because I am trying with this method and just have no return of anything.

  • Doesn’t work like? Be clearer! What happens? Does it make a mistake? The return is always the same?

  • Good,. in the sample code I try to access the property of my SUB FORM, frm.bypass, and step it to bypass , in my main form.

  • It turns out that in the main form, I have the error that there is no "bypass" property in "frm".

Show 2 more comments

2 answers

1


You can do this with a public property in 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;
  • Visual Studio returns me an error saying that the property that is in the secondary form does not exist, and it is public.

  • Dei Build in the project, the error persisted .. I closed and reopened the Visual Studio, and now the error has disappeared and worked perfectly.

  • Here’s the solution, it was right, but the visual studio was bugged and wouldn’t let me compile the program ... Thanks anyway!

0

Hello, To do it is very simple, poisbem, imagine that the form indicated by you is a class ( and is really a class) and that this class also has methods (buttonContinuar_Click) and properties. So on 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;
}

Browser other questions tagged

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