Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / December 2004

Tip: Looking for answers? Try searching our database.

Form activated

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark - 04 Dec 2004 22:24 GMT
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
Ajay Kalra - 05 Dec 2004 03:03 GMT
Handle form_Closing event in second form. You can pass the first form to
second form in its constructor.

--
Ajay Kalra [MVP - VC++]
ajaykalra@yahoo.com

> 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
[quoted text clipped - 7 lines]
>
> Mark
Dave - 06 Dec 2004 17:57 GMT
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

> Handle form_Closing event in second form. You can pass the first form to
> second form in its constructor.
[quoted text clipped - 17 lines]
> >
> > Mark
Mark - 06 Dec 2004 19:21 GMT
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

> Pardon me for butting in, but how would you do this?
>
[quoted text clipped - 48 lines]
> > >
> > > Mark
Bjarke Lindberg - 14 Dec 2004 12:06 GMT
> 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:
>
>     > Form2 myForm2 = new Form2( this );
>     > myForm2.Show();

Or:

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

/B

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.