Return - Method called on form1, and return on form2

0

Well, good night, a doubt. I have a form1 that calls a class and a method, sending this method two string parameters, this method processes, and returns an int value.

My question is, can I call this method on form1 and its return on form2?

If yes, how? If not, what should I do? I need the form1 parameters to execute the method.

Thank you

    
asked by anonymous 05.06.2016 / 23:39

2 answers

1

If this is what I am thinking is simple. Here's an example:

    static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Form1 f1 = new Form1();
        Form2 f2 = new Form2();
        Execute teste = new Execute();
        teste.DefineNome("Este é o novo nome", f2);
        f2.Show();
        Application.Run(f1);
    }
}

public class Execute
{
    public string DefineNome(string Nome, Form Formulario)
    {
        string novonome = "Completei" + Nome;
        Formulario.Text = novonome;
        return novonome;
    }
}

In the above example we have two forms. The Execute class I created has a method that asks what form will be updated. At the moment of requesting the method "DefineName" I pass two parameters, the first is any text and the second is the form that has already been started. Remember that if this target form is not already instantiated the "null" error will occur during the execution of your project

    
06.06.2016 / 20:25
1

Thank you for your help. Today, I was able to resolve by doing the following:

In form1 I added in my method:

public void logar()
    {
        c_consulta.sqlconexão();
        SqlConnection sqlcon = new SqlConnection(c_consulta.sqltring);

        usu = textBox1.Text;
        pass = textBox2.Text;

        int ret_eid = trazerid.retorna_eid(usu, pass);
        int ret_uid = trazerid.retorna_uid(usu, pass);
        //idchange(ret_eid, ret_uid);

        Tinicial telamenu = new Tinicial(ret_eid,ret_uid);
        telamenu.Show(); 
    }

No form2 I added in the constructor:

public Tinicial(int ret_eid, int ret_uid)
    {
        InitializeComponent();
        label7.Text = Convert.ToString(ret_eid);
        dados_cliente(ret_eid);
        dados_usuario(ret_uid);                                
    }

It worked, but I know it's not the best way to do it, right? I'll try to do it for threads. if they could advance me some good text on, it would be grateful.

Thank you people

    
07.06.2016 / 17:18