Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling event method of a form from another form. 1

Status
Not open for further replies.

junwebhead

Programmer
Jun 13, 2003
41
QA
Hi all!

Does anyone know how to call the event method of a form from another form? Actually, these forms are MDI childs. I wanted to fire up the refresh button of the first form when I closed the second form (in which I made some changes to some data that the first form displays).

I've try this:

// I made the method FillListView in the first form public

private FirstForm form1 = new FirstForm();

// then in the closed event of the second form...

form1.FillListView();

... But I have no luck. Can anyone help me please. I'm new in C# (coming from VB). I'm also curious that my code is really object oriented because I tend to use my techniques in VB.

Thanks in advance!!!
[bigsmile]

Jun
 
Hi!
This is my first help-post on C# forum, but allow me to clear a few things first.
You have two forms: form1 and form2. Closing form2 will call form1.FillListView() to refresh whatever.

[tt]
// I made the method FillListView in the first form public

private FirstForm form1 = new FirstForm();[/tt]
Where did you instantiated form1 (where is this code placed)? If in form2, you're not "refreshing"
form1, but an invisible "form3" that form2 created!!! What you want to know is how to call public method FillListView() of the already existing instance of FirstForm, form1.

There are a few tricks off the top of my head. One way is to use form2.Owner property. Owner property, well, is a reference to owner form. However, by default, this property references the MDI container. So, before showing form2, set its Owner property equal to the existing instance of FirstForm. Read MSDN on this property for more info.

To access your public method FillListView() through this property within form2...
Code:
FirstForm frm = (FirstForm)form2.Owner
frm.FillListView();

Another, you can overload form2 constructor so that it can accept a reference of FirstForm.
A static reference will also do.

There are other ways other than what I've already given and perhaps more OO.
BTW, how did you showed form2? Show() or ShowDialog()?


[peace]
 
Thanks for the quick response. I appreciate it.

I'm sorry I had a mistake in my question. Actually, Form1 is an Mdi child while

form2 is not (cause I want to show it modally i.e., using ShowDialog()).

You're right, I'm instantiating form1 in form2.

Yes. I want to know is how to call public method FillListView() of the already

existing instance of FirstForm, form1.

I still didn't try what you're suggesting because of my question mistake (I used

ShowDialog()). Is there a difference on what you said if I used ShowDialog()?

Thanks a lot! Have a star for your first help-post!!!

Jun
 
So, form1 calls form2.ShowDialog()? If so, you have you're solution. You don't have to call form1.FillListView() on form2, BUT, after form2.ShowDialog() completes its message loop. Assuming form2 is an instance of DataEntryForm class, you can do:
[tt]
DataEntryForm form2 = new DataEntryForm();
form2.ShowDialog();
/* Here, you don't have to bother what scope is FillListview(), unless you still want to share it with other objects in your project.
*/

this.FillListView();
[/tt]
After all, form2 sole task is to provide an interface for data entry. To my opinion, you should not oblige form2 to refresh form1 when it completes its task. But form2 must somehow notify form1 if form2 successfully committed the data.
[tt]
DataEntryForm form2 = new DataEntryForm();
form2.ShowDialog();
/* Here, you don't have to bother what scope is FillListview(), unless you still want to share it with other objects in your project.
*/

bool isCommitted = form2.Committed;
if(isCommitted)
this.FillListView();

[/tt]
All you need now is provide a "Committed" (or whatever you name it) member for form2 to determine if form2 successfully committed the data or not.

Hope this helps. [peace]
 
Thank you again!
I made it worked! I'd try using static reference just before I ask this question but is not working. I can't make it work using the owner property either so I just overload form2 to accept form1's reference and it works. Also based on your latest suggestion, I call FillListView() right after form2.ShowDialog() and it works perfectly. And I think it's much better. I also implemented refreshing only if the save button was clicked. Great idea.

I have a lot of tutorials and ebooks about C# but I think I'm not progressing fast enough about C#. I have a college background in C and pascal but started to learn VB instead rather than continue with C/C++. Which I think maybe the reason why I'm having difficulty learning C#. Can you suggest a good resource material for C# based on my programming history? I will greatly appreciate it.

Thanks a lot!

Jun
 
Thank you again!
I made it worked! I'd try using static reference just before I ask this question but is not working. I can't make it work using the owner property either so I just overload form2 to accept form1's reference and it works. Also based on your latest suggestion, I call FillListView() right after form2.ShowDialog() and it works perfectly. And I think it's much better. I also implemented refreshing only if the save button was clicked. Great idea.

I have a lot of tutorials and ebooks about C# but I think I'm not progressing fast enough about C#. I have a college background in C and pascal but started to learn VB instead rather than continue with C/C++. Which I think maybe the reason why I'm having difficulty learning C#. Can you suggest a good resource material for C# based on my programming history? I will greatly appreciate it.

Thanks a lot!
[bigsmile]

Jun
 
I understand you're condition as I was a VB'er too (actually, am still fond of using it for "quick" projects). Honestly, I'm only 2 months old with C#. But good resources does matter! These are must have: 1) MSDN 2) decent forums (eg Tek-Tips) 3) download code-samples.

You can also visit these sites:

and...
[wink]

[peace]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top