Form activated

M

Mark

I have two forms. The first form has a datagrid. The second form is opened
by the first form. When the second form closes, I want the datagrid on the
first form to refresh.

I tried using the Activated and Enter events of the first form, but neither
fires when the second form closes even though it's the first form that is
then given the focus. I don't see a "Got Focus" or similar event.

Suggestions? Thanks in advance.

Mark
 
A

Ajay Kalra

Handle form_Closing event in second form. You can pass the first form to
second form in its constructor.
 
D

Dave

Pardon me for butting in, but how would you do this?


If Form1 has the following code to launch Form2:

private void button1_Click(object sender, System.EventArgs e)
{
Form2 myForm2 = new Form2();
myForm2.Show();
this.Hide();
}


Then on Form2 when I want to redisplay Form1, how do I "pass the first form
to second form in its constructor."

If I do this on Form2:

private void Form2_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
Form1 myForm1 = new Form1();
myForm1.Show();
}

Then a Form1 will display when Form2 closes but it will be a new Form1. The
original Form1 remains hidden so now I have two Form1's in memory.

Is this the proper way to handle this?

Dave


Ajay Kalra said:
Handle form_Closing event in second form. You can pass the first form to
second form in its constructor.

--
Ajay Kalra [MVP - VC++]
(e-mail address removed)


Mark said:
I have two forms. The first form has a datagrid. The second form is opened
by the first form. When the second form closes, I want the datagrid on the
first form to refresh.

I tried using the Activated and Enter events of the first form, but neither
fires when the second form closes even though it's the first form that is
then given the focus. I don't see a "Got Focus" or similar event.

Suggestions? Thanks in advance.

Mark
 
M

Mark

feel free to butt away ...

Ajay's suggestions worked just fine, although i still don't get why my
original Activated and Enter events didn't do the job.

I'm winging this in pseudo code, but in form2, you'd need to change it to be
something like ...


public class Form2
{

private Form1 _frm;

public Form2(Form1 frm)
{
_frm = frm; //Sets a reference to the form1 instance;
}

public void CloseSomethingOrOtherEvent()
{
//Note that this does NOT create a new instance of the form, but uses
the existing reference held in _frm.
_frm.SomePublicMethodOnForm1ThatRefreshesDataGrid();
}

}

***** AND THEN *****

1. In form1 you'd do something like:
Form2 myForm2 = new Form2( this );
myForm2.Show();



HTH.

Mark
www.dovetaildatabases.com



Dave said:
Pardon me for butting in, but how would you do this?


If Form1 has the following code to launch Form2:

private void button1_Click(object sender, System.EventArgs e)
{
Form2 myForm2 = new Form2();
myForm2.Show();
this.Hide();
}


Then on Form2 when I want to redisplay Form1, how do I "pass the first form
to second form in its constructor."

If I do this on Form2:

private void Form2_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
Form1 myForm1 = new Form1();
myForm1.Show();
}

Then a Form1 will display when Form2 closes but it will be a new Form1. The
original Form1 remains hidden so now I have two Form1's in memory.

Is this the proper way to handle this?

Dave


Ajay Kalra said:
Handle form_Closing event in second form. You can pass the first form to
second form in its constructor.

--
Ajay Kalra [MVP - VC++]
(e-mail address removed)


Mark said:
I have two forms. The first form has a datagrid. The second form is opened
by the first form. When the second form closes, I want the datagrid
on
the
first form to refresh.

I tried using the Activated and Enter events of the first form, but neither
fires when the second form closes even though it's the first form that is
then given the focus. I don't see a "Got Focus" or similar event.

Suggestions? Thanks in advance.

Mark
 
B

Bjarke Lindberg

Mark said:
feel free to butt away ...

Thanks ;)
public Form2(Form1 frm)
{
_frm = frm; //Sets a reference to the form1 instance;
}

Well - this way Form2 is dependant on Form1.

Wouldn't it be 'cleaner' if you let Form1 handle the Form2's
CloseSomethingOrOtherEvent?

1. In form1 you'd do something like:

Or:

Form2 myForm2 = new Form2();
myForm2.CloseSomethingOrOtherEvent
+= new EventHandler(this.HandleForm2Close);
myForm2.Show();


/B
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top