Communication between Forms

Asked

Viewed 574 times

1

Hello. I am working on an application that should work as follows: When running, a main form (Form1) should open and always remain in the background. This main form contains options for the user to make their choices through a button with the text "add". When the user clicks on the "add" button, he must open another form (Form2) in which the user must place his information in textbox and then click on a button with the "save" text. By clicking the "save" button Form2 should be closed. The main form should always be open at the bottom. The user will have other options in the main form that he can choose to fill through other Forms (Form3, Form4...Formn) and whenever he clicks "save" in the Forms they will be closed remaining only the Form1.

In Form1 you will have a "generate report" button that when you click on it you will have to export all the data saved in the other Forms to an Excel spreadsheet.

I already built the application with all Forms and code to generate the report.

The question is: How do I send data from other Foms to Form1 when it will be closed or will be closed when the user clicks "save" ??

I have tried several communication alternatives between Forms but without success. I have checked all options only work if I give a command . show() and open the main form again...but in my application this cannot be done...

How do I save data without having to open Forms again...??

  • Confused your question, I even know the communication response between form but, the auxiliary Forms (form2, form3 etc), sends exactly which update to the main form?

  • The Forms 2, 3, 4...n will have textbox where the user will put the quantity information, product code, installation position finally, things inherent to the project... When filling in the data in the form2, 3, 4...n the user must click on a button that saves this information and closes the form2, 3, 4...or n...leaving only the Form1 that was in the background (maximized). Form1 has a "generate report" button that exports all data filled in forms2, 3, 4...n to an excel table...

  • However, in the tests I did, I only managed to generate the table in excel with the data, if by clicking on the save buttons of forms2, 3, 4...n I open the Form1 again (which was already open...in the background)...otherwise I can not transfer the data of the forms2 textbox, 3, 4...n for some vector, memory etc......

  • It seems that the update only occurs at the time the form is executed (open)...

  • put the opening code of an item and what on Form1 needs to stay with the values of the other Forms!

  • Actually your question is a bit confusing, but apparently the only way you can keep the data and use it in different Forms would be by using a database. Att. Flávio.

  • Possible duplicate of Pass information between C Beams#

  • Hello, you can create global variables. So a variable created in form 1 can be changed from other forms.

Show 3 more comments

1 answer

0

When opening another form, pass as parameter the form1 to be able to access a method within it to be able to store the information returned from the other form.

EXAMPLE FORM 1:

private void Button1_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2(this); //passe como parâmetro este form1.
}

public void SalvarDadosForm2(string dado1, string dado2, string dado3)
{
    //Salve os dados em variáveis ou outra forma que desejar, 
    //Pode criar um método único ou um para cada form, você quem sabe o melhor jeito.
}

TOO MANY EXAMPLE FORMS

In the constructor of the other Forms apply:

public partial class Form2: Form
{
    Form1 form1;

    public Form2(Form1 form1)
    {
        InitializeComponent();

        this.form1 = form1;
    }
    // Outras linhas de código, não interessantes ao exemplo.
}

Then do everything you need to do and in the method where the validation of the data from the other Erms ends, include:

   form1.Show();

   form1.SalvarDadosForm2(textbox1.Text, textbox2.Text, textbox3.Text); //Envia as informações para o form1

   form1.Focus();

   this.Close();
  • So, if I put Form1.Show(); there it works, but the screen that is in the maximized background will open again...and this is what I can’t have in my application. Without putting Form1.Show(); then the variables are not updated...I tried to create a specific class to save the values entered in the textbox, to then call these variables from Form1, but also did not work...

  • I will test Form1.Refresh();

Browser other questions tagged

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