-4

I want to refresh an already opened form (form1) from another opened form's (form2) button_click(). In form1 I display the data saved by form2 and when form1 is already opened I want it to refresh if new data is saved on form2 in order to display it.

The problem is that I've tried iterating through `Application.Openforms`, but it turns out that it is read-only and I cannot access the form once found and I don't know how to access *form1* from *form2*, since I can't simply find it.

How can I access *form1* from *form2*?

Edit:

Form1 is actually opened from Form2.

The problem with Application.Openforms is that , as I've stated, a read-only list of forms already opened and I cant actually access the forms through it. They simply don't have the methods for it, I sugest you try using Application.OpenForms and look it up if you don't know how it works. Also it's pointless to show what I've already tried because it includes Application.OpenForms, but for the sake of information:

FormCollection of = Application.OpenForms;
                foreach (var f in of)
                {
                    if (f.GetType().ToString() == "Kontrl_Doc.Visualizar")
                    {
                        f.Refresh();
                    }
                }

When I click the button (button_click()) in Form2 it checks if Form1 is open or not. If Form1 isn't open it opens one and if it is than I'd like it to refresh it. Simultaneously, it closes Form2 and opens Form2 again, in order to reset is fields.

What I wan to do is, if the form1 is already opened , it form2 should tell it to refresh the already opened window with the form 1.

Giovanni Di Toro
  • 797
  • 1
  • 14
  • 34
  • 1
    And what it is the problem with read-only `OpenForms` collection when you want to update **form's content**, not to replace the form? What do you mean by `I cannot access the form once found`? Show what you have already tried and what has failed to produce the desired result. Also, have you read http://stackoverflow.com/questions/10704020/changing-a-labels-text-in-another-form-in-c and http://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form? – Eugene Podskal May 29 '15 at 14:35
  • Is *form1* opened from *form2* or the other way around? Are they both opened from a common parent? Have you looked at/searched for how to set properties in one class from another (since forms are instances of a class) – Bernd Linde May 29 '15 at 14:46
  • "from another opened form's (form2) button_click()" When this button is clicked, do you expect Form2 to close and return to Form1?...or will Form2 stay open? – Idle_Mind May 29 '15 at 15:00
  • "_Form1 is actually opened from Form2_" - if this is the case, then just call Refresh using the form variable that you have in Form2. If necessary, make that a private field in the Form2 class or store it in an array for later use. – Chris Dunaway May 29 '15 at 15:56
  • At the point in Form2 where you open Form1, you should store the reference to Form1 in a variable so you can use it later. – Chris Dunaway May 29 '15 at 16:01
  • A hint for later: use relevant names to you forms. Than you didn't shearch to a form if you have more than a pair of forms. – H. Pauwelyn May 29 '15 at 16:17
  • @Hein Those aren't the actual names , the names are in Portuguese so I made it easy for the question. Thanks! – Giovanni Di Toro May 29 '15 at 18:31

3 Answers3

2

"Form1 is actually opened from Form2" - If this is the case, then just call Refresh using the form variable that you have in Form2. If necessary, make that a private field in the Form2 class or store it in an array for later use.

For example:

(Somewhere in Form2)

Form1 form1 = new Form1();
form1.Show();

(Inside the button click in Form2)

form1.Refresh();
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
  • 1
    That would just create another instance of form1 and open it in another window, What I want to do is: If I've already opened form1 , I want it to refresh the already opened window with that form. – Giovanni Di Toro May 29 '15 at 16:05
  • You said before in your post that you opened Form1 from Form2. When you do that, save the reference in a variable. You can than use that reference later on when you need to refresh. – Chris Dunaway May 29 '15 at 16:07
2

you can use events. In form2 you place this code

public event Action ReloadForm1;

//on the place where you will reload form1
ReloadForm1();

and in form1 if you have opening form2:

form2.ReloadForm1 += Reload;

//outside method
void Reload()
{
    this.Reload();
}
H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
  • @Giovanni Di Toro: if you find an answer good please accept it. Welcome to stackoverflow. more info: www.stackoverflow.com/tour – H. Pauwelyn Jun 10 '15 at 20:18
1

Create a void method in form1 and add the components u want to refresh maybe you want to reload a dropdown from db

public void Refresh()
{
  ...
}

then open a dialog of form2 catch the dialog result

Teezy7
  • 515
  • 5
  • 9