Return - Method called in Form1, and Return in form2

Asked

Viewed 129 times

0

Folks good night, one question. I have a Form1 that calls a class and a method, sending this method two string parameters, this method processes, and returns me an int value.

My question is, is it possible for me to call this method on Form1 and its return to form2?

If so, how? if not, what should I do? I need the parameters of Form1 to execute the method.

Thank you

  • 1

    It seems to me that you are learning to program the flow of an application with graphical interface. To help, try telling the real story of what you need to do. For example: do you need a "Wizard" (sequence of forms asking the user for information)? Or form2 is the result of processing Form1 (as if the first were a filter)?

  • @Caffé is right. You need to form your question better and say, for example, if these Forms are created in the same thread. If form2 is called by Form1 it is easy, for example if processing is finished before calling form2 this result can be used as parameter. If form2 is called before the processing is finished this can check (from time to time?) a public property of Form1 (set by the processing result). Already working with threads is kind of tricky for a beginner. Anyway it depends a lot on your scenario.

  • thank you very much for the comments, yes I’m beginning to venture into the world of programming, so I’m performing a login screen, which consults a bank where I have the information of my users, if successfully logged in, it sends me to a form2, where I have company information, and registered equipment, however, I need two information from Form1, id-user, and id-company, because in form2, I have a table that brings me which equipment is registered for that company, and I need the user id.

2 answers

1

Thanks for your help. Today, I managed to settle by doing the following:

on Form1 I added inside 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(); 
    }

In 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, though, I know it’s not the best way to do it, right? I’ll try threading. if they could advance me some good text on, I would be glad.

Thanks people

1

If this is what I’m thinking it’s simple. Look at this 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 example above we have two Forms. The Execute class I created has a method that asks which form will be updated. At the time of the "Definenome" method request, I pass two parameters, the first is any text and the second is the form that has already been started. Remembering that if this destination form is not instantiated yet the "null" error will occur during the execution of your project

  • Friend thanks for example, I will try to accomplish as you told me, today I managed to solve my problem by doing by constructor, however, I know that is not the recommended method to do this. I’ll try to improve and put here.

Browser other questions tagged

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