0

I have two Forms, Form1(main form) and Form2. Form 1 displays images files, pdf conversion, etc. But If user want to view Zip files, Form2 is called displaying a preview of all the Zip files available on a listView. If user selects a particular Zip file on Form2, the file is unzipped and image files sent to Form2. But I dont know how to refresh Form2 from Form1. By the way all the images from Form2 are now present in the variable list in Form! to be displayed but the form did not update.

form2 code:

 private void btn_read_Click(object sender, EventArgs e)
    {

        Form1 f1 = new Form1();
        f1.ReadArchives(Filepath);  //this function creates the image files on Form1
       this.Close(); //close form2

       for (int index = Application.OpenForms.Count - 1; index >= 0; index--)
        {
            if (Application.OpenForms[index].Name == "Form1")
            {
                //Application.OpenForms[index].Close();//EFFECTIVE BUT CLOSES THE WHOLE APPLICATION
               Application.OpenForms[index].Invalidate(); //no effect
                 Application.OpenForms[index].Refresh();//no effect
                Application.OpenForms[index].Update();//no effect
                Application.OpenForms[index].Show();//no effect
            }
        }            

    }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Basco
  • 87
  • 1
  • 1
  • 7

2 Answers2

0

You are instantiating new Form1 in your code - which is different from Form1 (main) you already have. This is why form doesnt get updated.

In my answer I show event-based approach to share variables/objects between different forms - it can be easily adjusted to transmit collection of objects. Hope it helps.

Community
  • 1
  • 1
Nogard
  • 1,779
  • 2
  • 18
  • 21
  • Thanks Nogard, Actually I am a beginner and I couldnt adjust your menthod to work in my case. Any further help will be much appreciated – Basco Feb 26 '13 at 02:34
0

So when you want the parent form to do something when something happens on the child form the appropriate mechanism is to use events. In this case the child form is being closed right when we want the information passed, so we can just re-used the FormClosed event.

This makes writing the child form very simple:

public partial class Form2 : Form
{
    public string Filepath {get;set;}

    private void btn_read_Click(object sender, EventArgs e)
    {
        Close();
    }
}

The parent form can then use the appropriate event to handle the rest:

public partial class Form1 : Form
{
    private void button1_Click(object sender, EventArgs args)
    {
        Form2 child = new Form2();
        child.FormClosing += (_, arg) => ReadArchives(child.Filepath);
        child.Show();
    }

    private void ReadArchives(string filepath)
    {
        throw new NotImplementedException();
    }
}
Servy
  • 202,030
  • 26
  • 332
  • 449
  • Thank you Servy, but I faced 2 issues when I try to implement this. 1) Form2 refused to close and 2) I have to close buttons on Form2. Close a) is just close form2 and close b)nis read archives and then close form2. Any suggestion? – Basco Feb 26 '13 at 02:33
  • Thanks again, Now its working perfectly. In the close only button I made Filepath=null, and in the ReadArchives(child.Filepath) I started with if (string.IsNullOrEmpty(child.Filepath)) return; – Basco Feb 26 '13 at 05:38