|
Hi! I'm trying to call a function from a parent form in a child form before the child form is closed. I tried overriding the refresh function but was recommended on Codeproject to instead create my own refresh function and call it using a casted this.parent.
Unfortunately I'm having issues in doing this. (Never would have imagined setting up a table to refresh would be so hard!)
A little background on this program. The form in question itself is a child of the UI which is passed an employee object from the mainUI. From there it calls a function (getSearch) which returns a DataSet to the first child. The first child dispplays the dataset into a table. From there you can select a row and move into an edit form (second child) and what I need it to do is refresh the first child (parent of the second) automatically upon execution of code without having to resort to a refresh button on the first child form.
Unfortunately my current status is this: if (eo.EmployeeEdit(oo)) { MessageBox.Show("Changes saved!"); ((SearchWindow)this.Parent).RefreshData(); this.Close(); }
I am getting exception: System.NullReferenceException: Object reference not set to an instance of an object.
And my current google crawl is not providing me with information.
What I got in response on codeproject was this:
Sounds like you haven't set the parent of the child form then.
I had assumed though that when a child is instantiated in its parent that it would automatically inherit the parent property.
I'm running into all sorts of trouble just to try to refresh a window. =)
Anyways I set the parent using this:
eef.Parent = this; (eef being the instance of the child form) then displayed it with eef.ShowDialog();
but unfortunately the window does not show and additionally it closes the parent form! I get this exception: System.ArgumentException: Top-level control cannot be added to a control. I can't do eef.ParentForm = this; because .ParentForm is read only.
What am I doing wrong?
|
| Veralix Wednesday, March 28, 2007 4:13 PM |
Try setting the Owner property of the Dialog window:
Code Snippet
using(frmMyDialog eef = new frmMyDialog())
{
eef.ShowDialog(this);
}
And in your dialog window:
Code Snippet
(this.Owner as frmSearchWindow).RefreshData();
|
| kryzchek Wednesday, March 28, 2007 4:43 PM |
Try setting the Owner property of the Dialog window:
Code Snippet
using(frmMyDialog eef = new frmMyDialog())
{
eef.ShowDialog(this);
}
And in your dialog window:
Code Snippet
(this.Owner as frmSearchWindow).RefreshData();
|
| kryzchek Wednesday, March 28, 2007 4:43 PM |
Thanks! It worked wonderfully!
What was I doing wrong?
|
| Veralix Wednesday, March 28, 2007 4:51 PM |
You need to set the Owner property, not the Parent property.
|
| kryzchek Wednesday, March 28, 2007 4:56 PM |
| posted code wrote: |
|
Code Snippet
(this.Owner as frmSearchWindow).RefreshData();
| |
Just a comment here... This may potentionaly lead to a thrownexception,when this dialog is called from a form, other than frmSearchWindow. A better way would be something like declaring and raising an event from this child dialog and handle it on the parent form.
Andrej |
| Andrej Tozon Wednesday, March 28, 2007 5:58 PM |
Unfortunately my skill with events involves clicking to add a button control and double clicking on it to post the event.
Do you perhaps have a link to somewhere where I can learn about events?
|
| Veralix Wednesday, March 28, 2007 6:29 PM |
Rereading your original post, I would like to ask why you even have to refresh your data on the parent form? This isn't the same data you're passing the editor (second child)?
Andrej |
| Andrej Tozon Wednesday, March 28, 2007 7:55 PM |
No. In the first child is a table pulled from a SQL Server DB displayed with a dataGridView control based on criteria specified in the main parent. From there a single row can be selected to be modified (or deleted) in the editor window which upon saving or closing refreshes it's parent window instead of dealing with a refresh button. Since the DataSet is pulled from the database and the dataGridViews source is the DataSet the DataSet must be pulled again to be redisplayed.
|
| Veralix Wednesday, March 28, 2007 8:46 PM |
| Veralix wrote: |
... From there a single row can be selected to be modified (or deleted) in the editor window ...
| |
I was thinking that if you'd edit this same row in the editor window (and save changes), you wouldn't have to refresh the dataset, because it would already carry the latest data. That would perhaps require you to write some additional binding code, but would be worth it.
For this kind of binding scenario,follow the fourth informaional link in this Windows FormsFAQ post.
Andrej |
| Andrej Tozon Wednesday, March 28, 2007 9:00 PM |
The changes are made to the actual database though, not the dataset. Unless I made changes to the dataset itself I'd have to requery the database in order to update the dataviewer.
|
| Veralix Wednesday, March 28, 2007 9:29 PM |
In this case, it might be better to manipulate the existing dataset and update it. It will save you from requerying...
Anyway, here's another thing we can try before going with event creation...
When you call your editor form, you could subscribe to its FormClosed event and handle refreshing there... Would something like that work for you?
Andrej |
| Andrej Tozon Thursday, March 29, 2007 7:36 AM |
See... I'm very new at C# and VS in general (I grew up with C++ on Borland) so I really have no idea how to do that either.
I'm still learning about multiple form interaction. Event handling is also new to me as well. I'm guessing that I should create a delegate that subscribes to the event (form).FormClosed and upon execution refreshes the data?
I'm still trying to understand how delegates and events and handling those events work.
|
| Veralix Thursday, March 29, 2007 1:34 PM |
Still looking for a way to simplify this...
For example, if you call your search child using the following code, it would only call RefreshData on the same form, when the child form is closed with DialogResult property set to OK:
Code Snippet
using (SearchWindow childForm = new SearchWindow())
{
if (childForm.ShowDialog() == DialogResult.OK)
{
RefreshData()
}
}
You'll set child form's DialogResult when clicking OK or Cancel button:
Code Snippet
this.DialogResult = DialogResult.OK // or DialogResult.Cancel
Andrej
|
| Andrej Tozon Thursday, March 29, 2007 2:41 PM |