7

I have been developing project in c#.

It has 2 form and these are connected with between each other.

I want to do that when second form is closed, first form refresh.

If I use Thread's Sleep program will be tired. I want to do this with closing events. How can I do ?(Like java's repaint)

Codes are below: Form1

public static Form1 form;
    public Form1()
    {
        InitializeComponent();
        form = this;
    }

    private void button11_Click(object sender, EventArgs e)
    {
        Form2 yeniform = new Form2();
        yeniform.Show();

    }

Form2(Close Button)

 private void button1_Click(object sender, EventArgs e)
    {

            Form1.form.Invalidate();
            Form1.form.Refresh();
            this.Close();
    }
OEASLAN
  • 150
  • 1
  • 1
  • 8
  • 1
    Be careful with object references! You create `Form2` in button click handler after which reference to `Form2` goes out of scope and therefore `Form2` object becomes eligible for garbage collection. GC might destroy `Form2` before you have chance to click button on it. – Bojan Komazec Jul 06 '14 at 10:25
  • 1
    Have you thought about showing form2 modally? (showdialog) – Paul Michaels Jul 06 '14 at 10:27
  • What are you really trying to do? __Why__ would the 1st form have to repaint itself?? You can use the `closing` event to trigger things in the 1st form, but first explain just what the problem is! If you know you want to use the `closing` event, well why not simply do it?? – TaW Jul 06 '14 at 17:29
  • create an event on each form that Form1() will open and make Form1 subscribe to this event and on the Closing of other form make them trigger their EventHandler and Form1 will be notified. Another way would be to pass Form1 by reference to all other forms so that on closing they can call some methods inside Form1 to do refresh as well – Franck Jul 07 '14 at 02:00
  • I can control forms event but I should refresh the first form because. When second form close, it serialize the data Then first form read new data. – OEASLAN Jul 07 '14 at 09:23

3 Answers3

9

Bind Form_Closing event in your first form.

//Form1
private void button11_Click(object sender, EventArgs e)
{
    Form2 yeniform = new Form2();
    yeniform.FormClosing += new FormClosingEventHandler(this.Form2_FormClosing);
    yeniform.Show();

}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
    //Do your stuff here.
}
Shell
  • 6,818
  • 11
  • 39
  • 70
0

' button in the second form which is to be closed ' form1 is the form which is to be re loaded when form 2 is closed

private void btn_close_Click(object sender, EventArgs e) form1.close() 'unload form 1 before closing form2 form1.show() ' form 1 reloading unload(me) 'closing form2 end sub

Suji
  • 1,326
  • 1
  • 12
  • 27
0

This is a working sample. In parent form

private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
    VendorsAddForm f = new VendorsAddForm();
    f.StartPosition = FormStartPosition.CenterScreen;
    f.FormClosed += new FormClosedEventHandler(child_FormClosed);
    f.Show();
}

void child_FormClosed(object sender, FormClosedEventArgs e)
{
    this.Refresh();
    var query = dbContext.AccountObjects.Where(p => p.IsVendor == true).ToList();
    accountObjectsBindingSource.DataSource = new BindingList<AccountObject>(query);
}

Note: child form is VendorsAddForm

Thank to https://www.daniweb.com/posts/jump/1302760 , I learned from there.

Vy Do
  • 46,709
  • 59
  • 215
  • 313